Question

Cant see what i'm doing wrong ...

my GameWindow.h has code -

#include <iostream>
#include <string>

using namespace::std;
class GameWindow{
    string WindowType;
    string WindowName;
    int TopLeftX,TopLeftY;
    int Height,Width;
    bool GetBoxed;
public:
    GameWindow(string WinType, string WinName,int TLX,int TLY,int y,int x,bool box);
    void SetHeight(int y);
    void SetWidth(int x);
    void SetTopLeftY(int TLY);
    void SetTopLeftX(int TLY);
};

And the source file code being -

#include "GameWindow.h"

GameWindow::GameWindow(string WinType, string WinName,int TLX,int TLY,int y,int x,bool box){
    WindowType = WinType; WindowName = WinName;
    TopLeftX = TLX; TopLeftY = TLY;
    Height = y; Width = x;
    GetBoxed = box;
};

void GameWindow::SetHeight(int y){Height = y;}
void GameWindow::SetWidth(int x){Width = x;}
void GameWindow::SetTopLeftY(int TLY){TopLeftY=TLY;}
void GameWindow::SetTopLeftX(int TLX){TopLeftX=TLX;}

so then in another source file i attempt to create a vector of GameSpace and call the constructor each time i add one to the vector -

int OffsetX =5;

    vector<GameWindow>GameSpace;

    GameSpace.resize(8);
    GameSpace[0] = GameWindow("MonstersLeftWin", "Misc", 
    (getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);

i get the "no matching constructor for initialisation of GameWindow" error. Cant see what im doing wrong at all!

i also get the error if i have -

    GameSpace[0] = *new GameWindow("MonstersLeftWin", "Misc", 
    (getmaxx(stdscr)-22-OffsetX), 2, 1, 22, true);

still unsure if i need the "new" there or not.

Thanks for the help.

Was it helpful?

Solution

The problem here is the GameSpace.resize(8) statement. As soon as you provide a custom constructor, the compiler wont generate a default constructor for you anymore. However, std::vector will default initialize elements in the resize call.

Either provide a default constructor (preferred): GameWindow()

Or use the void resize (size_type n, const value_type& val); overload

OTHER TIPS

In this statement

GameSpace.resize(8);

the compiler tries to call the default constructor of class GameWindow. However the class has no default constructor. So the compiler issues the error.

Instead of

GameSpace.resize(8);

you could write for example something as

GameSpace.resize( 8, { "", "", 0, 0, 0, 0, false }  );
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top