C ++程序编译时出错

时间:2013-11-04 17:08:41

标签: c++

为什么这个c ++程序会给我错误:

#include <iostream>
using namespace std;

int main (){
    NumbersClass num;       
    num.setNumbers(1);          
}

class NumbersClass
    {
    public:
        NumbersClass() {}           
        void setNumbers(int i) { }          
    };

以下是我的错误:

taskbcplus.cpp(7): error C2065: 'NumbersClass' : undeclared identifier
taskbcplus.cpp(7): error C2146: syntax error : missing ';' before identifier 'num'
taskbcplus.cpp(7): error C2065: 'num' : undeclared identifier
taskbcplus.cpp(9): error C2065: 'num' : undeclared identifier
taskbcplus.cpp(9): error C2228: left of '.setNumbers' must have class/struct/union
1>          type is ''unknown-type''

1 个答案:

答案 0 :(得分:5)

您需要将NumberClass定义放在首次实例化之前,即main之前。

class NumbersClass
{
public:
    NumbersClass() {}           
    void setNumbers(int i) { }          
};

int main (){
    NumbersClass num;       
    num.setNumbers(1);          
}