C++:没有匹配函数来调用 std::deque<SnakePart>::emplace_front
C++ : No matching function for call to std::deque<SnakePart>::emplace_front
最近我一直在努力重制我对经典蛇游戏的重制,这次使用双端队列而不是蛇尾的链表。我构建了一个尾段并尝试将其放置在双端队列的前面并得到了一个奇怪的错误。
no matching function for call to 'std::deque<SnakePart>::emplace_front(<brace-enclosed initializer list>)'|
因为我对 C++ 还是很陌生,所以我不知道为什么会出现这个错误。如果有人能帮我解决这个难题,我将不胜感激。
SnakePart.cpp
http://pastebin.com/verR9bpn
Snake.h
http://pastebin.com/XUyNAVKK
最小完整且可验证的示例:
#include <deque>
struct Test {
Test(int x, int y) : x(x), y(y){}
int x;
int y;
};
int main () {
int x = 4, y = 5;
std::deque<Test> tester;
tester.emplace_front({x, y});
return 0;
}
SnakePart.cpp
#include "Snake.h"
#include <deque>
void SnakePart::advance(int x, int y, bool loose_tail = true) {
parts.emplace_front({x, y});
if(loose_tail)
parts.pop_back();
}
Snake.h
#ifndef SNAKE_H_INCLUDED
#define SNAKE_H_INCLUDED
#include <deque>
class SnakeHead {
private:
int posX, posY;
// Snake head functions
void input();
void movement();
};
class SnakePart {
private:
std::deque<SnakePart> parts;
void advance(int x, int y, bool loose_tail);
};
#endif // SNAKE_H_INCLUDED
从 emplace_front
函数参数中删除大括号。 emplace*
函数的语义意味着参数像在容器类型 T
的构造函数中一样传递。用 {}
传递它使参数类型为 std::initializer_list
。
emplace_front
function takes its arguments and passes to a suitable constructor in the contained object. The Test
class doesn't have a constructor which takes a single initializer-list 参数。它有一个带有两个 int
参数的构造函数,这意味着您可以使用
tester.emplace_front(x, y);
它还有一个复制构造函数,这意味着你可以这样做
tester.emplace_front(Test{x, y});
最近我一直在努力重制我对经典蛇游戏的重制,这次使用双端队列而不是蛇尾的链表。我构建了一个尾段并尝试将其放置在双端队列的前面并得到了一个奇怪的错误。
no matching function for call to 'std::deque<SnakePart>::emplace_front(<brace-enclosed initializer list>)'|
因为我对 C++ 还是很陌生,所以我不知道为什么会出现这个错误。如果有人能帮我解决这个难题,我将不胜感激。
SnakePart.cpp
http://pastebin.com/verR9bpn
Snake.h
http://pastebin.com/XUyNAVKK
最小完整且可验证的示例:
#include <deque>
struct Test {
Test(int x, int y) : x(x), y(y){}
int x;
int y;
};
int main () {
int x = 4, y = 5;
std::deque<Test> tester;
tester.emplace_front({x, y});
return 0;
}
SnakePart.cpp
#include "Snake.h"
#include <deque>
void SnakePart::advance(int x, int y, bool loose_tail = true) {
parts.emplace_front({x, y});
if(loose_tail)
parts.pop_back();
}
Snake.h
#ifndef SNAKE_H_INCLUDED
#define SNAKE_H_INCLUDED
#include <deque>
class SnakeHead {
private:
int posX, posY;
// Snake head functions
void input();
void movement();
};
class SnakePart {
private:
std::deque<SnakePart> parts;
void advance(int x, int y, bool loose_tail);
};
#endif // SNAKE_H_INCLUDED
从 emplace_front
函数参数中删除大括号。 emplace*
函数的语义意味着参数像在容器类型 T
的构造函数中一样传递。用 {}
传递它使参数类型为 std::initializer_list
。
emplace_front
function takes its arguments and passes to a suitable constructor in the contained object. The Test
class doesn't have a constructor which takes a single initializer-list 参数。它有一个带有两个 int
参数的构造函数,这意味着您可以使用
tester.emplace_front(x, y);
它还有一个复制构造函数,这意味着你可以这样做
tester.emplace_front(Test{x, y});