C++ &引用;与';操作员<<';在';标准:操作员<<'&引用;在Linux中编译时出错,VS不';不要出现这个错误
出于某种原因,我的代码在Visual Studio中工作,但在Linux编译器中不工作,因此在Linux中出现了一个错误C++ &引用;与';操作员<<';在';标准:操作员<<'&引用;在Linux中编译时出错,VS不';不要出现这个错误,c++,C++,出于某种原因,我的代码在Visual Studio中工作,但在Linux编译器中不工作,因此在Linux中出现了一个错误 test_main.cpp:65: error: no match for 'operator<<' in 'std::operator<<' 我的concat代码 String::ListNode * String::ListNode::concat(ListNode * L1, ListNode * L2) { return L1 == NULL
test_main.cpp:65: error: no match for 'operator<<' in 'std::operator<<'
我的concat代码
String::ListNode * String::ListNode::concat(ListNode * L1, ListNode * L2)
{
return L1 == NULL ? copy(L2): new ListNode(L1->info, concat(L1->next, L2));
}
代码测试它
String firstString("First");
String secondString("Second");
cout << "+: " << firstString + secondString << endl;
stringfirststring(“第一”);
字符串第二字符串(“第二”);
cout问题在于输出运算符:
ostream & operator << (ostream & out, String & l);
您的输出运算符在哪里声明/定义。发布一个please以获得问题的简明诊断。过载的输出运算符是什么样子的?它是否将String
参数作为常量引用(即String const&
或const String&
)?不要试图简化错误,请发布完整且未经编辑的错误输出,包括任何信息注释。Hees输出运算符和完整错误信息是100行以上的巨大原因,这是为什么它在Windows上工作的原因是MSVC实现了一个C++的扩展,允许临时绑定到非conconst参考。谢谢:5分钟后,我会接受你的答案。我很好奇,为什么visual Studio不关心这个问题,而Linux关心这个问题?还有一种方法可以在我的+操作符中解决这个问题吗?我知道原因是我在里面做了一个临时变量,这个变量正在被解构,但我不知道有什么好办法,我的教授给了我一个包含所有声明的头文件,所以如果有办法不改变
ostream & operator << (ostream & out, String & l);
ostream & operator << (ostream & out, String & l)
{
l.print(out);
return out;
}
void String::print(ostream & out)
{
for (ListNode * p = head; p != nullptr; p = p->next)
out << p->info;
}
ostream & operator << (ostream & out, String & l);
ostream & operator << (ostream & out, String const & l);
// ^^^^^
// Note use of `const` here