我需要有两个具有相同名称的备用类,我可以通过简单地更改main中包含哪个类来相互切换.
例如;
Mode_1.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
Run Code Online (Sandbox Code Playgroud)
Mode_2.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
Run Code Online (Sandbox Code Playgroud)
main.cpp中
#include "Mode_1.h"
int main(){
Draw D;
int x = 2;
char y = 'x';
D.Render(x, y);
}
Run Code Online (Sandbox Code Playgroud)
目前我不得不注释掉我没有使用的.h和.cpp文件来避免"先在此处定义"错误.我想要的是,我需要做的就是改变它们
#include "Mode_1.h"
Run Code Online (Sandbox Code Playgroud)
至
#include "Mode_2.h"
Run Code Online (Sandbox Code Playgroud)
您应该将它们放在不同的名称空间中:
namespace Mode2
{
class Draw{
private:
// private stuff
public:
Draw(int x, char y);
};
}
Run Code Online (Sandbox Code Playgroud)
在main中,您可以选择要使用的命名空间:
#include "Mode_1.h"
#include "Mode_2.h"
using namespace Mode2;
int main()
{
Draw D;
int x = 2;
char y = 'x';
D.Draw(x, y);
return 0;
}
Run Code Online (Sandbox Code Playgroud)