这是我的示例代码:
#include <iostream>
#include <string>
using namespace std;
class MyClass
{
string figName;
public:
MyClass(const string& s)
{
figName = s;
}
const string& getName() const
{
return figName;
}
};
ostream& operator<<(ostream& ausgabe, const MyClass& f)
{
ausgabe << f.getName();
return ausgabe;
}
int main()
{
MyClass f1("Hello");
cout << f1;
return 0;
}
如果我注释掉#include <string>
,编译器不会报错,我猜这是因为它通过#include <iostream>
被包含了。如果在Microsoft VS中右键单击->转到定义,它们两个都指向xstring
文件中的同一行:
typedef basic_string<char, char_traits<char>, allocator<char> >
string;
但是当我运行我的程序时,我遇到了一个异常错误:
0x77846B6E (ntdll.dll) 在 OperatorString.exe 中:0xC00000FD:堆栈溢出(参数:0x00000001,0x01202FC4)
如果将#include <string>
注释掉,为什么会出现运行时错误?我正在使用VS 2013 Express。
cl /EHsc main.cpp /Fetest.exe
,则会出现警告信息...\main.cpp(23) : warning C4717: 'operator<<': recursive on all control paths, function will cause runtime stack overflow
。 - CroCo