你的主要是在一分钟有点won。。
int main(){
classname(20, 20) object; // You are incorrectly calling a constructor which does not exist
object.classfunction2(50, 50); // more like correct behavior.
}
您已经定义没有任何成员变量的类,所以也没有store
任何数据。它只有两个功能。所以这意味着你可以使用编译器为每个类定义的“默认构造函数”(如果你愿意,你可以提供你自己的)。
int main(){
classname object; // Call the default constructor
object.classfunction1(10, 20); // Call the functions you want.
object.classfunction2(50, 50);
}
如果您想提供一个构造函数,你应该这样做:
class classname{
public:
classname(int variable1, int variable2):
member1(variable1), member2(variable2){}; //note that there is no return type
void classfunction1(); //because instead of taking parameters it uses member1 & 2
void classfunction2(int, int);
private:
int member1;
int member2;
};
,您主要会再看看这样的:
int main(){
classname object(10, 20); // Call the default constructor. Note that the (10, 20) is AFTER "object".
object.classfunction1(); // Call the function... it will use 10 and 20.
object.classfunction2(50, 50); //This function will use 50, 50 and then call classfunction1 which will use 10 and 20.
}
几件事情要注意:该方法你试图调用第一个构造函数是错误的,你需要变量名后面的参数。 请参阅下面的注释以了解另一件需要注意的事项。
我很难理解你想要达到的目标,你的伪代码没有任何意义,对不起。你在寻找如何编写构造函数吗? – Mat 2012-03-30 12:23:01