为什么这个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''
答案 0 :(得分:5)
您需要将NumberClass
定义放在首次实例化之前,即main
之前。
class NumbersClass
{
public:
NumbersClass() {}
void setNumbers(int i) { }
};
int main (){
NumbersClass num;
num.setNumbers(1);
}