Hi,

I am still a newbie to C++ and ran into a strange problem - I am using a static instance variable in my class, as soon as I do so I get an

error LNK2001: unresolved external symbol error message - however this only occurs when I am trying to set it to static, not when I just leave it non-static.

I need the variable to be static and dont know how to fix the problem. I did some searches and got an idea that this might either have to do with some compiler flags that need to be changed (however I only know how to ADD flags to the command line, not how to REMOVE them in Visual Studio Espress 2008) ... or with some C libraries which require including for this problem to disappear.

Can somebody help me with this and explain in more beginners-terms what I need to do, I find this slightly overwhelming.

I am using Visual Studio Express 2008 and the CImg library

Many thanks!

Static fields in a class are really external declarations. You need to define the field outside of the class for it to exist:

#include <iostream>

class A
{
public:
    // external declaration
    static double ratio;
};

// actual definition
double A::ratio;

int main()
{
    A a;

    a.ratio = 1.76;
    std::cout << a.ratio << '\n';
}

If you remove the actual definition, building the code will give you an unresolved external object error. For static ints that are const, you can initialize them inline and that counts as a definition. But until the next C++ standard comes out, that only works with const int static fields:

#include <iostream>

class A
{
public:
    static const int Max = 50;
};

int main()
{
    A a;

    std::cout << a.Max << '\n';
}

Oh my god (sorry, so cheesy!) but you just released me from 3 days of torture ... no wonder I was not finding a solution, I thought I had a different problem.

THANK YOU.

Is this generally necessary or just for Visual Studio C++ ? I seem to remember that when using Borland and a text editor it worked without an external declaration? In fact it seems to work fine even in VS if I dont declare a CImg but a "normal" type such as a double or an int ?

But until the next C++ standard comes out, that only works with const int static fields

That's actually not entirely true. It only works with integral-data types, which means that in addition to static const int also unsigned and char types are permitted to be declared and defined in the class as static const Example:

class neFoo {
private:
    static const int a = 9; // ok!
    static const unsigned int a2 = 9; // ok!
    static const char b = 'a'; // also ok!
    static const float c = 2.0; // NOT OK! (not integral)
};

aah okay, here is the answer already. Makes sense.

That's actually not entirely true. It only works with integral-data types, which means that in addition to static const int also unsigned and char types are permitted to be declared and defined in the class as static const

It is true when you read 'int' as integer type. I had a brain fart and forgot that you guys do not interpret normal prose and need everything spelled out in standardese. ;)

It is true when you read 'int' as integer type. I had a brain fart and forgot that you guys do not interpret normal prose and need everything spelled out in standardese. ;)

Nitpicking is one of my favorite hobbies here on Daniweb ;)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.