没有匹配函数来调用"构造函数"

时间:2014-10-15 01:09:14

标签: c++ arrays

我正在尝试创建一个类型为Vertex的数组,然后初始化函数中的每个成员。 Vertex类在构造函数中使用Vector3f:

Vertex::Vertex(const Vector3f& position) : position(position) { }

在头文件中,我声明了这样的数组:

class Application
{
    //...
    private:
        Vertex data[3];
    //...
};

并在源文件中,在函数中我尝试这个:

data[0] = Vertex(Vector3f(0, 0, 0));
data[1] = Vertex(Vector3f(0, 0, 0));
data[2] = Vertex(Vector3f(0, 0, 0));

但是当我尝试编译时,我得到了这个错误:

/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() { }
                   ^

我尝试将数组声明为顶点指针,然后执行data = new Vertex[3];,但结果是一样的。我该怎么做才能解决这个问题?

1 个答案:

答案 0 :(得分:1)

Vertex类没有默认构造函数。 声明对象数组时,每个数组条目都是通过调用默认构造函数构建的。 你可以通过添加一个默认的construcor或通过声明一个Vertex指针数组然后在实例化对象时调用正确的构造函数来解决这个问题。

相关问题