开发者

Help About a Error

HI, I got this Error in my code i can't understand how to pass a command line argument while the exe of my programe is not created how i write the name of that .exe file.

C:\Program Files\Microsoft Visual Studio\MyProjects\filehandling\file.cpp(205) : error C2451: conditional expression of type 'class std::basic_fstream >' is illegal Ambiguous user-defined-conversion

#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;
}


Here's your problem area (at least the area of the problem you've identified):

while(file)
{
    file>>a;

What you're getting should be a warning, not an error -- there's one conversion that should be used here. Even though what it's told you is technically wrong, it's still done you a favor by identifying buggy code. The problem is that you're testing whether the read succeeded before you actually do the read. Therefore, when/if the read fails, you'll execute another iteration of your loop before the loop exits.

You want to combine the reading and testing so you'll detect a failed read immediately after it happens. You can do this by replacing both of the lines above with: while (file >> a) {


Your problem is the line 205, while (file). It should be while (!file.eof())


As pointed out by CariElf, your file won't turn to NULL or 0 or something like that during that while loop. On the other hand, the file's "end of file" marker will be true at some point.

About your... well, other question I guess ("i can't understand how to pass a command line argument while the exe of my programe is not created"):

You can tell VS you want to pass some command line arguments by changing "Command Arguments" in Project->Properties, on the Configuration Properties->Debugging tab.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜