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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?