错误:无法绑定 'std::ostream {aka std::basic_ostream}'左值到 'std::basic_ostream&&'
所以,我最近通过大学接触了 C++ 中的 OOP,我发现自己遇到了一些问题。 我尝试过重载 ostream 运算符 << 并发现自己有一些问题需要解决。 首先是作为 ostream& 进行重载。 operator<<(ostream& outs, const Test&);
存在一个问题,描述为 \src\Test.h:15:48: error: 'std::ostream& test::Test::operator<<(std::ostream&, const test::Test&)' 必须恰好采用一个参数 ostream& Operator<<(ostream&outs, const Test&);
,所以,就像我对 operator ==
所做的那样,删除了第二个参数。但是一旦我尝试构建以下代码,我就会收到错误: \src\main.cpp:9:10: error: Cannot bind 'std::ostream {aka std::basic_ostream
。 我读到这个错误,但无法理解它,我应该做什么,我做错了什么?
Test.h
#include <iostream>
#include <cstring>
using namespace std;
#ifndef SRC_TEST_H_BECCIO
#define SRC_TEST_H_BECCIO
namespace test {
class Test {
private:
string mStr;
public:
Test(string str);
string getString(){return this->mStr;};
ostream& operator<<(ostream& outs);
};
} /* namespace test */
#endif /* SRC_TEST_H_BECCIO */
Test.cpp
#include "Test.h"
namespace test {
Test::Test(string str) {
this->mStr=str;
}
ostream& Test::operator<<(ostream& outs){
outs << this->getString()<<endl;
return outs;
}
} /* namespace test */
main.cpp
#include <iostream>
#include "Test.h"
using namespace std;
using namespace test;
int main() {
Test t("let's hope this goes well");
cout << t;
return 0;
}
So, I have recently approched OOP in c++ through university and I found myself with a few problems.
I gave a shot at overloading the ostream operator <<
and found myself with some problems to solve.
First was that doing the overloading as ostream& operator<<(ostream& outs, const Test&);
carries a problem described as \src\Test.h:15:48: error: 'std::ostream& test::Test::operator<<(std::ostream&, const test::Test&)' must take exactly one argument ostream& operator<<(ostream& outs, const Test&);
, so, just like I did with the operator ==
removed the second argument. But once i Try to build the following code i get the error: \src\main.cpp:9:10: error: cannot bind 'std::ostream {aka std::basic_ostream<char>}' lvalue to 'std::basic_ostream<char>&&' cout << t;
.
I read this error but can't comprhend it, what should I do and what is the mistake I'm doing?
Test.h
#include <iostream>
#include <cstring>
using namespace std;
#ifndef SRC_TEST_H_BECCIO
#define SRC_TEST_H_BECCIO
namespace test {
class Test {
private:
string mStr;
public:
Test(string str);
string getString(){return this->mStr;};
ostream& operator<<(ostream& outs);
};
} /* namespace test */
#endif /* SRC_TEST_H_BECCIO */
Test.cpp
#include "Test.h"
namespace test {
Test::Test(string str) {
this->mStr=str;
}
ostream& Test::operator<<(ostream& outs){
outs << this->getString()<<endl;
return outs;
}
} /* namespace test */
main.cpp
#include <iostream>
#include "Test.h"
using namespace std;
using namespace test;
int main() {
Test t("let's hope this goes well");
cout << t;
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来参数的顺序是倒退的。请参阅 https ://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes。
尝试这个声明和类似的实现:
It looks like the order of the arguments is backwards. See https://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes.
Try this declaration, and a similar implementation: