-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);

有人可以向我解释为什么我收到错误吗?

提前致谢。

4

3 回答 3

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 ){

编译器抱怨初始化器列表,因为它应该跟在标题后面,而不是参数列表的另一个副本。

于 2011-10-22T10:15:36.980 回答
1

尝试在 SelectionPage 前面添加访问说明符

于 2011-10-22T10:16:14.083 回答
1

您可以在构造函数初始化列表中执行一些初始化,并在构造函数主体中执行其余初始化。

SelectionPage::SelectionPage(
  int newPoints, 
  string newManager, 
  string newLoginName, 
  string MemberFile)
  : points(newPoints)
  , manager(newManager)
  , loginName(newLoginName)
  , memberList(MemberFile)
{
  // do the rest initialization here
}
于 2011-10-22T10:20:39.887 回答