RyuAkamatsu
RyuAkamatsu

Reputation: 239

Creating a getter function for an std::vector<struct>

This is the first time i've ever actually asked a question on here so I apologise if the formatting is a little off. Also i'm returning to programming for the first time in a good couple of years so i'm rusty and i'm confused as to how to create a getter function for my vector of structs.

assetLoader.h:

public:
    assetLoader();
    virtual ~assetLoader();

    std::vector<assetLoader:playerStruct> getTypesOfPlayer() {return typesOfPlayer;}

private:
    //Create a struct to hold the Player data
    struct playerStruct
    {
    };
    playerStruct newPlayer;
    std::vector <playerStruct> typesOfPlayer;

Whenever I try to compile the solution, I am getting errors of:

error: template argument 1 is invalid

error: template argument 2 is invalid

I have tried several different attempts to fix it myself but I haven't managed to get anywhere so any help will be greatly appreciated :)

Upvotes: 1

Views: 302

Answers (1)

geoalgo
geoalgo

Reputation: 688

You need either to forward declare your playerStruct or to define it before.

This is how you would forward declare :

class assetLoader{
  struct playerStruct;
public:
  assetLoader();
  virtual ~assetLoader();
  std::vector<assetLoader::playerStruct> getTypesOfPlayer() {
    return typesOfPlayer;
  }
private:
  //Create a struct to hold the Player data
  struct playerStruct
  {
  };
  playerStruct newPlayer;
  std::vector <playerStruct> typesOfPlayer;
};

Upvotes: 2

Related Questions