Question

I am trying to set a static variable in Coeffs.cpp:

#include "Coeffs.h"
class Coeffs
{
    public:
    double Coeffs::alpha5b = 0.0;
};

with the header file

#ifndef COEFFS_H
#define GOEFFS_H
class Coeffs
{
    public:
    static double alpha5b;
};
#endif

with the following code:

#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;
}

but everytime I compile I get this:

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

Ive looked through some of the other similar questions and cant find anything that works. I've tried adding a constructor and making an object, but then I still get the same error. Anyone know what to do?

Was it helpful?

Solution

Class declaration should be placed in the header (Coeffs.h)

#ifndef COEFFS_H
#define COEFFS_H
class Coeffs
{
    public:
    static double alpha5b;
};
#endif

but initialization of static member in a source file (.cpp,.cxx):

#include "Coeffs.h"

double Coeffs::alpha5b = 0.0;

OTHER TIPS

in your Coeffs.cpp:

#include "Coeffs.h"

double Coeffs::alpha5b = 0.0;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top