Resolving "only static const integral data members can be initialized within a class" compilation error

36,143

Solution 1

Just start addressing the errors one by one. A lot of the errors are just cascaded from the initial errors, so it looks like there are a lot of problems when there's only a couple. Just start from the top:

1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class

You can't initialize a member in the class definition unless it's static, const, and one of the integral types. Leave the "= 5" off of the declaration of number. Then you'll need to have a definition of Tester::number outside of the class definition, like so:

int Tester::number = 5;

Problem #2:

1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'

Almost exactly what it says (missing semi-colon errors can be a bit inexact in saying where the semicolon should be) - you need a semi-colon after the definition of the Tester class.

Fix those and your compilation problems go away.

The key thing is to try and take compiler errors one at a time from the top. If you get more than about 3 of them, you can probably just ignore everything after the 3rd or so because the initial error just cause the compile to into the weeds (and if they are real errors, they'll show up again in the next compile anyway).

Solution 2

  • Error C2864: either add a const modifier to your integer, or move the initialization outside the class (as in class Tester { static int number; }; int Tester::number = 5;). The latter seems more appropriate to your case.
  • Error C2146: you're missing a semicolon after the declaration of class Tester { ... }. It should be class Tester { ... };

The other errors are probably caused by the previous error. They should fix themselves automatically when it is fixed.

As a side note, I don't think you really want the static modifier on your member. It seems more appropriate for an instance field. You still can't initialize it in-place though (this isn't C#), you have to move the initialization to the constructor. For example:

class Tester {
    int number;
    static int staticNumber; // just to show you how to use a static field

public:
    Tester() : number(5) {}
    ~Tester() {} // I suggest you remove the destructor unless you need it

    int getNumber() { return number; }
    void setNumber(int value) { number = value; }

    static int getStaticNumber() { return staticNumber; }
    static void setStaticNumber(int value) { staticNumber = value; }
};

// initialize static members *outside* the class
int Tester::staticNumber = 5;
Share:
36,143
OneShot
Author by

OneShot

Computer Science Student

Updated on July 09, 2022

Comments

  • OneShot
    OneShot almost 2 years

    The following for creating a Global Object is resulting in compilation errors.

    #include "stdafx.h" 
    #include <iostream> 
    
    using namespace System; 
    using namespace std;    
    #pragma hdrstop 
    
    class Tester;
    
    
    void input();
    
    class Tester
    {
        static int number = 5;
    
    public:
    
        Tester(){};
        ~Tester(){};
    
        void setNumber(int newNumber)
        {
            number = newNumber;
        }
    
        int getNumber()
        {
            return number;
        }
    }
    
    Tester testerObject;
    
    void main(void)
    {
        cout << "Welcome!" << endl;
    
            while(1)
            {
                input();
            }
    }
    
    void input()
    {
        int newNumber = 0;
    
        cout << "The current number is " << testerObject.getNumber();
        cout << "Change number to: ";
    
            cin >> newNumber;
    
        cout << endl;
    
        testerObject.setNumber(newNumber);
    
        cout << "The number has been changed to " << testerObject.getNumber() << endl;
    }
    

    Here are the compile errors:

    1>------ Build started: Project: test, Configuration: Debug Win32 ------
    1>Compiling...
    1>test.cpp
    1>.\test.cpp(15) : error C2864: 'Tester::number' : only static const integral data members can be initialized within a class
    1>.\test.cpp(33) : error C2146: syntax error : missing ';' before identifier 'testerObject'
    1>.\test.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    1>.\test.cpp(49) : error C2039: 'getNumber' : is not a member of 'System::Int32'
    1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
    1>.\test.cpp(55) : error C2039: 'setNumber' : is not a member of 'System::Int32'
    1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
    1>.\test.cpp(57) : error C2039: 'getNumber' : is not a member of 'System::Int32'
    1>        c:\windows\microsoft.net\framework\v2.0.50727\mscorlib.dll : see declaration of 'System::Int32'
    1>Build log was saved at "file://c:\Users\Owner\Documents\Visual Studio 2008\Projects\test\test\Debug\BuildLog.htm"
    1>test - 6 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    
    1. How do I create a Global Class Object correctly like I've attempted here.
    2. And how do I fix that "only static const integral data members can be initialized within a class"
    3. And basically how do I fix the rest of the errors so I can get this to compile?

    I like declaring Global Class Objects at file scope (I like declaring all globals at file scope) because when I have to create separate source files and do "extern" and everything it becomes extremely complicated and never works for me. Although, I do want to figure out how to do that eventually... it seems every tutorial I look at won't compile though and unless it compiles I have no idea how to recreate it!

    If I can just get this to compile...then I can successfully learn how to do this. So if someone could rewrite the above to where it literally copies & pastes into Visual C++ Express 2008 and works I will finally be able to figure out how to recreate it. I'm extremely excited on seeing the fix for this! It is just I can't get Global Objects to work right! Any other information on declaring Global Class Objects...or anything for that matter is welcome!

  • OneShot
    OneShot over 15 years
    Thank you very much! I've been programming all day...I can't believe I missed the semi-colon error...but the fix for the variables in classes was extremely helpful!
  • OneShot
    OneShot over 15 years
    Thank you very much! Your answer was very helpful! Im not too clear on why not to use static on my members though.
  • Hosam Aly
    Hosam Aly over 15 years
    You're welcome. It just seemed like this was what you wanted, but I have probably been mistaken. I was misled by the fact that your getter and setter were instance methods.
  • Michael Burr
    Michael Burr over 15 years
    I often find myself wanting to initialize non-const statics in the class declaration, too. Actually I often find myself wanting to do it for instance members as well. C# lets you do that, and I found it convenient.