这里是a.h header:
#include <string>
template <typename L>
class A
{
L l;
public:
A() : l("a-text") {}
const std::string get() const { l.get(); } // <<<< Edit: missing return!
};
这是a.cpp:
#include "a.h"
#include <iostream>
class L {
const std::string v;
public:
L(const std::string& v_): v(v_) {}
const std::string get() const { return v; }
};
int main() {
L l("l-text");
std::cout << l.get().c_str() << std::endl;
A<L> a;
std::cout << a.get().c_str() << std::endl; // <<<< - this will report Segmentation fault
return 0;
}
首先std::cout
可以正常显示l-text然而第二个std::cout
将报告分段错误而不是显示a-text.
提前致谢!
打开你的警告。你缺少一个return
此处声明:
const std::string get() const { l.get(); }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)