Click here to Skip to main content
15,888,984 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I have a class called "Server Class". This is the ServerClass.h file:

C++
#define DEFAULT_BUFLEN 512
#ifndef SERVERCLASS_H
#define SERVERCLASS_H
#endif

class ServerClass_h
{
public:
    int CreateServer(HWND);
    int CloseServer();
    char* getcbuffer();
    int listensock();
private:
    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;
    char recvbufx[DEFAULT_BUFLEN];
    int recvbuflen;
};


And the CPP, that is a little bit large, so I will put it in a pastebin: http://pastebin.com/6wKDWXX4[^]

Then, I include the ServerClass header into my main CPP file:

C++
#include "ServerClass.h"


And then, in the right line, I add the following code:

C++
if(ServerClass::CreateServer(hwnd) == 0)
SetWindowText(hwnd,"Session created");


But the compiler complains about this, and says:

error: '::CreateServer' has not been declared

It even appears in the autocomplete, so it should exist. I don't know what's wrong.

I'm using the GNU GCC compiler, and programming in Windows.
Posted

Quote:
ServerClass::CreateServer(hwnd)
Since CreateServer is not a static member of the class, you cannot call it that way. You first to create an instance of the ServerClass and the invoke its CreateServer method.


BTW
Quote:
class ServerClass_h
{
public:
int CreateServer(HWND);
int CloseServer();
The name of the class should be instead ServerClass.

BTW2:
listensock is defined as static but it is NOT declared as static.
 
Share this answer
 
Comments
Magnus9998 22-Aug-13 9:51am    
Yeah, I had to mess so much with the code, that I ended doing stupid things without even noticing.

I modified the header


<pre lang="c++">#define DEFAULT_BUFLEN 512
#ifndef SERVERCLASS_H
#define SERVERCLASS_H
#endif

class ServerClass
{
public:
static int CreateServer(HWND);
static int CloseServer();
static char* getcbuffer();
static int listensock();
private:
static SOCKET ListenSocket;
static SOCKET ClientSocket;
static char recvbufx[DEFAULT_BUFLEN];
static int recvbuflen;
};</pre>


But I still get an error.

"error: an anonymous struct cannot have function members"

And also

"error: abstract declarator '' used as declaration"

Both errors point to the lines 7 and 18 of my ServerClass.h header.

I only want to have access to those functions from another classes.
maybe it work like this:
C++
#define DEFAULT_BUFLEN 512
#ifndef SERVERCLASS_H
#define SERVERCLASS_H
#endif
 
class ServerClass_h
{
public:
    static int CreateServer(HWND);
    static int CloseServer(HWND);
    char* getcbuffer();
    int listensock();

private:
    SOCKET ListenSocket = INVALID_SOCKET;
    SOCKET ClientSocket = INVALID_SOCKET;
    char recvbufx[DEFAULT_BUFLEN];
    int recvbuflen;
};


then you can use it like this:
C++
HWND someHwnd;

// ...

if(ServerClass::CreateServer(hwnd) == 0)
	SetWindowText(hwnd,"Session created");

// ...

if(ServerClass::CloseServer(hwnd) == 0)
	SetWindowText(hwnd,"Session closed");



or you doing something like this:

C++
class ServerClass
{
public:
	static ServerClass* CreateServer(HWND hWnd)
	{
		try
		{
			ServerClass serverClass = new ServerClass(hWnd);
			return serverClass;
		}
		catch
		{
			return NULL;
		}
	}
	BOOL CloseServer(ServerClass* serverClass)
	{
		try
		{
			if(serverClass)
			{
				delete serverClass;
				serverClass = NULL;
			}

			return TRUE;
		}
		catch
		{
			return FALSE;
		}
	}
	// ...

private:
	ServerClass(HWND);
	~ServerClass();
	// ...

}


then you could use it like this:
C++
ServerClass* m_pServerClass = NULL;

// ...

ServerClass* pServerClass = ServerClass::CreateServer(hwnd);
if(pServerClass != NULL)
{
	m_pServerClass = pServerClass;
	SetWindowText(hwnd,"Session created");
}

// ...

if(ServerClass::CloseServer(m_pServerClass) == TRUE)
	SetWindowText(hwnd,"Session closed");
 
Share this answer
 
v2
Comments
Magnus9998 22-Aug-13 10:59am    
I still keep getting errors:

"error: an anonymous struct cannot have function members"
And also
"error: abstract declarator '' used as declaration"

Both errors point to the lines 7 and 18 of my ServerClass.h header. Which means that, for some bizarre reason, it doesn't get the name of my class, but why?
Okay, I got it!

The problem was the name of the class. It must have been conflicting with something, and that caused the error of the compiler, confusing my class with a struct.

I changed the name of the class to "servman", defined the functions as static, and now I got it working "fine". I still must have a glitch somewhere since it gets stuck somewhere, but that's another story. I can now call the CreateServer function from another class.
 
Share this answer
 
Comments
C3D1 23-Aug-13 4:09am    
What's about a Namespace around your class?!
Sergey Alexandrovich Kryukov 28-May-14 12:56pm    
How you can consider it as the "answer" or "solution"? Imagine what can happen if everyone would report how one screwed up and then fixed something...
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900