Mr_Visionary
Mr_Visionary
采纳率66.7%
2020-05-25 14:57

一点关于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 wyg_031113 11月前

    //问题有点多,看看。 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;
        }
    }
    
    点赞 1 评论 复制链接分享