包括c++中的头文件(类定义和方法实现)
Including header files in C++ (class definition and method implementation)
我已经检查了StackOverflow以找到解决我的问题的方法,但我想我可能错过了一些东西。我试图在头文件(.h)中定义一个类,并在cpp文件(.cpp)中实现其方法,但它不起作用。
main.cpp:
#include <iostream>
#include "Message.h"
using namespace std;
int main()
{
Message *t = new (Message);
t->display();
return 0;
}
message:
#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()'
用g++ -std=c++11 Message.cpp main.cpp
相关文章:
- 在父类中为多个子类定义方法
- 在.cpp文件中定义方法而不是在 C++ 的 .h 文件中定义方法时,如何提高代码的性能?
- 对已定义方法的未定义引用
- 需要派生的类来定义方法
- 在某些名称空间中定义方法时,在全局名称空间中声明
- 在类 C++ 中重定义方法
- 这两种定义方法的样式之间是否存在差异
- 对已定义方法的未定义引用、标头中的声明和源匹配
- 如果未在类 c++ 中定义方法,如何在类对象上使用方法
- 在为嵌入式类定义方法时,有没有办法避免重复包含类
- 有关C++中一个或多个乘法定义方法的错误
- 在 cpp 文件中定义方法时未解析外部,但在标头中定义时未解析
- 是否允许所有未使用的未定义方法
- 如何在声明之外为专门用于无参数的可变模板类定义C++方法
- 什么是编程语言为派生类重新定义方法的能力
- 在C++中,在类定义内部或外部定义方法体是否更为传统
- 向std::vector或typdef添加自定义方法
- 如果在头文件中定义方法,内联关键字有什么用?
- C++ 我知道数组长度,但想知道是否是一种更简洁的数组元素定义方法
- 在类作用域内定义方法时,除非在原始 cpp 文件中的其他位置调用方法,否则项目不会编译