【发布时间】:2012-05-08 08:14:00
【问题描述】:
我在重载运算符以与我的地图中的映射值一起使用时遇到问题:
map<string,abs*> _map;
// that my declaration, and I have filled it with keys/values
这两个我都试过了:
std::ostream& operator<<(std::ostream& os, abs*& ab)
{
std::cout << 12345 << std::endl;
}
std::ostream& operator<<(std::ostream& os, abs* ab)
{
std::cout << 12345 << std::endl;
}
在我的程序中,我只是调用:
std::cout << _map["key"] << std::endl;
// trying to call overloaded operator for mapped value
// instead it always prints the address of the mapped value to screen
我也试过了:
std::cout << *_map["key"] << std::endl;
// trying to call overloaded operator for mapped value
// And that gives me a really long compile time error
有谁知道我可以改变什么来让它输出映射值的值,而不是地址?
感谢任何帮助
【问题讨论】:
-
你能发布一个完整的小样本吗?
-
您发布的代码可以正常工作。问题在于您没有发布的代码。请将您的实际程序简化为演示问题的最小完整示例。您很可能会自己发现问题。如果没有,请在您的问题中发布完整的小示例。见sscce.org。
-
附言。 works for me.
-
你没有从你的运营商那里返回一个 ostream
-
是否有任何命名空间在起作用?
map<string,abs*> _map;声明、operator<<声明和使用它们的代码在哪个命名空间中?
标签: c++ pointers reference map g++