3

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?

1
  • [OT]: You have a typo in your header : #define GOEFFS_H, it should be COEFFS_H Commented Feb 5, 2014 at 11:30

2 Answers 2

6

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;
Sign up to request clarification or add additional context in comments.

Comments

4

in your Coeffs.cpp:

#include "Coeffs.h"

double Coeffs::alpha5b = 0.0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.