我正在尝试在Coeffs.cpp中设置一个静态变量:
#include "Coeffs.h"
class Coeffs
{
public:
double Coeffs::alpha5b = 0.0;
};
与头文件
#ifndef COEFFS_H
#define GOEFFS_H
class Coeffs
{
public:
static double alpha5b;
};
#endif
使用以下代码:
#include <iostream>
#include <fstream>
#include <string>
#include "json/json.h"
#include "Coeffs.h"
using namespace std;
int main()
{
cout << "start" << endl;
string json;
ifstream inputStream;
inputStream.open("coeffTest.json");
inputStream >> json;
Json::Value root;
Json::Reader reader;
bool parseSuccess = reader.parse(json, root);
if(!parseSuccess)
{
cout << "failed" << endl;
}
else
{
Coeffs::alpha5b = 1.1;
//Coeffs::alpha5b = root.get("alpha5b", "NULL").asDouble();
//double item1[] = root.get("delta21b", "NULL").asDouble();
//cout << "alpha5b is: " << Coeffs::alpha5b << endl;
}
cout << "done" << endl;
}
但是每次我编译我都会得到这个:
pottsie@pottsie:~/Documents/CoeffsJSON$ g++ -o JsonToCoeffs JsonToCoeffs.cpp -ljson_linux-gcc-4.6_libmt
/tmp/ccFxrr0k.o: In function `main':
JsonToCoeffs.cpp:(.text+0x10b): undefined reference to `Coeffs::alpha5b'
collect2: ld returned 1 exit status
艾夫研究了其他一些类似的问题,却找不到任何可行的方法。我尝试过添加一个构造函数并创建一个对象,但随后仍然遇到相同的错误。有人知道该怎么办吗?
类声明应放在标头(Coeffs.h)中
#ifndef COEFFS_H
#define COEFFS_H
class Coeffs
{
public:
static double alpha5b;
};
#endif
但是初始化源文件(.cpp,.cxx)中的静态成员:
#include "Coeffs.h"
double Coeffs::alpha5b = 0.0;
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句