默认构造函数未称为C ++ OOP [英] Default constructor not being called c++ OOP

查看:106
本文介绍了默认构造函数未称为C ++ OOP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我正在用C ++编写一个程序来处理向量,并且大部分都在那儿,但是我只是想对其进行测试,所以我有这个:

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:

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)

并且作为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

但是他们不应该给出相同的输出吗?我想念的东西真的很明显吗?

But shouldn't they give the same output? Am I missing something really obvious?

编译时有一条警告说:

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;

这篇关于默认构造函数未称为C ++ OOP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆