简体   繁体   English

默认构造函数未称为C ++ OOP

[英]Default constructor not being called c++ OOP

So I'm making a program in c++ to handle vectors, and it's mostly there, but I just wanted to test it, so I have this: 所以我正在用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;
    }
};

(there's more stuff, things for << etc) (还有更多的东西,例如<<等)

and as main() I have: 作为main()我有:

int main() {

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

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

And it gives the output: 它给出了输出:

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

But shouldn't they give the same output? 但是他们不应该给出相同的输出吗? Am I missing something really obvious? 我是否真的缺少明显的东西?

Edit: There's a warning when compiling that says: 编辑:编译时有一条警告说:

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();

You're declaring a function here, and displaying a function pointer. 您在这里声明一个函数,并显示一个函数指针。 Use: 采用:

vector3 vec1;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM