运算符 <<超载

发布于 2024-08-29 18:34:10 字数 791 浏览 10 评论 0 原文

//using namespace std;

using std::ifstream;

using std::ofstream;

using std::cout;

class Dog
{

    friend ostream& operator<< (ostream&, const Dog&);

    public:
        char* name;
        char* breed;
        char* gender;

        Dog();
        ~Dog();

};

我试图超载<<操作员。我也在尝试练习良好的编码。但除非我取消注释 using 命名空间 std,否则我的代码将无法编译。我不断收到此错误,但我不知道。我使用 g++ 编译器。

Dog.h:20: error: ISO C++ forbids declaration of ‘ostream’ with no type
Dog.h:20: error: ‘ostream’ is neither function nor member function; cannot be declared friend. if i add line using std::cout; then i get this error.
Dog.h:21: error: ISO C++ forbids declaration of ‘ostream’ with no type. 

有人可以告诉我超载 << 的正确方法吗?不带 out using 命名空间 std 的运算符;

//using namespace std;

using std::ifstream;

using std::ofstream;

using std::cout;

class Dog
{

    friend ostream& operator<< (ostream&, const Dog&);

    public:
        char* name;
        char* breed;
        char* gender;

        Dog();
        ~Dog();

};

im trying to overload the << operator. I'm also trying to practice good coding. But my code wont compile unless i uncomment the using namespace std. i keep getting this error and i dont know. im using g++ compiler.

Dog.h:20: error: ISO C++ forbids declaration of ‘ostream’ with no type
Dog.h:20: error: ‘ostream’ is neither function nor member function; cannot be declared friend. if i add line using std::cout; then i get this error.
Dog.h:21: error: ISO C++ forbids declaration of ‘ostream’ with no type. 

Can somebody tell me the correct way to overload the << operator with out using namespace std;

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

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

发布评论

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

评论(2

壹場煙雨 2024-09-05 18:34:10

您使用的是 using std::ofstream 而不是 using std::ostream,因此它不知道 ostream 是什么。

您还需要包含

但实际上,没有理由使用使用任何东西;您应该只使用名称空间来限定名称(特别是如果这是头文件,以避免污染其他文件的全局名称空间):

friend std::ostream& operator<< (std::ostream&, const Dog&);

You have using std::ofstream instead of using std::ostream, so it doesn't know what ostream is.

You also need to include <ostream>.

Really, though, there's no reason to use using anything; you should just qualify the names with the namespace (especially if this is a header file, to avoid polluting the global namespace of other files):

friend std::ostream& operator<< (std::ostream&, const Dog&);
与酒说心事 2024-09-05 18:34:10

using 关键字只是意味着让您可以访问某些内容,而无需为其添加名称空间前缀。换句话说,using std::ofstream; 只是说让您以 ofstream 的方式访问 std::ofstream

您似乎还需要一个 #include ;这就是为什么编译器不知道ostream是什么。将其放入,将友元声明更改为 friend std::ostream&运算符<< (std::ostream&, const Dog&);,并删除所有 using 内容,因为将 using 放在标头中是一种不好的形式,你应该没问题。

The using keyword just means to let you access something without prefixing it with its namespace. In orther words, using std::ofstream; just says to let you access std::ofstream as ofstream.

You also seem to need a #include <iostream>; that's why the compiler doesn't know what ostream is. Put that in, change the friend declaration to friend std::ostream& operator<< (std::ostream&, const Dog&);, and get rid of all the using stuff, since it's bad form to put using in a header, and you should be okay.

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