问题描述
我正在尝试创建类型为 Vertex
的数组,然后初始化函数中的每个成员。 Vertex
类在构造函数中采用Vector3f:
I am trying to create an array of type Vertex
and then initialize each member in a function. The Vertex
class takes a Vector3f in the constructor:
Vertex::Vertex(const Vector3f& position) : position(position) { }
在头文件中,我声明像这样的数组:
In the header file I declare the array like this:
class Application
{
//...
private:
Vertex data[3];
//...
};
在源文件中的某个函数中,我尝试这样:
and in the source file, in a function I try this:
data[0] = Vertex(Vector3f(0, 0, 0));
data[1] = Vertex(Vector3f(0, 0, 0));
data[2] = Vertex(Vector3f(0, 0, 0));
但是当我尝试编译时出现此错误:
But when I try to compile I get this error:
/home/mert/dev/C++/C++3D/src/Application.h: In constructor ‘Application::Application()’:
/home/mert/dev/C++/C++3D/src/Application.h:31:19: error: no matching function for call to ‘Vertex::Vertex()’
Application() { }
^
我尝试将数组声明为Vertex指针,然后执行 data = new Vertex [3];
,但结果相同。我应该怎么做才能解决此问题?
I have tried declaring the array as a Vertex pointer and then doing data = new Vertex[3];
but the result was the same. What should I do to fix this?
推荐答案
Vertex类没有默认构造函数。
声明对象数组时,每个数组条目都是通过调用默认构造函数来构建的。
您可以通过添加默认构造函数或声明一个Vertex指针数组,然后在实例化该对象时调用正确的构造函数来解决此问题。
Vertex class has no default constructor.When you declare an array of objects, each array entry is built by calling the default constructor.You may fix this either by adding a default construcor or by declaring an array of Vertex pointers and then calling the correct constructor when instantiating the object.
这篇关于没有匹配函数来调用&“ constructor&”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!