为什么编译器在类构造函数之前要求初始化?
How come the compiler asks for initializer before class constructor?
我正在创建一个名为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);
有人能告诉我为什么我得到一个错误吗?
如果您的代码中确实有这一行,那么您可能复制了两次构造函数:
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 ){
编译器抱怨初始化列表,因为它应该在头文件后面,而不是参数列表的另一个副本。
尝试在SelectionPage前面添加访问说明符
您可以在构造函数初始化列表中执行一些初始化,而在构造函数体中执行其余初始化。
SelectionPage::SelectionPage(
int newPoints,
string newManager,
string newLoginName,
string MemberFile)
: points(newPoints)
, manager(newManager)
, loginName(newLoginName)
, memberList(MemberFile)
{
// do the rest initialization here
}
相关文章:
- CLANG 编译器 说:变量"PTR"可能未初始化
- 初始化迭代器错误 C++ 在 GCC 编译器中
- C1001内部编译器错误是由于矢量初始化(如数组)引起的
- 为什么编译器接受具有长双精度文本的浮点数的初始化?
- 在MSDOS编译器上,用大于最大值的值初始化int时输出混乱
- 如何在macOS中的旧扩展clang和gcc编译器中初始化数组和向量
- (C++)for循环中的空初始语句是否保存所有编译器中变量的上一次初始化
- 使用编译器进行外部初始化时出错C++
- 如何让MSVC编译器优化多步POD初始化?
- 编译器是否在由 new 初始化的对象上调用隐式析构函数
- TBB 编译器错误 -"my_task":必须初始化引用
- 英特尔 TBB - "初始化关键部分 Ex":找不到标识符编译器错误
- 为什么编译器在试图初始化具有C 11样式的对象数组时隐含删除构造函数
- C编译器如何使用非初始化的变量处理
- 多变量初始化编译器支持
- 编译器错误,因为构造函数必须显式初始化成员
- Linux Opensuse C 编译器问题.(无法初始化部分调试器的解压缩状态)
- 为什么c++编译器接受这种初始化?静态int x=x
- 如何使编译器在实例变量未初始化时C++生成错误或警告
- 使用参数化构造函数初始化类成员 - 编译器认为我在声明一个函数?