构造函数参数问题C++

Constructor Parameter issue C++

本文关键字:C++ 问题 参数 构造函数      更新时间:2023-10-16

我有一个类的构造函数,它接受布尔值、数组指针和字符串。

TheConstructor(bool theBool=true, int *theArray=0, std::string message="0");

这是在头文件中写入它的正确方法吗?我的程序现在没有编译,因为"对"构造函数"和其他成员函数的未定义引用"。

这也可能导致这种情况的原因是什么?我检查了一下,在main中.cpp我 #included"Class.h"并定义了我在"Class.cpp"中写的"Class.h"中

#include <string> class C { public: C(bool b = 0, int *theArray = 0, std::string message = "0"); };

C.cpp:

#include "C.h"
C::C(bool b, int *theArray, std::string message)
{
}

未指定第一个参数的名称,但这可能不会导致您遇到的错误:

TheConstructor(bool=0, int *theArray=0, std::string message="0");

您可能想执行以下操作:

TheConstructor(bool flag=0, int *theArray=0, std::string message="0");

但是,如果没有看到定义,我无话可说。仅仅通过查看声明很难预测还有什么问题。