c++ - 帮助解决错误

标签 c++ visual-studio

嗨, 我在我的代码中遇到了这个错误,我无法理解如何在我的程序的 exe 未创建时传递命令行参数,我如何编写该 .exe 文件的名称。

C:\Program Files\Microsoft Visual Studio\MyProjects\filehandling\file.cpp(205):错误 C2451:“类 std::basic_fstream >”类型的条件表达式是非法的 不明确的用户定义转换

#include "iostream"
#include "cstdlib"
#include "cstdio"
#include "ctime"
#include "fstream"
#include "istream"
using namespace std;
class shapes
{
    public:
        virtual void draw()=0;
        virtual void save(fstream &out)=0;
        virtual void open(fstream &in)=0;
};
class myline : public shapes
{
private:
    int sx,sy,ex,ey,color;
public:
    myline()
    {}
    myline(int x1, int y1, int x2, int y2, int clr)
    {
        sx=x1;
        sy=y1;
        ex=x2;
        ey=y2;
        color=clr;
    }
    void draw()
    {
        cout<<"Line-draw()"<<endl;
    }

    void save(fstream &out)
    {
        out<<"L"<<"\n";
        out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
    }

    void open(fstream &in)
    {

        in>>sx>>sy>>ex>>ey>>color;
    }
};

class myrectangle: public shapes
{
private:
    int sx,sy,ex,ey,color;
public:
    myrectangle()
    {}
    myrectangle(int x1, int y1,int x2, int y2,int clr)
    {
        sx=x1;
        sy=y1;
        ex=x2;
        ey=y2;
        color=clr;
    }

    void draw()
    {
        cout<<"Rectangle-draw()"<<endl;
    }

    void save(fstream &out)
    {
        out<<"R"<<"\n";
        out<<sx<<""<<sy<<""<<ex<<""<<ey<<""<<color<<"\n";
    }
    void open(fstream &in)
    {
        in>>sx>>sy>>ex>>ey>>color;
    }
};

class mycircle: public shapes
{
private:
    int sx, sy, radius, color;

public:

    mycircle()
    {
    }

    mycircle(int x1, int y1, int r, int clr)
    {
        sx=x1;
        sy=y1;
        radius=r;
        color=clr;
    }

    void draw()
    {
        cout<<"Circle-draw()"<<endl;
    }

    void save(fstream &out)
    {
        out<<"C"<<"\n";
        out<<sx<<""<<sy<<""<<radius<<""<<color<<"\n";
    }

    void open(fstream &in)
    {
        in>>sx>>sy>>radius>>color;
    }
};

struct node
{
    void*obj;
    node*link;
};

class objarray
{
private:
    node*head;
public:
    objarray()
    {
        head= NULL;
    }

    void add(void*o)
    {
        node*temp = new node;
        temp->obj=o;
        temp->link=NULL;
        if(head==NULL)
            head=temp;
        else
        {
            node*q;
            q=head;
            while(q->link != NULL)
                q=q->link;
            q->link=temp;
        }
    }

    void*getobj(int i)
    {
        node*q;
        q=head;

        int n;
        for (n=1; n<i; n++)
        {
            q=q->link;
        }

        return(q->link);
    }

    int getcount()
    {
        int n=0;
        node*q;
        q=head;

        while(q != NULL)
        {
            q=q->link;
            n++;
        }
        return n;
    }

    ~objarray()
    {
        node *q;

        q=head;

        while(q != NULL)
        {
            head = head->link;
            delete q;
            q=head;
        }
    }
};

int main(int argc ,char*argv[])
{
    fstream file;
    char choice;
    int clmum,sx,sy,ex,ey,rad;
    shapes*ptr;
    objarray arr;

    char a[2];
    int i;

    if(argc==2)
        file.open(argv[1], ios::in|ios::out);
    while(file)
    {
        file>>a;

        if(strcmp(a,"L")==0)
        {
            myline*l = new myline();
            l->open(file);
            arr.add(l);
        }

        if(strcmp(a,"R")==0)
        {
            myrectangle *a=new myrectangle();
            a->open(file);
            arr.add(a);
        }
        if(strcmp(a,"C")==0)
        {
            mycircle*c=new mycircle();
            c->open(file);
            arr.add(c);
        }
    }

    int count = arr.getcount();
    for(i=1; i<=count; i++)
    {
        ptr=(shapes*)arr.getobj(i);
        ptr->draw();
    }

    srand((unsigned ) time(NULL));

    while(1)
    {
        cout<<endl<<"1.Line 2. Rectanle 3.Circle 4.Exit"<<endl;

        cout<<"Your Choice:";
        fflush(stdin);
        cin.get(choice);;
        clmum=rand()%16;
        sx=rand()%638;
        sy=rand()%478;
        ex=rand()%638;
        ey=rand()%478;
        rad=rand()%200;

        myline*l;
        myrectangle*a;
        mycircle*c;

        switch(choice)
        {
        case '1':
            l = new myline(sx, sy, ex,ey,clmum);
            if(l=NULL)
                exit(1);
            arr.add(l);
            cout<<"Following Line added to array"<<endl;
            cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
            break;

        case '2':

            a = new myrectangle(sx,sy,ex,ey,clmum);
            if(a==NULL)
                exit(1);

            arr.add(a);
            cout<<"Following Rectangle added to array"<<endl;
            cout<<"sx="<<sx<<"sy="<<sy<<"ex ="<<ex<<"ey ="<<ey<<"color ="<<clmum<<endl;
            break;

        case '3':

            c=new mycircle(sx,sy,rad,clmum);
            if(c==NULL);

                exit(1);

            arr.add(c);
            cout<<"Following Circle added to array"<<endl;
            cout<<"sx="<<sx<<"sy="<<sy<<"rad ="<<rad<<"color"<<clmum<<endl;
            break;
        case '4':

            if(argc==1)
            {

                cout<<"Enter File name:";
                char name[67];
                cin>>name;
                file.open(name,ios::out);
            }

            count=arr.getcount();
            file.seekp(0L,ios::beg);
            file.clear();
            for(i=1; i<=count;i++)
            {
                ptr=(shapes*) arr.getobj(i);
                ptr->save(file);
            }
            file.close();
            cout<<"Array save to file......exiting"<<endl;
            exit(1);
        }
    }
    return 0;
}

最佳答案

这是您的问题区域(至少是您已确定的问题区域):

while(file)
{
    file>>a;

您得到的应该是一个警告,而不是一个错误——这里应该使用一个转换。尽管它告诉您的内容在技术上是错误的,但它仍然通过识别错误代码帮了您一个忙。问题是您在实际执行读取操作之前 测试读取是否成功。因此,当/如果读取失败,您将在循环退出之前执行另一次循环迭代。

您想将读取和测试结合起来,以便在读取失败后立即检测到它。您可以通过将上面的两行替换为:while (file >> a) {

关于c++ - 帮助解决错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5666202/

相关文章:

在 Windows 中编译的 C++ 程序产生不同的输出

c++ - 当 define 对运算符有值(value)时,#define 如何在编程中工作?

visual-studio - Visual Studio 11 安装错误

c++ - Mac 和 Windows 区别头文件路径(/vs\)

c# - Microsoft 测试管理器中的自动化测试(测试运行的构建目录不存在或需要访问权限)

c++ - 如何使用移位寄存器操作数移位数组的元素?

c++ - 如何修复引用特定 NuGet 包交付的 dll 版本的托管 C++ 程序集 (VS2017)

sql-server - 在 SSMS 中按架构对表进行分组

c++ - 将 idl 文件导入 Visual Studio (C++)?

c++ - CLion 只显示调试器中的结果类型,而不是实际值