Hi all,

I am trying to write a small game with a tiled gameplay area. I have a vector of Tile objects - vector<Tile> area_tiles; - and I'm trying to add Tile objects to this vector like so:

Tile t();			// initialize new tile object
    area_tiles.push_back(t);	// and add it to vector

This is the way I have added objects to vectors in previous projects and it has always seemed to work (perhaps due to blind luck?), until now. When I try to compile, I get this error:

error: no matching function for call to `std::vector<Tile, std::allocator<Tile> >::push_back(Tile (&)())'

I am a rank beginner with C++ and have absolutely no idea what is wrong, and I'm not experienced enough to understand what the error message is trying to tell me. I am sure the root of the problem is painfully obvious to anyone who has a clue, if someone could enlighten me?

could you post where you declared the vector template (im assuming its a template) theres not enough information for me to help right now

Edited 6 Years Ago by Anarionist: n/a

could you post where you declared the vector template (im assuming its a template)

I'm afraid I'm not sure what you're asking for (this is probably indicative of my overall level of understanding).

He means when you declared area_tiles I think it might be the parens you have after t. You don't need them when declaring an object by the default constructor. Just do Tile t;

He means when you declared area_tiles

I have a separate class for the game area, and the vector of tiles is declared therein:

class Area
{
    public:

    Area( int y, int x );

    // stuff

    vector<Tile> area_tiles;
};

I think it might be the parens you have after t. You don't need them when declaring an object by the default constructor.

Cool, I didn't know that :)

Just do Tile t;

This gets me the following:

error: aggregate `Tile t' has incomplete type and cannot be defined

I guess this points to me having messed up somewhere else?

Post your whole code or attach it as files.

For clarity's sake, here is everything relevant.

#include <iostream>
#include <time.h>
#include <vector>

using namespace std;

class Tile
{
    public:

    Tile();

    int tile_type;
};

Tile::Tile()
{
    // select one of three types of tile
    tile_type = rand()% 3;
}

class Area
{
    public:

    Area( int y, int x );

    vector<Tile> area_tiles;
};

Area::Area( int y, int x )
{
    for ( int i = 0; i != y*x; ++i )
    {
        Tile t();
        area_tiles.push_back(t);
    }
}

int main()
{
    srand(time(NULL));

    Area game_area ( 20, 20 );

    return 0;
}

When I cleared the parentheses mentioned in the other post (line 35 in this code) I don't get any errors or warnings.

replacing Tile with Tile1

I'm not sure what the OP would gain from that.

I ran it and I got a vector of size 400 at the end of the Area constructor.

I wanted to check whether there is a naming conflict since the code has

using namespace std;

It could be something specific to the version that is being used.

Edited 6 Years Ago by thomas_naveen: n/a

Area::Area( int y, int x )
{
    for ( int i = 0; i != y*x; ++i )
    {
        // the compiler sees the below line as a function declaration,
        // 't' is a function that takes no arguments and returns a Tile.
        Tile t();

        area_tiles.push_back(t);
    }
}

So, the fix is to remove the parenthesis i.e.

Tile t;

Yes, it's been suggested.

I know, it's just that the exact/real reason why that line is wrong hasn't been pointed out. Hence my two cents.

This article has been dead for over six months. Start a new discussion instead.