Mr_Visionary 2020-05-25 14:57 采纳率: 66.7%
浏览 128
已采纳

一点关于c++输入输出与重载的问题

#include
#include
#include
using namespace std;
struct student
{
int no;
string name;
};
class studentarray
{
private:
student* p;
ifstream infile;
int count = 0;
student st;
int max_num = 3;
public:
studentarray(){}
friend istream& operator >>(istream& in, student& st);
friend ostream& operator <<(ostream& out, student& st);
studentarray(ifstream& in)
{
p = new student[max_num];
in >> st;
while (!in.eof())
{
p[count] = st;
count++;
in >> st;
if (count == max_num)
{
max_num = 2;
student
q = new student[max_num];
for (int i = 0; i < count; i++) q[i] = p[i];
delete[]p;
p = q;
}
}
}
~studentarray()
{
ofstream outfile("d:\student.bat");
int i = 0;
while (i < count)
{
i++;
outfile << p[i];
}
outfile.close();
}

};
istream& operator >>(ifstream& in, student& st)
{
in >> st.no >> st.name;
return in;
}
ostream& operator <<(ostream& out, student& st)
{
out << st.no << " " << st.name << endl;
return out;
}
int main()
{
student st;
ifstream infile("d:\student.bat");
if (infile.fail())
{
cout << "请输入学生的学号与姓名,并用EOF结束输入" << endl;
ofstream outfile("d:\student.bat");
while ((cin >> st) != EOF)
{
outfile << st;
cin >> st;
}
cout << "您的数据已成功写入d:\student.dat,请重新打开本程序" << endl;
outfile.close();
infile.close();
}
else
{
studentarray array1(infile);
cout << "已成功读取数据文件!" << endl;
}
}

代码如上
编译器提示: 二进制">>"没有找到接受“student”类型的右操作数的运算符(或没有可接受的转换)
二进制“<<”: 没有找到接受“student”类型的右操作数的运算符(或没有可接受的转换)

诸如此类的提示有七八个,请问我的代码哪里有问题?图片说明


  • 写回答

1条回答

  • wyg_031113 2020-05-25 15:49
    关注

    //问题有点多,看看。 fstream istream不能通用。 cin是istream.

    #include <iostream>
    #include <fstream>
    using namespace std;
    struct student
    {
        int no;
        string name;
    };
    istream &operator>>(istream &in, student &st)
    {
        in >> st.no >> st.name;
        return in;
    }
    ostream &operator<<(ostream &out, student &st)
    {
        out << st.no << " " << st.name << endl;
        return out;
    }
    
    class studentarray
    {
    private:
        student *p;
        ifstream infile;
        int count = 0;
        student st;
        int max_num = 3;
    
    public:
        studentarray() {}
        friend istream &operator>>(istream &in, student &st);
        friend ostream &operator<<(ostream &out, student &st);
        studentarray(ifstream &in)
        {
            p = new student[max_num];
            in >> st;
            while (!in.eof())
            {
                p[count] = st;
                count++;
                in >> st;
                if (count == max_num)
                {
                    max_num = 2;
                    student *q = new student[max_num];
                    for (int i = 0; i < count; i++)
                        q[i] = p[i];
                    delete[] p;
                    p = q;
                }
            }
        }
        ~studentarray()
        {
            ofstream outfile("d:\\student.bat");
            int i = 0;
            while (i < count)
            {
                i++;
                outfile << p[i];
            }
            outfile.close();
        }
    };
    
    int main()
    {
        student st;
        ifstream infile("d:\\student.bat");
        if (infile.fail())
        {
            cout << "请输入学生的学号与姓名,并用EOF结束输入" << endl;
            ofstream outfile("d:\\student.bat");
            while ((cin >> st))
            {
                outfile << st;
                cin >> st;
            }
            cout << "您的数据已成功写入d:\\student.dat,请重新打开本程序" << endl;
            outfile.close();
            infile.close();
        }
        else
        {
            studentarray array1(infile);
            cout << "已成功读取数据文件!" << endl;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)
  • ¥20 matlab yalmip kkt 双层优化问题