Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

MagiXSphere

Trouble with namespaces...

This topic is 5510 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m trying to create a class with a predefined object (like cout, cerr, cin etc.) and then access it in the main function which contains another object of the same class and with the exact same name. Here''s some example code I threw together:
main.cpp
-----------------------------------------
#include <iostream>
#include "CObject.h"
using namespace std;

int main() {
	CObjectH::CObject cobject(); //Local object

	//Prints the order in which the objects were created
	cout << "Pre-defined cobject: " << CObjectH::cobject.GetNum() << endl << endl;
	cout << "Local cobject: " << cobject.GetNum() << endl; //<-- It''s this line that generates the error.

	return 0;
}


CObject.h
-----------------------------------------
namespace CObjectH {
	#ifndef COBJECT_H
	#define COBJECT_H

	class CObject {
	public:
		CObject() { Num = cNum++; }; //Default constructor

		int GetNum();

	private:
		static unsigned long cNum;
		unsigned long Num;
	};

	extern CObject cobject;

#endif
}

CObject.cpp
-----------------------------------------------
namespace CObjectH {
	#include "CObject.h"
	using namespace CObjectH;

	int CObject::GetNum() { return Num; }

	unsigned long CObject::cNum = 0;

	CObject cobject(); //Pre-defined object
}
 
I get this error trying to compile this with VC6: \main.cpp(10) : error C2228: left of ''.GetNum'' must have class/struct/union type I don''t get what it is I''m doing wrong so I''m hoping that someone here can tell me. Also why is it that I in CObject.cpp need to include "using namespace std;" to avoid writing CObjectH before every variable and function but I don''t (and can''t or I''ll get a whole bunch of errors) in CObject.h. Appreciate your help.

Share this post


Link to post
Share on other sites
Advertisement
* The way you placed your includes, you have two nested namespace CObjectH. When included from main.cpp, CObject.h creates a ::CObjectH namespace, but when included from within ::CObjectH in CObject.cpp, it creates a ::CObjectH::CObjectH namespace. The way to fix this should be obvious.

* You don''t have to use fully qualified names when you are within the namespace itself.

* using namespace std; imports the std namespace into the global namespace. using namespace CObjectH; would do the same for your namespace.

* CObject cobject(); is a function declaration - a function taking no parameters and returning a CObject - not an object definition.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
An interesting note is that GCC 3.2 allows this:

struct Foo {
void bar();
}

namespace Foo {
void bar() {
//do something
}
}

I.e. you won''t have to type this to define a member function:

void Foo::bar() {
//do something
}

Well, unfortunately this is AFAIK non-standard behaviour.. At least it doesn''t work in VS.NET 7.1

Share this post


Link to post
Share on other sites
Two rules that will help keep your namespaces out of trouble:

1. Never put "#include ..." inside a namespace.
2. Never put "using namespace ...;" in a header file.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!