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