默认构造函数不被称为c ++ OOP

时间:2014-03-20 23:44:00

标签: c++ oop g++ default-constructor

所以我用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;

1 个答案:

答案 0 :(得分:2)

vector3 vec1();

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

vector3 vec1;