命名空间的乐趣 [英] Namespace Fun

查看:71
本文介绍了命名空间的乐趣的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,是的,这是一个大学项目,不,我不希望您为我编写程序.这是一个使用SDL库编写游戏的C ++项目.

我对名称空间有疑问.对于项目,我需要将我的逻辑单元与屏幕上显示它们的部分分开.我正在使用名称空间逻辑和可视化将它们分开.现在,我需要逻辑命名空间中的Player类成为可视命名空间中Player类的基类.

visual :: Player.h类如下所示:

First off, yes this is a university project, no I don''t want you to write the program for me. This is a C++ project to write a game with the SDL libarary.

I have a problem with namespaces. for the project I need to separate my logical units from the part where I show them on the screen. I''m using the namespaces logic and visual to separate them. Now I need the Player class from the logic namespace to be the base class of the Player class in the visual namespace.

The visual::Player.h class looks like this:

#include "../logic/Player.h"

#ifndef PLAYER_H_
#define PLAYER_H_

namespace visual {

/**
 * This class represents a player in the game. This part of the class will
 * put the player score and remaining lives on the screen
 */
class Player : public logic::Player {
public:
	/**
	 * constructor for the player class
	 */
	Player();

	/**
	 * destructor for the player class
	 */
	virtual ~Player();


};

}
#endif /* PLAYER_H_ */



而visual :: Player.cpp文件看起来像这样:



And the visual::Player.cpp file looks like this:

#include "Player.h"

namespace visual {

Player::Player() {
	// TODO Auto-generated constructor stub

}
Player::~Player() {
	// TODO Auto-generated destructor stub
}

}



现在,我在.cpp文件中的行上出现错误:
播放器:: Player()
这行有多个标记
-ISO C ++禁止在没有
的情况下声明播放器" 类型
-尚未声明玩家"

播放器::〜Player()
在':: token



Now I''m getting errors on the lines in the .cpp file:
Player::Player()
Multiple markers at this line
- ISO C++ forbids declaration of ‘Player’ with no
type
- ‘Player’ has not been declared

Player::~Player()
expected constructor, destructor, or type conversion before ‘::’ token

推荐答案

我将您的代码分解为最小的组件,因此:
I broke your code down to its smallest component thus:
namespace logic {
    class Player {
    public:
        Player() {}
        virtual ~Player(){}
    };
}
namespace visual {
    class Player : public logic::Player {
    public:
        Player();
        virtual ~Player();
    };
}
using namespace visual;
Player::Player() {
    // TODO Auto-generated constructor stub
}
Player::~Player() {
    // TODO Auto-generated destructor stub
}


上面的代码在Visual C ++ 2010中编译时没有错误.


The above compiles without error in Visual C++ 2010.


您编写的内容看起来很有效,我可以在许多不同的编译器上得到可比的编译结果.但是,当我看到来自logic::Player的派生词时,我的好味觉传感器出现问题".没有看到其中的代码很难说,但是如果删除基类,问题是否就消失了?如果问题出在比您向我们展示的问题还要深的地方.

干杯,

Ash
What you''ve written looks valid and I can get something comparable compiling on a whole bunch of different compilers. However my good taste sensor when "bleugh" when I saw the derivation from logic::Player. Without seeing the code in there it''s hard to tell but does the problem go away if you remove the base class? If it does the problems somewhere a bit deeper than what you''ve shown us.

Cheers,

Ash


我认为您的.cpp文件中的namespace visual声明应替换为using namespace visual;语句.
I think your namespace visual declaration in your .cpp file should be replaced by a using namespace visual; statement.


这篇关于命名空间的乐趣的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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