带有glm向量的struct中没有合适的默认构造函数 [英] No appropriate default constructor available in struct with glm vectors
问题描述
在.h中:
enum collisionType {AB, BA, AoverB, AunderB};
struct Collision {
public:
collisionType type;
glm::vec2 point1;
glm::vec2 point2;
Collision(enum collisionType, glm::vec2, glm::vec2);
};
<.cpp中的
:
in .cpp:
Collision::Collision(enum collisionType collisType, glm::vec2 p1, glm::vec2 p2) : type(collisType), point1(p1), point2(p2)
{
}
使用
std::vector<Collision> collisions;
glm::vec2 point1(11.0, 12.0);
glm::vec2 point2(12.0, 13.0);
collisions.push_back(Collision(AoverB, point1, point2));
遇到错误C2512:碰撞":没有合适的默认构造函数,为什么?
Getting error C2512: 'Collision' : no appropriate default constructor available, why?
推荐答案
您可以在此处阅读 类型T
非常适合std::vector
的要求.
此处未列出default-constructible.
You can read here the requirements for a type T
to be well suited for std::vector
.
Default-constructible is not listed there.
我还尝试编译此最小代码示例,其中X没有默认构造函数,并且可以使用MSVC很好地编译:
I also tried compiling this minimal code sample, in which X doesn't have a default constructor, and it compiles fine with MSVC:
#include <vector>
struct X {
X(int a, int b) : A(a), B(b) {}
int A;
int B;
};
int main() {
std::vector<X> v;
v.push_back(X(10,20));
}
因此,问题必须出在代码的其他地方.
So, the problem must be elsewhere in your code.
无论如何,您可能想要添加一个不带参数的构造函数,以使Collission类默认可构造",并使编译器满意:
Anyway, you may want to add a constructor with no arguments to make your Collission class "default-constructible", and make the compiler happy:
struct Collision {
// Default constructor.
// Initialize data members to some init values.
Collision() {
...
}
PS 请注意,C ++中的struct
等同于class { public: ...
,因此您可以在代码中省略public:
行:这是通过使用关键字struct
来暗示的.
PS Note that struct
in C++ is equivalent to class { public: ...
, so you can omit the public:
line in your code: it's implied by the use of the keyword struct
.
这篇关于带有glm向量的struct中没有合适的默认构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!