绕过"first defined here"错误的方法?
Way around "first defined here" error?
我需要有两个同名的备用类,这样我就可以通过简单地更改main中包含的类来在彼此之间切换。
例如;
模式1.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
模式2.h
class Draw{
private:
// private stuff
public:
void Render(int x, char y);
};
main.cpp
#include "Mode_1.h"
int main(){
Draw D;
int x = 2;
char y = 'x';
D.Render(x, y);
}
目前,我不得不注释掉我没有使用的.h和.cpp文件,以避免出现"此处首次定义"错误。我想要的是,我所要做的就是在它们之间切换
#include "Mode_1.h"
至
#include "Mode_2.h"
您应该将它们放在不同的名称空间中:
namespace Mode2
{
class Draw{
private:
// private stuff
public:
Draw(int x, char y);
};
}
在主目录中,您可以选择要使用的名称空间:
#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;
}
您可以这样尝试:
#ifdef MODE1
#include "Mode_1.h"
#else
#include "Mode_2.h"
#endif
int main(){
Draw D;
int x = 2;
char y = 'x';
Draw(x, y);
}
并使用-DMODE1或无编译此源文件,具体取决于您希望包含Mode_1.h或Mode_2.h
相关文章:
- C++:正在检查LinkedList中的回文-递归方法-错误
- C++数组队列实现方法错误
- 方法错误"not all control paths return a value"和方法不返回值
- OpenGL 顶点缓冲区类重定义和模板方法错误
- 如何在 VC++ 中通过引用传递另一个对象的方法(错误 C2664)
- 复数程序编译时的计算方法错误
- CMake 构建错误 - 虚拟方法错误缺少 vtable
- 接受 lambda 作为参数的类方法 - 错误消息
- c++程序方法错误visualstudio
- OpenCV "findContours"方法错误
- std::线程类方法错误
- 基类没有新方法错误
- C++派生类继承方法错误
- 指向类方法错误c++11的指针数组
- 使用jni调用方法:错误的jni oop参数
- QVector.push_back方法错误
- 对静态方法错误的未定义引用
- Friend方法错误
- async_write方法错误
- 重写方法错误C++