James Balajan James Balajan - 1 month ago 26
C++ Question

C++ : No matching function for call to std::deque<SnakePart>::emplace_front

Recently I have been working on remaking my remake of the classic snake game, this time employing a deque instead of a linked list for the snake's tail. I construct a tail segment and try to emplace it at the front of the deque and get a strange error.

no matching function for call to 'std::deque<SnakePart>::emplace_front(<brace-enclosed initializer list>)'|


Since I am still quite new to C++ I am clueless as to why this error is occurring. It would be appreciated if someone could help me solve this conundrum.

SnakePart.cpp


http://pastebin.com/verR9bpn

Snake.h

http://pastebin.com/XUyNAVKK

Minimal Complete & Verifiable Example:



#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

Answer

The 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 argument. It have a constructor taking two int arguments which means you could use

tester.emplace_front(x, y);

It also have a copy-constructor which means you could do

tester.emplace_front(Test{x, y});