2
我想设置在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;
}
,但每次我编译得到这个:
[email protected]:~/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
我看了一些其他类似的问题,并找不到任何有效的工作。我试着添加一个构造函数并创建一个对象,但是我仍然得到相同的错误。 任何人都知道该怎么办?
[OT]:您的头文件中有一个拼写错误:'#define GOEFFS_H',它应该是'COEFFS_H' – Jarod42