C++ Class static methods sharing

Started by
7 comments, last by Captain P 15 years, 8 months ago
Hello I've just started to learn C++ to see what it's like, I usually use C#. The following code generates Error an identifier not found error. I think the solution is a header file, but i am not familiar with the concept or how to fix the problem. In One File

namespace MySpace
{
	
	class MyClass
	{
	public:
		static void MyMethod(something.. )
		{
		       //Logic
		}
	};

}

Somewhere in another file


MySpace::MyClass::MyMethod(params.. );


Advertisement
Your class and function are not known in that other file unless you make it known. You can do so by putting the definition in a header file (while keeping the implementation in a cpp file) and including that header wherever you need to use that class.

For example:
test.h// Definition:#ifndef TEST_H#define TEST_Hclass Test{public:    int Function();};#endif
test.cpp// Implementation:#include "test.h"int Test::Function(){    return 5;}
otherfile.cpp// Usage:#include "test.h"int main(){    Test a;    a.Function();}

Note that you can put your implementation in a header file, but that's generally avoided since changing a header means you'll have to recompile any cpp file that includes it. Only for inline functions or template classes this is done. What it comes down to is that a header file contains the 'interface' of your class. The function names and signatures. That's what other code needs to know if it wants to call your functions and use your classes.
Create-ivity - a game development blog Mouseover for more information.
You'll also want to look into include guards. The C/C++ preprocessor does make you work harder to implement what was easy in C#, but there are some benefits that come from the flexibility.
OK,

Is there any method of grouping the class contents if the class is defined in the header?

If not for any other reason than being able to collapse the code block in Visual Studio.
Quote:Original post by Agwan
OK,

Is there any method of grouping the class contents if the class is defined in the header?

If not for any other reason than being able to collapse the code block in Visual Studio.


If you're talking about something like the #region/#endregion directives from C#, in VC++ you can use #pragma region/endregion, but it might not be supported on other compilers.
No, that's now quite what I meant; I was looking for something more like the syntax for partial classes .. ish, thanks anyway.
It doesn't really matter where you put the implementation of a class - this can be spread across multiple files if you so desire. You can't, however, split the definition across multiple files.

You probably don't want to, either - if your class definitions grow that large, you're likely focusing too much responsibilities in one place.


Or did you mean something else?
Create-ivity - a game development blog Mouseover for more information.
Not Quite, I mean like this

HEADER FILE
namespace MySpace{		definetypething class MyClass	{	public:		static void MyMethod(something.. );	};}


SOURCE FILE
namespace MySpace{	definetypething class MyClass	{	public:		static void MyMethod(something.. );		{		       //Logic and actual stuff		}	};}


[Edited by - Agwan on August 18, 2008 9:15:49 PM]
The header file looks fine (except for 'definetypething'). The source file needs to be modified somewhat, though. Keep in mind that you're working with a different language now, one that uses a much older compilation system.

The following code should do the trick. For every function that you provide an implementation, you'll have to specify it's 'origin'. That is, to what class it belongs. If you wrote a class definition around it, you'd be defining that class again and that's not going to work.

// Source file: myclass.cpp#include "myclass.h"namespace MySpace{	void MyClass::MyMethod(something.. )	{	       //Logic and actual stuff	}}


Note that, for static functions, you may as well use loose functions: unlike C#, C++ does not force you to use classes.
Create-ivity - a game development blog Mouseover for more information.

This topic is closed to new replies.

Advertisement