问题描述:最近遇到了两个小小的bug,用一个小例子记录下
bug1:一个b.cpp文件调用另一个a.cpp的函数,许多博客都说b.cpp只要包括a.h即可,但在编译的时候出现找不到函数的问题
解决1:在b.cpp下面包括a.cpp,#include <a.cpp>,即可解决。
后来探究了以下,猜测cmakelist写的不到位
解决2:add_executable(b b.cpp)改成add_executable(b b.cpp a.cpp)
bug2:结构体作为函数返回值
错误写法
struct student{
string name;
double score;
};
student set(){
student s1;
s1.name="lucky";
s1.score=100;
return s1;
}
正确写法,应该再加个struct
struct student{
string name;
double score;
};
struct student set(){
student s1;
s1.name="lucky";
s1.score=100;
return s1;
}