Reputation: 134
I have a class Player which contains an instance variable: \
vector<Card> userCards;
In order to avoid any compilation errors I forward declared the class Card
. However now when I try to Build Solution I get an error saying
Card *: Unknown size.
Basically I am trying to create a Player
who contains a non-fixed number of cards, so I tried using a vector and now I cannot get it to work.
Player.h
#include <iostream>
#include <vector>
using std::string;
using std::vector;
#ifndef PLAYER_H_
#define PLAYER_H_
class Card;
class Player {
private:
vector<Card> userCards;
};
#endif
Card.h
#include <iostream>
using std::string;
#ifndef CARD_H_
#define CARD_H_
class Card {
private:
string name;
string type;
public:
Card(const string& name, const string& type);
};
#endif
I have a bunch of different functions that are not related, so I did not include them.
Upvotes: 1
Views: 11635
Reputation: 1463
I am assuming you included the Card.h header in the Player.h file using the #include "Card.h"
directive
but, if you did not then be informed that std::vector<T>
requires its parameter to be a complete type and you cannot pass a forward declared type as its template argument.
Here is a another question/answer that clarifies your problem: When can I use a forward declaration?
Upvotes: 3
Reputation: 19607
The type template argument of std::vector
cannot be an incomplete type. It has to be defined (complete) before instantiation of std::vector<Card>
. To do that, replace your forward declaration class Card;
with #include "Card.h"
directive.
You can see the further requirements on template arguments here.
Upvotes: 5
Reputation: 1
vector<Card>
needs to see a complete declaration of Card
. There are some certain functions that are needed for instantiation.
You can do something like a
vector<unique_ptr<Card>> userCards;
though, which behaves as any pointer (reference) declaration, and accepts the forward declaration.
Upvotes: 2
Reputation: 92271
The vector doesn't have to know how many cards you want to store, but is has to know the size of a Card
.
So don't forward declare, but #include
the Card.h.
Upvotes: 2