2011-10-22 84 views
-1

我正在创建一个名为SelectionPage的类。基本上是一组菜单。编译器如何在类构造函数之前请求初始化程序?

然而,当我编译代码,编译器给了我以下错误:

g++ C_Main.cpp C_HomePage.cpp C_SelectionPage.cpp C_MemberManagement.cpp -o Project 
C_SelectionPage.cpp:9:104: error: expected initializer before ‘SelectionPage’ 
make: *** [Project] Error 1 

这里是C_SelectionPage.cpp的前几行:

#include "H_SelectionPage.h" 


//Constructor for the SelectionPage class 
//It assigns "managing" which decides if the user 
//is a manager or not. 
SelectionPage::SelectionPage(
    int newPoints, 
    string newManager, 
    string newLoginName, 
    string MemberFile) 
     SelectionPage(
      int newPoints, 
      string newManager, 
      string newLoginName, 
      string MemberFile) 
    { 
     points = newPoints; 
     manager = newManager; 
     loginName = newLoginName; 
     flatMemberList.clear(); 
     //Create Object Governing Flat Members. 
     memberList = MemberManagement temp(MemberFile); 
} 

这里是宣言的头文件中的构造函数:

SelectionPage(
    int newPoints, 
    string newManager, 
    string newLoginName, 
    string MemberFile); 

有人请向我解释为什么ia m出现错误?

在此先感谢。

+3

这是**真的**你在C++文件中有什么? 'SelectionPage :: SelectionPage(int newPoints,string newManager,string newLoginName,string MemberFile)** SelectionPage **(int newPoints,string newManager,string newLoginName,string MemberFile){? – Mat

回答

3

如果你在你的代码这行真的,你可能拷贝构造函数两次:

SelectionPage::SelectionPage(int newPoints, string newManager, string newLoginName, string MemberFile)SelectionPage(int newPoints, string newManager, string newLoginName, string MemberFile){ 

应该是这样的:

SelectionPage::SelectionPage(int newPoints, string newManager, string newLoginName, string MemberFile){ 

编译器抱怨初始化程序列表,因为这是应该遵循标题,而不是参数列表的另一个副本。

+0

我为我的无知道歉,我应该发现它......但经过一整天的编码,我想我只是没有看。非常感谢您指出我的错误。 – Synia

1

尝试SelectionPage

1

前面添加一个访问说明符可以在构造函数初始化列表执行一些初始化的和做休息初始化中构造体

SelectionPage::SelectionPage(
    int newPoints, 
    string newManager, 
    string newLoginName, 
    string MemberFile) 
    : points(newPoints) 
    , manager(newManager) 
    , loginName(newLoginName) 
    , memberList(MemberFile) 
{ 
    // do the rest initialization here 
}