>’ “在命名空间内的类中使用带有运算符重载的字符串流时-6ren"> >’ “在命名空间内的类中使用带有运算符重载的字符串流时-我试图在命名空间内的类中重载 >> 运算符,但是当我尝试将它与字符串流一起使用时,它就不起作用了。这是我的代码的精简版: #include #include #include using nam-6ren">
gpt4 book ai didi

c++ - "no match for ‘operator>>’ “在命名空间内的类中使用带有运算符重载的字符串流时

转载 作者:太空狗 更新时间:2023-10-29 21:31:50 25 4
gpt4 key购买 nike

我试图在命名空间内的类中重载 >> 运算符,但是当我尝试将它与字符串流一起使用时,它就不起作用了。这是我的代码的精简版:

#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;
}

这是错误:

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

重要的是这些其他的工作方式:

#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;
}

问题是,我不知道为什么第一种方法会出错。如果有帮助,我在 linux 上使用 g++ 编译器。有人可以帮助我了解发生了什么吗?

最佳答案

感谢 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;
}

关键是要确保 operator>> 函数是在同一范围内声明和定义的。我仍然希望能够在命名空间大括号之外定义函数,所以我必须在命名空间内添加一个声明,这样编译器就会知道命名空间中应该有那个函数。保持函数定义独立允许我将我的代码分成三个文件,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;
}

无论如何,非常感谢您的帮助!并感谢您没有亲手给我一个解决方案;通过自己弄清楚来学习要好得多,即使我确实得到了一些帮助,为我指明了正确的方向。

关于c++ - "no match for ‘operator>>’ “在命名空间内的类中使用带有运算符重载的字符串流时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57067591/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com