0

为什么这个 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''
4

1 回答 1

5

您需要将NumberClass定义放在您第一次实例化它的点之前,即 before main

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

int main (){
    NumbersClass num;       
    num.setNumbers(1);          
}
于 2013-11-04T17:09:38.280 回答