打印对象图,其中有另一个对象作为键
Printing a map of object, which has another object as Key
我正在尝试打印我的 map <Object, int>
的内容。这是有问题的代码:
void Inventory::print_initial_inventory()
{
for(auto p : inventory) {
std::cout << p.first << " : " << p.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Inventory& inv)
{
outstream << inv.first << "\nNumber of parts: " << inv.second << std::endl;
return outstream;
}
我知道问题出在 p.first
?因为 std::cout
不知道 如何打印对象 ,所以我试图重载 operator<<
,但我不确定该怎么做。谁能指出我正确的方向?
EDIT 这是我再次尝试这个问题的方法。有人建议我将 key 类型传递给 operator<<
重载。这是我的代码:
void Inventory::print_initial_inventory()
{
for(auto x : inventory) {
std::cout << x.first << " : " << x.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Auto_Part& inv)
{
outstream << "Type: " << inv.type << "\nName: " << inv.name << "\nPart Number: " << inv.part_number << "\nPrice: $" << inv.price << std::endl;
return outstream;
}
我仍然收到指向 x.first
.
的 无效二进制表达式错误
std::cout
是 std::ostream
对象。对于某些基本类型 (you can check them out here),std::ostream
按标准重载了 operator<<
。如果您希望能够将 operator<<
与 class 一起使用(比方说 class MyType
),您必须自己重载该运算符。
对于像 std::ostream
这样的内置 C++ 类型,您可以在 class 之外执行此类重载(因为否则您必须修改 std::ostream
),其语法是:
std::ostream& operator<< (std::ostream& s, const MyType& arg)
{
/* enter you implementation here */
return s;
}
可以找到更多信息 here。
I know the problem is at p.first
? because std::cout
doesn't know
how to print an object
, so I tried to overload the << operator
,
but I'm not sure how to do it.
运算符重载的基础知识可以在这个post中找到:运算符重载的基本规则和习语是什么?
我强烈建议您在进一步阅读之前先阅读这篇文章
在你的案例中,我发现了两个基本问题:
- 你没有多次提到你的
Key
class。特别是,如果您不提供 operator<
,您将如何将元素插入 std::map
(Inventory
class 的成员)?由于 Key
class 是用户定义的,因此您需要提供一个。您可以在这个 SO post:std::maps with user-defined types as key 中阅读更多相关信息
- 您的代码的第二个也是主要问题是,没有为您的
Key
类型提供 operator<<
。可以按如下方式完成:
例如,假设 class Key
class Key
{
int member;
public:
Key(const int a): member(a){}
// provide operator< for your Key type
bool operator<(const Key& other)const { return this->member < other.member; }
// provide operator<< for Key class like follows
friend std::ostream& operator<<(std::ostream& out, const Key& key);
};
// implement outside the class
std::ostream& operator<<(std::ostream& out, const Key& key)
{
// simply for this case
return out << key.member;
}
现在您可以以类似的方式为 Inventory
class 提供 operator<<
。
// now in Inventory class
class Inventory
{
std::map<Key, int> inventory;
public:
Inventory(const std::map<Key, int>& m): inventory(std::move(m)) {}
// declared as friend, so that you can access the private member(map)
friend std::ostream& operator<<(std::ostream& out, const Inventory& inv);
};
// implement outside the class
std::ostream& operator<<(std::ostream& out, const Inventory& inv)
{
for(const auto& entry: inv.inventory )
out << entry.first << " Number of parts: " << entry.second << std::endl;
return out;
}
// in the main
int main()
{
Key key1{1};
Key key2{2};
std::map<Key, int> tempInvObj{{key1, 11}, {key2, 12}};
Inventory obj{tempInvObj};
std::cout << obj;
return 0;
}
输出:
1 Number of parts: 11
2 Number of parts: 12
我正在尝试打印我的 map <Object, int>
的内容。这是有问题的代码:
void Inventory::print_initial_inventory()
{
for(auto p : inventory) {
std::cout << p.first << " : " << p.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Inventory& inv)
{
outstream << inv.first << "\nNumber of parts: " << inv.second << std::endl;
return outstream;
}
我知道问题出在 p.first
?因为 std::cout
不知道 如何打印对象 ,所以我试图重载 operator<<
,但我不确定该怎么做。谁能指出我正确的方向?
EDIT 这是我再次尝试这个问题的方法。有人建议我将 key 类型传递给 operator<<
重载。这是我的代码:
void Inventory::print_initial_inventory()
{
for(auto x : inventory) {
std::cout << x.first << " : " << x.second << std::endl;
}
}
std::ostream& operator<<(std::ostream& outstream, Auto_Part& inv)
{
outstream << "Type: " << inv.type << "\nName: " << inv.name << "\nPart Number: " << inv.part_number << "\nPrice: $" << inv.price << std::endl;
return outstream;
}
我仍然收到指向 x.first
.
std::cout
是 std::ostream
对象。对于某些基本类型 (you can check them out here),std::ostream
按标准重载了 operator<<
。如果您希望能够将 operator<<
与 class 一起使用(比方说 class MyType
),您必须自己重载该运算符。
对于像 std::ostream
这样的内置 C++ 类型,您可以在 class 之外执行此类重载(因为否则您必须修改 std::ostream
),其语法是:
std::ostream& operator<< (std::ostream& s, const MyType& arg)
{
/* enter you implementation here */
return s;
}
可以找到更多信息 here。
I know the problem is at
p.first
? becausestd::cout
doesn't know how to print anobject
, so I tried to overload the<< operator
, but I'm not sure how to do it.
运算符重载的基础知识可以在这个post中找到:运算符重载的基本规则和习语是什么? 我强烈建议您在进一步阅读之前先阅读这篇文章
在你的案例中,我发现了两个基本问题:
- 你没有多次提到你的
Key
class。特别是,如果您不提供operator<
,您将如何将元素插入std::map
(Inventory
class 的成员)?由于Key
class 是用户定义的,因此您需要提供一个。您可以在这个 SO post:std::maps with user-defined types as key 中阅读更多相关信息 - 您的代码的第二个也是主要问题是,没有为您的
Key
类型提供operator<<
。可以按如下方式完成:
例如,假设 class Key
class Key
{
int member;
public:
Key(const int a): member(a){}
// provide operator< for your Key type
bool operator<(const Key& other)const { return this->member < other.member; }
// provide operator<< for Key class like follows
friend std::ostream& operator<<(std::ostream& out, const Key& key);
};
// implement outside the class
std::ostream& operator<<(std::ostream& out, const Key& key)
{
// simply for this case
return out << key.member;
}
现在您可以以类似的方式为 Inventory
class 提供 operator<<
。
// now in Inventory class
class Inventory
{
std::map<Key, int> inventory;
public:
Inventory(const std::map<Key, int>& m): inventory(std::move(m)) {}
// declared as friend, so that you can access the private member(map)
friend std::ostream& operator<<(std::ostream& out, const Inventory& inv);
};
// implement outside the class
std::ostream& operator<<(std::ostream& out, const Inventory& inv)
{
for(const auto& entry: inv.inventory )
out << entry.first << " Number of parts: " << entry.second << std::endl;
return out;
}
// in the main
int main()
{
Key key1{1};
Key key2{2};
std::map<Key, int> tempInvObj{{key1, 11}, {key2, 12}};
Inventory obj{tempInvObj};
std::cout << obj;
return 0;
}
输出:
1 Number of parts: 11
2 Number of parts: 12