头文件中的c++编译错误
C++ compilation errors in header
提醒一下,这是学校的作业,但不计分。
我提供了这4个源代码文件,但头似乎阻碍了我与字符串/函数错误等。
代码块
super.h
class super
{
public:
///default constructor
super(){}
///alternate constructor
super(string i, string a, string m, string l)
{Ident = i; Alias = a; MState=m; Location=l;}
///destructor
~super(){cout<<"BOING!! Hero Disappears"<<endl;}
///mutators -- Sets
void setIdent(string i){Ident = i;}
void setAlias(string a){Alias = a;}
void setMState(string m){MState = m;}
void setLocation(string l){Location = l;}
///accessors -- Gets
string getIdent(){return Ident;}
string getAlias(){return Alias;}
string getMState(){return MState;}
string getLocation(){return Location;}
///other functionality -- declared here, defined in impl file
void display();
void changeLoc();
private:
string Ident;
string Alias;
string MState;
string Location;
};
super_impl.cpp
///first.....go find the definition of a super.
#include "super.h"
///this file contains the "guts" of
///functions too large to be in the header.
///this is apart of the definition of a SUPER
///a void function display that follows an instance of super
void super::display()
{
cout<<"The Amazing "<<Alias<<endl;
cout<<"Mild Mannered "<<Ident<<" seeks ";
cout<<MState<<endl;
cout<<"Currently found at "<<Location<<endl<<endl;
}
void super::changeLoc()
{ string stemp;
cout<<"Where is "<<Alias<<" going?"<<endl;
getline(cin,stemp);
Location = stemp;
cout<<endl;
}
super_test_1.cpp
#include <string>
#include <iostream>
using namespace std;
#include "super_impl.cpp"
int main()
{
super S1;
S1.display();
S1.setIdent("Robby Williams");
S1.setAlias("Snail");
S1.setMState("To Protect Invertebrates Everywhere");
S1.setLocation("His secret lair under the rock");
S1.display();
S1.changeLoc();
S1.display();
cout<<"we are getting ready to call S1's destructor as we leave scope"<<endl;
return 0;
}
super_test_2.cpp
#include <string>
#include <iostream>
using namespace std;
#include "super_impl.cpp"
int main()
{
///sptr is a location that can contain
/// ONE thing...the address of a super
super * sptr;
///we have an address..but no super..sooooo...
///using the alternate constructor
sptr = new super;
sptr ->setAlias("temp");
sptr ->display();
cout<<"before delete: "<<&sptr<<endl;
///the next line calls the destructor of the thing that
///sptr is pointing at...sptr is NOT deleted..how weird.
delete sptr;
cout<<"After delete: "<<&sptr<<endl;
sptr = new super("Brian Ye","Proxy", "Save People", "no one knows");
///NOW we have a NEW super because sptr STILL EXISTS
sptr->display();
sptr->setIdent("Robby Williams");
sptr->setAlias("Snail");
sptr->setMState("To Protect Invertebrates Everywhere");
sptr->setLocation("His secret lair under the rock");
sptr->display();
return 0;
}
编译错误||=== Build: Debug in Simple Super Person Object (compiler: GNU GCC Compiler) ===|
Z:CS250super.h|7|error: expected ')' before 'i'|
Z:CS250super.h|13|error: 'string' has not been declared|
Z:CS250super.h|14|error: 'string' has not been declared|
Z:CS250super.h|15|error: 'string' has not been declared|
Z:CS250super.h|16|error: 'string' has not been declared|
Z:CS250super.h|20|error: 'string' does not name a type|
Z:CS250super.h|21|error: 'string' does not name a type|
Z:CS250super.h|22|error: 'string' does not name a type|
Z:CS250super.h|23|error: 'string' does not name a type|
Z:CS250super.h|30|error: 'string' does not name a type|
Z:CS250super.h|31|error: 'string' does not name a type|
Z:CS250super.h|32|error: 'string' does not name a type|
Z:CS250super.h|33|error: 'string' does not name a type|
Z:CS250super.h||In destructor 'super::~super()':|
Z:CS250super.h|10|error: 'cout' was not declared in this scope|
Z:CS250super.h|10|error: 'endl' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setIdent(int)':|
Z:CS250super.h|13|error: 'Ident' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setAlias(int)':|
Z:CS250super.h|14|error: 'Alias' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setMState(int)':|
Z:CS250super.h|15|error: 'MState' was not declared in this scope|
Z:CS250super.h||In member function 'void super::setLocation(int)':|
Z:CS250super.h|16|error: 'Location' was not declared in this scope|
Z:CS250super_impl.cpp||In member function 'void super::display()':|
Z:CS250super_impl.cpp|11|error: 'cout' was not declared in this scope|
Z:CS250super_impl.cpp|11|error: 'Alias' was not declared in this scope|
Z:CS250super_impl.cpp|11|error: 'endl' was not declared in this scope|
Z:CS250super_impl.cpp|12|error: 'Ident' was not declared in this scope|
Z:CS250super_impl.cpp|13|error: 'MState' was not declared in this scope|
Z:CS250super_impl.cpp|14|error: 'Location' was not declared in this scope|
Z:CS250super_impl.cpp||In member function 'void super::changeLoc()':|
Z:CS250super_impl.cpp|18|error: 'string' was not declared in this scope|
Z:CS250super_impl.cpp|18|error: expected ';' before 'stemp'|
Z:CS250super_impl.cpp|19|error: 'cout' was not declared in this scope|
Z:CS250super_impl.cpp|19|error: 'Alias' was not declared in this scope|
Z:CS250super_impl.cpp|19|error: 'endl' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'cin' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'stemp' was not declared in this scope|
Z:CS250super_impl.cpp|20|error: 'getline' was not declared in this scope|
Z:CS250super_impl.cpp|21|error: 'Location' was not declared in this scope|
||=== Build failed: 34 error(s), 0 warning(s) (0 minute(s), 0 second(s)) ===|
In super.h
:
你应该用std::
作为string
出现的前缀。cout
和endl
也是一样,它们也来自标准库。您应该在super.h
中包含相关的标题。
您还可以添加标题保护,以防止多个包含。
In super_test_1.cpp
:
您希望包含super.h
而不是super_impl.cpp
。为什么?因为如果多次包含super_impl.cpp
,同一函数的多个定义可能会导致问题。
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?