我是C++的初學者,我嘗試通過創建一個對象並通過點運算符訪問它來訪問構造方法。在這個過程中,我得到了錯誤,這很正常嗎?我只是想試驗。如果有辦法做同樣的事情,請讓我知道程序,我已經Google搜索,但找不到任何解決方案。下面是代碼。使用點運算符的訪問構造函數
#include <iostream>
using namespace std;
class box
{
public:
box(double);
private:
double width;
};
box::box(double w)
{
cout<<"\n I'm inside the constructor ";
width=w;
}
box::~box()
{
cout<<"\n I'm inside the desstructor ";
}
int main()
{
box box1;
box1.box(10);
}
我想experiemnt。爲什麼向下票呢? –
另請注意:a)你的類沒有默認的構造函數,它在'box box1;'line; b)你的析構函數是隱式聲明的,但你試圖在全局範圍內重新定義它。 – soon
@soon我知道,我只是想看看我們是否可以調用構造函數並初始化類的成員變量。 –