默认构造函数没有被调用C ++面向对象(Default constructor not being

2019-10-19 15:20发布

所以,我在C ++来处理向量做节目,和它主要是有,但我只是想测试它,所以我有这样的:

class vector3 {
    protected: 
        double x,y,z;  

    public: 
        // Default 3 vector Constructor
        vector3() { 
            cout << "Default constructor called." << endl;
            x=y=z=0; 
        }
    vector3(double xin, double yin, double zin) {
        cout << "parametrised constructor called." << endl;
        x=xin;
        y=yin;
        z=zin;
    }
};

(还有更多的东西,东西<<等)

并作为main()我有:

int main() {

    vector3 vec1();
    cout << "Vector 1: " << vec1 << endl;

    vector3 vec2(0, 0, 0);
    cout << "Vector 2: " << vec2 << endl;
    return 0;
}

它给人的输出:

Vector 1: 1
Parametrised constructor called.
Vector 2: (0,0,0)
Destroying 3 vector

但是,不应该给他们同样的输出? 难道我失去了一些东西真的很明显?

编辑:有编制是说,当一个警告:

test.cpp: In function ‘int main()’:
test.cpp:233:26: warning: the address of ‘vector3 vec1()’ will always evaluate as ‘true’ [-Waddress]
  cout << "Vector 1: " << vec1 << endl;

Answer 1:

vector3 vec1();

你在这里声明一个函数,并显示一个函数指针。 采用:

vector3 vec1;


文章来源: Default constructor not being called c++ OOP