为什么 Xcode 给我“重新定义控制台”错误? header 和 cpp 中的名称不应该相同吗?
代码如下:
ui.cpp:
#include "ui.h"
class Console {
public:
void run() {
puts("Hello my friend!");
}
};
ui.h:
class Console {
public:
void run();
};
主要.cpp:
#include <iostream>
#include "ui.h"
int main(int argc, const char * argv[]) {
Console c;
c.run();
return 0;
}
最佳答案
Should not be the same name in the header and cpp?
不,.cpp 文件应该有实现,而不是声明。这看起来像:
#include "ui.h"
void Console::run() {
puts("Hello my friend!");
}
请注意,您可能还应该 include guards在您的 .h 文件中以防止它们被多次包含。
关于c++ - C++ 中的类和头文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29308261/