![](/img/trans.png)
[英]Error: 'argument' does not name a type, supposed problems with .cpp and .h files
[英]'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
然后該類實現displayer.cpp
#include <iostream>
#include "rtt_hello.hpp"
void displayer::display()
{
std::cout<<"Hello";
}
最后是主程序rtt_hello.cpp。我沒有main,因為我想在其他應用程序中使用該對象。
#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()
這樣的表達式需要成為函數的一部分。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.