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;
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 求用matlab求解上述微分方程的程序代码
  • ¥15 请问各位,如何在Jetson nano主控板的Ubuntu系统中安装PyQt5
  • ¥15 MAC安装佳能LBP2900驱动的网盘提取码
  • ¥400 微信停车小程序谁懂的来
  • ¥15 ATAC测序到底用什么peak文件做Diffbind差异分析
  • ¥15 安装ubantu过程中第一个vfat 文件挂载失败
  • ¥20 GZ::CTF如何兼容一些靶机?
  • ¥15 etcd集群部署问题
  • ¥20 谁可以帮我一下问一下各位
  • ¥15 为何重叠加权后love图的SMD与svyCreateTableOne函数绘制基线表的不一致