假设在Human.h文件中定义有一个成员函数description(),他的返回值类型是string类型。
#pragma once
class ODU {
public:
string description() const;
};
然后在Human.cpp文件中补全函数方法
#include <iostream>
#include <string>
#include "ODU.h"
using namespace std;
string ODU::description() const {
stringstream ret;
cout << "为什么description()函数会显示错误呢?" << endl;
return ret.str();
}
这时候,Human.cpp文件中description()函数会报错,这是为什么呢?
=
=
=
我猜你们肯定想不到!
我就不卖关子了~
其实,问题出现在Human.h文件中。在写description()函数声明时,并没有在文件中写上头文件和命名空间,没有加上这些,编译器并不认识 string 是什么东西,这就是导致出现报错的根本原因~~~
现在我们把头文件和命名空间加上去:
看,报错消除啦!!!
总结:
这是小编在做练习时发现的问题,花费了小编好大力气才找出问题所在,现在写出来分享给大家,希望大家看到此篇文章,以后能避开这些低级错误,早日走上编程巅峰!(编程时,一定要注意各种细节,不要马虎,不然就会像小编一样,自讨苦吃)
最后,附上修改完后的代码:
Human.h文件
#pragma once
#include <string>
using namespace std;
class ODU {
public:
string description() const;
};
Human.cpp文件
#include <iostream>
#include <sstream>
#include <string>
#include "ODU.h"
using namespace std;
string ODU::description() const {
stringstream ret;
cout << "完美,报错没有啦!!!" << endl;
return ret.str();
}