将类分成 .h 和 .cpp 文件时'Does not name a type'错误
'Does not name a type' error while separating class into a .h and .cpp file
我创建了三个简单的 c++ 文件,如下所示:rtt_hello.hpp
#ifndef RTT_HELLO_HPP
#define RTT_HELLO_HPP
#include<iostream>
class displayer
{
public:
void display();
};
#endif
然后是类实现显示器.cpp
#include <iostream>
#include "rtt_hello.hpp"
void displayer::display()
{
std::cout<<"Hello";
}
最后是主程序rtt_hello.cpp.我没有主程序,因为我想在不同的应用程序中使用该对象。
#include<iostream>
#include "rtt_hello.hpp"
displayer message1;
message1.display();
现在当我编译它时,我收到错误
sambeet@Holmes ~/NewRockPort/x86/Build/rock/rtt_test $ /home/sambeet/NewRockPort/x86/Install/rtems/4.11.0-rc3/bin/i386-rtems4.11-g++ rtt_hello.cpp displayer.cpp -Ihome/sambeet/NewRockPort/x86/Build/rock/rtt_test/
rtt_hello.cpp:5:1: error: 'message1' does not name a type
message1.display();
^
我创建了标头并包含它,那么为什么会发生此错误呢?
你不能只把随机代码放在一个文件中(在任何函数之外)。在顶层,您只能声明/定义内容。像message1.display()
这样的表达式需要成为函数的一部分。
相关文章:
- 将系数存储在头文件的数组中("does not name a type"错误)
- 当初始值设定项是基类名时'initializer does not name a non-static data member or base class'错误
- 由于"error C4430: missing type specifier - int assumed. Note: C++ does not support default-int",我现在无法编
- 如何解决"'mutex' in namespace 'std' does not name a type"?
- 在 C++ 中使用命名空间时出现"does not name a type"错误
- 已声明时"Does not name a type"错误
- 嵌套类"does not name a type"
- 这在C++ "It does not own the underlying data, and so is cheap to copy or assign"中意味着什么
- Eigen loadMarket does not compile
- 为什么当我的代码超出函数范围时,"does not name a type"出现编译器错误?
- 创建C++ Redis 模块 - "does not export RedisModule_OnLoad() symbol"
- 无法分隔标头类。我得到"does not name a type"
- 模板元编程:"does not have integral or enumeration type"
- 如何修复类函数"prototype does not match"和"cadidate is"错误
- #define 的"Declaration does not declare anything"错误
- 修复第三方代码:"error: ‘enable_if’ in namespace ‘std’ does not name a template type"
- 在 c++ 中,如果包含标头,为什么我会收到'does not a name of type'错误?
- 什么原因导致我的"‘FatalError’ in namespace ‘Foam’ does not name a type"错误?
- C++ 迭代器错误"does not refer to a value"
- 编译错误"String does not name a type"