#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

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.