Can't Instantiate Object in Constructor

-1

I have a C++ header file like so:

class someClass : public someBaseClass
{
public:
    someClass();
    ~someClass();

private:
    Text playText; //declare text object
};

The C++ source file for it is:

someClass::someClass() : playText("Play") //instantiate text object
{
}

someClass::~someClass()
{
}

Then I have another class which has a static member of someClass:

class anotherClass
{
public:
    anotherClass();
    ~anotherClass();
private:
    static someClass className; //declare someClass object
};

The corresponding C++ source file:

anotherClass::anotherClass()
{
}

anotherClass::~anotherClass()
{
}

someClass anotherClass::className; //must do because its static

My problem is that when I close my application I get a error message that says:

Unhandled exception at 0x6903a9e0 in Breakout.exe: 0xC0000005: Access violation reading location 0x00000054.

But strangely enough when I comment out the : playText("Play") part of my code in the someClass constructor the message goes away. I basically can't instantiate playText or else I get an error (which is only when I close the application never during run-time).

I am completely confused and can't find anything about why this happens so does anyone know why this happens and how I can fix it?

Thanks.

c++
object
static
instantiation
asked on Stack Overflow Dec 8, 2012 by ProgrammerGuy123 • edited Dec 8, 2012 by ProgrammerGuy123

1 Answer

0

class is a reserved keyword. You can't use it as an identifier here:

static someClass class;

You have to use another name for your static member variable. I'm surprised that this even compiles.

answered on Stack Overflow Dec 8, 2012 by Nikos C.

User contributions licensed under CC BY-SA 3.0