Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/124.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
默认构造函数不被称为c++;哎呀 我正在用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; } };_C++_Oop_G++_Default Constructor - Fatal编程技术网

默认构造函数不被称为c++;哎呀 我正在用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; } };

默认构造函数不被称为c++;哎呀 我正在用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; } };,c++,oop,g++,default-constructor,C++,Oop,G++,Default Constructor,它给出了输出: 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 e

它给出了输出:

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;
test.cpp:在函数“int main()”中:
test.cpp:233:26:警告:“vector3 vec1()”的地址将始终计算为“true”[-Waddress]
库特
在这里声明一个函数,并显示一个函数指针。使用:

vector3 vec1;

使用警告编译。如何使用g++?在编译时添加适当的选项(例如,
-Wall
)。@0x499602D2,据我所知,MVP是
A(B())
。类似,两个都有相同的问题,但我认为这是MVP定义的要点。@user3018144,因为函数指针可以隐式转换为
bool
。好的,谢谢。这就解决了问题。你知道为什么它不能有
()
s吗?因为函数声明会使它模棱两可:)嗯,好的,是的,我想它会的。我没有考虑到这一点,因为我倾向于在主循环之外编写所有函数。谢谢,我试过了,但它说我必须等8分钟,大约4分钟前编辑:好了:)
vector3 vec1();
vector3 vec1;