3
我已经检查过StackOverflow来找到我的问题的解决方案,但我想我可能会丢失一些东西。我试图在一个头文件(.h)中定义一个类并在一个cpp文件(.cpp)中实现它的方法,但它不起作用。在C++中包含头文件(类定义和方法实现)
main.cpp中:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
Message.h:
#ifndef MESSAGE_H_INCLUDED
#define MESSAGE_H_INCLUDED
class Message {
public:
void display();
};
#endif // MESSAGE_H_INCLUDED
Message.cpp:
#include "Message.h"
void Message::display() {
cout << "Hello!";
}
我不明白为什么我不断收到以下错误
undefined reference to 'Message::display()'
你如何编译这个? – imreal
你的代码是正确的(除了内存泄漏)。 Message.cpp没有被链接 - 可能未被编译。 –
我正在使用CodeBlocks(GNU GCC编译器),并带有以下标志:-O2,-ansi,-Wall和-pedantic –