Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ I';我是c+的新手+;,发生意外错误,而<&书信电报;运算符重载。你能告诉我一些建议吗?_C++_Operator Overloading - Fatal编程技术网

C++ I';我是c+的新手+;,发生意外错误,而<&书信电报;运算符重载。你能告诉我一些建议吗?

C++ I';我是c+的新手+;,发生意外错误,而<&书信电报;运算符重载。你能告诉我一些建议吗?,c++,operator-overloading,C++,Operator Overloading,当我自己做一些代码练习时,我犯了一些错误 student.h文件 #pragma once class Student { public: Student() = default; Student(int id, const char* name, int score); Student(const Student& s); Student(Student&& other); Student& operator=

当我自己做一些代码练习时,我犯了一些错误

student.h文件

#pragma once
class Student {

public:
    Student() = default;
    Student(int id, const char* name, int score);
    Student(const Student& s);
    
    Student(Student&& other);
    Student& operator=(Student&& other);
    virtual ~Student();

    **friend std::ostream & operator<<(std::ostream & os, const Student & rhs);**

    void print();
private:
    int mId;
    char* mName;
    int mScore;
    size_t mSize;
};
#include "student.h"
#include <iostream>
#include <string>


... //bunch of constructors, override...

    std::ostream & operator<<(std::ostream& os, const Student & rhs)
    {
        os << rhs.mId << rhs.mName << rhs.mScore ; // Compile Error: Cannot Access Student Member(rhs.mId, rhs.mName...)
    
        return os;
    }
那么,为什么include语句序列很重要呢?我想#include语句只是复制粘贴到我的cpp文件中

当我向student.h声明
#include
时,它也编译成功

所以当我包含一些#include things时,最好的情况是将其声明到头文件中

你能给我一些建议吗


我在Visual Studio 2019中编译了我的程序.< /p>

C++,在使用之前必须声明任何东西。 所以你的问题是你没有声明std::ostream,但是你在student.h中使用了它

一个合适的解决方案是在student.h开头的
#包含
,在
#pragma once
下面

您也可以将它们放在
类Stduent
之前:

namespace std
{
  class ostream;
}

这称为向前声明。C++中的

在使用之前必须声明任何东西。< /P> 所以你的问题是你没有声明std::ostream,但是你在student.h中使用了它

一个合适的解决方案是在student.h开头的
#包含
,在
#pragma once
下面

您也可以将它们放在
类Stduent
之前:

namespace std
{
  class ostream;
}

这称为转发声明。

在student.h中添加一个包含:

#pragma一次
#包括
班上的学生。。。
这将确保std::ostream符号的正确分辨率,并将定义的ostream运算符识别为声明为友元的运算符


iosfwd头包含iostream头中符号的前向声明,它正是为这种情况定义的。

在student.h中添加一个include for:

#pragma一次
#包括
班上的学生。。。
这将确保std::ostream符号的正确分辨率,并将定义的ostream运算符识别为声明为友元的运算符


iosfwd头包含iostream头中符号的前向声明,它正是针对这种情况定义的。

确切的错误是什么?头文件包含在需要它们的地方。在头文件中,您使用的是
std::ostream
,因此需要包含其头文件,以使编译器能够看到它。您应该在问题中包含完整的错误消息。代码中的问题是
Student.h
需要声明
std::ostream
,但它没有声明。我预计会出现不同的错误,但这可以解释为什么更改包含修复程序的顺序会出现错误C2039:“ostream”:不是“std”的成员,错误C2248:“Student::mId”:无法访问在类“Student”中声明的私有成员感谢您的评论确切错误是什么?头文件包含在需要它们的地方。在头文件中,您使用的是
std::ostream
,因此需要包含其头文件,以使编译器能够看到它。您应该在问题中包含完整的错误消息。代码中的问题是
Student.h
需要声明
std::ostream
,但它没有声明。我预计会出现另一个错误,但这可以解释为什么更改包含修复的顺序我得到了错误C2039:“ostream”:不是“std”的成员,错误C2248:“Student::mId”:无法访问在类“Student”中声明的私有成员谢谢您的评论尽管您的想法是正确的,但我们有
iosfwd
(其他答案).这在原则上是可行的。在iostreams的情况下,ostream实际上是std::basic_ostream上的typedef。这里可能也有一个分配器类型-因此转发声明是非常重要的。感谢它帮助了很多人。虽然您的想法是正确的,但我们有
iosfwd
(其他答案)。这在原则上是有效的。在iostreams的情况下,ostream实际上是std::basic_ostream上的typedef。这里可能也有一个分配器类型,所以转发声明是非常重要的。谢谢它帮了很多忙谢谢你的回答谢谢你的回答
#pragma once

#include <iosfwd>

class Student ...