简体   繁体   English

“在命名空间内的类中使用带有运算符重载的stringstream时,不匹配'运算符>>'”

[英]“no match for ‘operator>>’” while using stringstream with operator overloading in a class inside a namespace

I'm trying to overload the >> operator in a class inside a namespace, but as soon as I try to use it with a string stream it doesn't work. 我试图在命名空间内的类中重载>>运算符,但是一旦我尝试将它与字符串流一起使用它就不起作用。 Here's a distilled version of my code: 这是我的代码的提炼版本:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

namespace Foo {
    class Bar {
    public:
        string str;
        friend istream& operator >>(istream& in, Bar& t);
    };
}

inline istream& operator >>(istream& in, Foo::Bar& t) {
    in >> t.str;
    return in;
}

int main() {
    Foo::Bar foo;
    stringstream("foo") >> foo;

    cout << foo.str << endl;

    return 0;
}

and here's the error: 这是错误:

main.cpp:22:22: error: no match for ‘operator>>’ (operand types are ‘std::stringstream {aka std::__cxx11::basic_stringstream<char>}’ and ‘Foo::Bar’)

The thing is these other ways of doing it work: 事情是这些其他方式的工作:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

namespace Foo {
    class Bar {
    public:
        string str;
        friend istream& operator >>(istream& in, Foo::Bar& t) {
            in >> t.str;
            return in;
        }
    };
}



int main() {
    Foo::Bar foo;
    stringstream("foo") >> foo;

    cout << foo.str << endl;

    return 0;
}
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

class Bar {
public:
    string str;
    friend istream& operator >>(istream& in, Bar& t);
};

inline istream& operator >>(istream& in, Bar& t) {
    in >> t.str;
    return in;
}

int main() {
    Bar foo;
    stringstream("foo") >> foo;

    cout << foo.str << endl;

    return 0;
}

The thing is, I have no idea why the first way of doing it should be wrong. 问题是,我不知道为什么第一种做法应该是错误的。 I'm using the g++ compiler on linux if that helps. 如果有帮助,我在linux上使用g ++编译器。 Could someone help me understand what's going on? 有人能帮我理解发生了什么吗?

Thanks to hints from Sam Varshavchik (in the comments above), I've been able to come up with a correct version of the first version: 感谢Sam Varshavchik的提示(在上面的评论中),我已经能够提出第一个版本的正确版本:

#include <iostream>
#include <string>
#include <sstream>

namespace Foo {
    class Bar {
    public:
        std::string str;
        friend std::istream& operator >>(std::istream& in, Bar& t);
    };

    std::istream& operator >>(std::istream& in, Bar& t);
}

std::istream& Foo::operator >>(std::istream& in, Foo::Bar& t) {
    in >> t.str;
    return in;
}

using namespace std;

int main() {
    Foo::Bar foo;
    stringstream("foo") >> foo;

    cout << foo.str << endl;

    return 0;
}

The key was making sure that the operator>> function was both declared and defined in the same scope. 关键是确保在同一范围内声明和定义了operator >>函数。 I still wanted to be able to define the function outside of the namespace braces, so I had to add a declaration inside the namespace so the compiler would know that there's supposed to be that function in the namespace. 我仍然希望能够在命名空间括号之外定义函数,因此我必须在命名空间内添加一个声明,以便编译器知道在命名空间中应该有该函数。 Keeping the function definition separate allowed me to separate my code into three files, main.cpp, foo.hpp, and foo.cpp: 保持函数定义分离允许我将我的代码分成三个文件,main.cpp,foo.hpp和foo.cpp:

// main.cpp

#include <iostream>
#include <string>
#include <sstream>

#include "foo.hpp"

using namespace std;

int main() {
    Foo::Bar foo;
    stringstream("foo") >> foo;

    cout << foo.str << endl;

    return 0;
}
// foo.hpp

#ifndef FOO_HPP
#define FOO_HPP

#include <string>
#include <iostream>

namespace Foo {
    class Bar {
    public:
        std::string str;
        friend std::istream& operator >>(std::istream& in, Bar& t);
    };

    std::istream& operator >>(std::istream& in, Bar& t);
}

#endif
// foo.cpp

#include "foo.hpp"

std::istream& Foo::operator >>(std::istream& in, Foo::Bar& t) {
    in >> t.str;
    return in;
}

Anyways, thanks so much for the help! 无论如何,非常感谢你的帮助! And thanks for not hand-feeding me a solution; 谢谢你没有亲自给我一个解决方案; it's so much better to learn by figuring it out myself, even if I did get some help pointing me in the right direction. 通过自己搞清楚来学习它会好得多,即使我确实得到了一些指导我指向正确方向的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM