cout 的运算符问题

发布于 2024-08-30 21:42:32 字数 662 浏览 4 评论 0原文

我有一个简单的包类,它已重载,因此我可以简单地使用 cout << 输出包数据。包名。我还有两种数据类型,名称是字符串,运费是双精度数。

protected:
    string name;
    string address;
    double weight;
    double shippingcost;

ostream &operator<<( ostream &output, const Package &package )
{
    output << "Package Information ---------------";
    output << "Recipient: " << package.name << endl;
    output << "Shipping Cost (including any applicable fees): " << package.shippingcost;

问题出现在第 4 行(输出 <<“收件人:...)。我收到错误“无操作员“<<”匹配这些操作数”。但是,第 5 行没问题。

我猜这与数据类型是包名称的字符串有关。有什么想法吗?

I have a simple package class which is overloaded so I can output package data simply with cout << packagename. I also have two data types, name which is a string and shipping cost with a double.

protected:
    string name;
    string address;
    double weight;
    double shippingcost;

ostream &operator<<( ostream &output, const Package &package )
{
    output << "Package Information ---------------";
    output << "Recipient: " << package.name << endl;
    output << "Shipping Cost (including any applicable fees): " << package.shippingcost;

The problem is occurring with the 4th line (output << "Recipient:...). I'm receiving the error "no operator "<<" matches these operands". However, line 5 is fine.

I'm guessing this has to do with the data type being a string for the package name. Any ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

烟雨凡馨 2024-09-06 21:42:32

您必须包含错误的字符串标头。 是两个完全不同的标准标头。

#include <string.h> //or in C++ <cstring>

这适用于 C 风格的以 null 结尾的 char 数组的函数(如 strcpy、strcmp 等)。 cstring 参考

#include <string>

这是针对 std::string 的。 字符串参考

You must be including a wrong string header. <string.h> and <string> are two completely different standard headers.

#include <string.h> //or in C++ <cstring>

That's for functions of C-style null-terminated char arrays (like strcpy, strcmp etc). cstring reference

#include <string>

That's for std::string. string reference

爱要勇敢去追 2024-09-06 21:42:32

您可能缺少#include

You are likely missing #include <string>.

随遇而安 2024-09-06 21:42:32

尝试在类声明中将 operator<< 声明为 friend

struct Package
{
public:
    // Declare {external} function "operator<<" as a friend
    // to give it access to the members.
    friend std::ostream& operator<<(std::ostream&, const Package& p);

protected:
    string name;
    string address;
    double weight;
    double shippingcost;
};

std::ostream&
operator<<(std::ostream& output, const Package& package)
{
    output << "Package Information ---------------";
    output << "Recipient: " << package.name << endl;
    output << "Shipping Cost (including any applicable fees): " << package.shippingcost;
    return output;
}

顺便说一句,使用具有与数据类型的名称相同,但大小写不同。这对搜索和分析工具造成了严重破坏。此外,拼写错误也会产生一些有趣的副作用。

Try declaring operator<< as a friend in your class declaration:

struct Package
{
public:
    // Declare {external} function "operator<<" as a friend
    // to give it access to the members.
    friend std::ostream& operator<<(std::ostream&, const Package& p);

protected:
    string name;
    string address;
    double weight;
    double shippingcost;
};

std::ostream&
operator<<(std::ostream& output, const Package& package)
{
    output << "Package Information ---------------";
    output << "Recipient: " << package.name << endl;
    output << "Shipping Cost (including any applicable fees): " << package.shippingcost;
    return output;
}

By the way, it is very bad form to use variable names that have the same name as the data type, excepting different case. This wreaks havoc with search and analysis tools. Also, typos can have some fun side-effects too.

感情废物 2024-09-06 21:42:32

用它来输出字符串..

输出<< “收件人:” << package.name.c_str() <<结束;

use this to output the string..

output << "Recipient: " << package.name.c_str() << endl;

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