遇到麻烦(Windows编程) [英] Having troubles with classes (Windows programming)

查看:68
本文介绍了遇到麻烦(Windows编程)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为Server Class的类。这是ServerClass.h文件:



I have a class called "Server Class". This is the ServerClass.h file:

#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;
};





和CPP,有点大,所以我会把它放在一个pastebin:< a href =http://pastebin.com/6wKDWXX4> http://pastebin.com/6wKDWXX4 [ ^ ]



然后,我将ServerClass标头包含在我的主CPP文件中:





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:

#include "ServerClass.h"





然后,在右边,我添加以下代码:





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

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





但编译器抱怨这,并说:



呃ror:':: CreateServer'尚未声明



它甚至出现在自动完成中,因此它应该存在。我不知道出了什么问题。



我正在使用GNU GCC编译器,并在Windows中进行编程。



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.

推荐答案

Quote:

ServerClass :: CreateServer(hwnd)

ServerClass::CreateServer(hwnd)

CreateServer 不是该类的静态成员,你不能这样称呼它。首先创建 ServerClass 的实例,并调用其 CreateServer 方法。





BTW

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( );

class ServerClass_h
{
public:
int CreateServer(HWND);
int CloseServer();

类的名称应该是 ServerClass



BTW2:

listensock 已定义 static 但未声明为 static

The name of the class should be instead ServerClass.

BTW2:
listensock is defined as static but it is NOT declared as static.


也许它的工作原理如下:

maybe it work like this:
#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:

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:

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:

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");


好的,我明白了!



问题是班级的名称。它肯定与某些东西冲突,导致编译器的错误,使我的类与结构混淆。



我将类的名称改为servman ,将函数定义为静态,现在我让它工作很好。我仍然必须在某个地方出现故障,因为它卡在某个地方,但这是另一个故事。我现在可以从另一个类调用CreateServer函数。
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.


这篇关于遇到麻烦(Windows编程)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆