你的主要是在一分鐘有點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