myMain.cpp
初始化:C++:的unique_ptr不正确
#include <memory>
#include "myClass.h"
static std::unique_ptr<myClass> classPtr; // Error thrown here
...
我在全局范围内初始化,因为所有的数据加载到这个类的属性需要一段时间,所以我想这样做一次,并有该数据一直存在,直到我给出一个明确的命令来删除它(classPtr.reset(nullptr)
)。
当我尝试编译这个:g++ myMain.cpp -o myMain.o
我得到:error: expected initializer before '<' token
。
为什么我得到这个错误?
我在myClass.h
和myClass.cpp
中定义了myClass
;我认为这个错误与构造函数有关。我简化了代码&,仅包含下面的重要行。
myClass.h
:
class myClass {
std::string dataPath;
std::vector<double> data;
public:
myClass(std::string P = "./path/to/data-file.csv");
~myClass() {}
const double findPercentile(double percentile = 0.0);
}
编辑:继从@FrançoisAndrieux我有固定我的构造函数提示。
myClass.cpp
:
myClass::myClass(const std::string P) :
dataPath(P) {
// read data-sets into class member variables
}
注意与你的问题有关:你声明你的构造函数为'myClass(std :: string)',但实现它为'myClass(const std :: string)'。你定义的参数具有默认值'“./path/to/data-file.csv”',但是你忽略了参数并始终使用默认值进行初始化。 –