错误:无法绑定 'std::ostream {aka std::basic_ostream}'左值到 'std::basic_ostream&&'

发布于 2025-01-16 23:49:33 字数 1592 浏览 3 评论 0原文

所以,我最近通过大学接触了 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}' lvalue to 'std::basic_ostream&&'计算<< t;。 我读到这个错误,但无法理解它,我应该做什么,我做错了什么?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-23 23:49:34

看起来参数的顺序是倒退的。请参阅 https ://learn.microsoft.com/en-us/cpp/standard-library/overloading-the-output-operator-for-your-own-classes
尝试这个声明和类似的实现:

friend ostream& operator<<(ostream& os, Test t);

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:

friend ostream& operator<<(ostream& os, Test t);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文