未调用c++OOP的默认构造函数

Default constructor not being called c++ OOP

本文关键字:默认 构造函数 c++OOP 调用      更新时间:2023-10-16

所以我正在用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;
vector3 vec1();

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

vector3 vec1;