帮助关于错误

时间:2011-04-14 16:10:19

标签: c++ visual-studio

HI, 我的代码中出现此错误,我无法理解如何传递命令行参数,而我的程序的exe文件没有创建我如何编写该.exe文件的名称。

C:\ Program Files \ Microsoft Visual Studio \ MyProjects \ filehandling \ file.cpp(205):错误C2451:类型'class 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;
}

3 个答案:

答案 0 :(得分:1)

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

while(file)
{
    file>>a;

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

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

来完成此操作

答案 1 :(得分:0)

您的问题是第205行,while (file)。它应该是while (!file.eof())

答案 2 :(得分:0)

正如CariElf所指出的那样,你的文件在while循环期间不会变为NULL或0或类似的东西。另一方面,文件的“文件结束”标记在某些时候将为真。

关于你......好吧,其他问题我猜(“我无法理解如何在我的程序的exe文件未创建的情况下传递命令行参数”):

您可以通过在“配置属性” - >“调试”选项卡上的“项目” - >“属性”中更改“命令参数”来告诉VS您想传递一些命令行参数。