#ifndef DARK_OBJECT_H
#define DARK_OBJECT_H

#include "DarkGDK.h"

namespace DarkObject
{
	class Object // abstract
	{
	public:
		Object();
		virtual void x() = 0;

	protected:
		int imageID, spriteID;
	};

	class Rect2D
	{
	public:
		Rect2D(int x, int y, int width, int height): x(x), y(y), width(width), height(height) {};
		
		int GetX() const {return x;}
		int GetY() const {return y;}
		int GetWidth() const {return width;}
		int GetHeight() const {return height;}

	protected: // might be needed for inheritance, e.g. to create a square
		int x, y, width, height;
	};
}

#endif // DARK_OBJECT_H
#include "DarkObject.h"

int DarkObject::Object::imageID = 1;

I get:

error C2761: 'int DarkObject::Object::imageID' : member function redeclaration not allowed

Dani AI

Generated

The compiler error is coming from trying to define a non-static member outside the class. called this out and correctly contrasted the per-instance vs class-wide options. Two practical fixes are shown below.

If imageID should be per-object (one value per instance), initialize it in the constructor (or use an in-class default if you compile with C++11 or later). Example implementation in the .cpp file:

DarkObject::Object::Object()
: imageID(1), spriteID(0)
{
}

If imageID is meant to be shared by the whole class, declare it static in the class and provide a single definition in one source file, or use an inline static (C++17+) so no out-of-class definition is needed. Example using C++17 inline static:

namespace DarkObject {
class Object {
protected:
    inline static int imageID = 1;   // class-wide, needs C++17
    int spriteID = 0;
public:
    Object() = default;
    virtual void x() = 0;
};
}

Troubleshooting notes: do not attempt to define a non-static member outside the class body — the compiler will reject it. If you choose a traditional static data member (non-inline), remember the one-definition rule: put exactly one definition in a single .cpp. If using inline static, enable C++17 support (for MSVC use the appropriate /std option). Finally, keep protected/private as needed — access specifiers do not change whether a member is static or not, only how it can be accessed by other classes (as hinted).

Recommended Answers

All 3 Replies

Well first off you are trying to get a protected object.
Second even if the object was public you are trying to get it as a static object.

So you need to (A) make the object static and then it there is only one version in all instances of the class. [sometimes used to implement singletons for example] You get one initialization (never in a function) before your main() is called.

(B) make the object public and then access it via an instance of the class (each instance is different).

(C) Make it static AND public. Then you get an initialization (obligitory) and you can access it when you like.

You need one of the following:

namespace NY
{
class X
{
   private: 
     static int Item;
   public:
     static int Number;
     int OtherItem;
};
}


int NY::X::Item=1;        // valid as static initializer
int NY::X::Number=3;   // also valid
int main()
{  
NY::X xobj;                   // create an obect of type X
xobj.OtherItem=2;      // Valid as via an instance of X
NY::X::Item=3;            // NOT valid
NY::X::Number++;      // VALID
int NY::X::Number=4;  // NOT valid (this is initialization)
}

Namespaces are just globally or locally visible scoping rules. Use them as such. They are not class/struct/union.

Your problem has nothing to do with use of namespaces.

DarkObject::Object::imageID is a non-static member of DarkObject::Object, so it would usually be initialised within a constructor of DarkObject::Object.

Your are attempting to initialise it as if it is a static member of DarkObject::Object.

Thanks for the replies.

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.