Search code examples
c++extern

Usage of extern & inline


In my C++ code, I have a definition like that;

class A {
    int i;
public:
    void setI(int temp) {
        i = temp;
    }
};

extern A* a;

After that I have a function which fills the variable of a with some stream obtained from arguments.

inline void Func() {
    ... // Stream definition with ifstream etc.
    int k;
    stream >> k;
    a->setI(k);    // Gives segmentation fault...
}

What can be the reason? Thanks in advance...


Solution

  • Use a = new A() somewhere in your initilization code. A more better way would be to call Func(A *a) with an argument instead of using your "extern" statement.