在vc中调试 console程序.#include<iostream.h>
#include<conio.h>void main()
{
point pt;

pt.x=1000;
pt.y=2000; pt.output();
getch();
}class point
{
public:
int x;
int y;
void output()
{
cout<<x<<endl<<y<<endl;
}
};这样会报错改成下面就对了.
#include<iostream.h>
#include<conio.h>class point
{
public:
int x;
int y;
void output()
{
cout<<x<<endl<<y<<endl;
}
};void main()
{
point pt;

pt.x=1000;
pt.y=2000; pt.output();
getch();
}
为什么?难道对main函数的先后位置顺序还有要求吗?

解决方案 »

  1.   

    main是入口函数在main中你定义了一个point的变量,point必须前置声明,否则谁知道point是个什么啥东西
      

  2.   

    #include<iostream.h>
    #include<conio.h>
    class point;
    void main()
    {
        point pt;
        
        pt.x=1000;
        pt.y=2000;    pt.output();
        getch();
    }class point
    {
    public:
        int x;
        int y;
        void output()
        {
            cout<<x<<endl<<y<<endl;
        }
    };
      

  3.   

    main函数是入口函数,main函数里用到了point类,所以你必须把
    class point
    {
    public:
        int x;
        int y;
        void output()
        {
            cout<<x<<endl<<y<<endl;
        }
    };
    放在main函数之前。
    当然,你还可以向楼上一样,先声明class point; 
    这时候main中用到   point pt;
    时就知道point是一个类了。
      

  4.   

    编译产生的错误信息会告诉你point没有定义。
      

  5.   

    要用那个东西,得那个东西有才行;第一种情况,point都还没定义就在main()中用,当然出错
      

  6.   

    class 定义要在引用该class之前的。放在main前就可以了
      

  7.   

    嘻嘻~这个问题是从C语言中集成下来的,也就是说,使用之前必须定义
    这个问题除了定义类,还有定义结构,函数都是一样。比如:#include<iostream.h>void main()
    {
       int nNum = fun();
    }
    int fun()
    {
       return 0;
    }
    上面的程序也会报错
    解决方法如下:
    #include<iostream.h>int fun();void main()
    {
       int nNum = fun();
    }
    int fun()
    {
       return 0;
    }当然如果你非要把类的定义放在下面,你的程序也可以找房抓药改为:
    7楼说的样子,正解所以,你可以看到,一般使用VC的是,对于一个类的定义会分为两个文件,一个是.h文件,一个是.cpp文件
    一般.h文件里面全部都是定义部分,实现部分在.cpp文件中,使用时直接把.h文件include了,就把定义全都包含了嘻嘻~这种问题在以后VC中使用全局函数时会时时出来,嘻嘻~多看看书,慢慢就明白了~嘻嘻