snazziii
snazziii

Reputation: 491

Why is my constructor for maps in a struct not working?

This is the struct that I have and I'm trying to write the default constructor for that.

struct Cnode
{
typedef std::map<char, int> nextmap;
typedef std::map<char, int> prevmap;

Cnode() : nextmap(), prevmap() {} //error
Cnode(const nextmap2, const prevmap2) : nextmap(nextmap2), prevmap(prevmap2) {}

};

Please help me understand what this error means:

Type 'nextmap'(aka 'map<char,int>') is not a direct or virtualbase of 'Cnode'
Type 'prevmap'(aka 'map<char,int>') is not a direct or virtualbase of 'Cnode'

Upvotes: 0

Views: 168

Answers (1)

Luchian Grigore
Luchian Grigore

Reputation: 258548

Because nextmap and prevmap aren't variables, but types. As clearly indicated by the typedef (it defines a type).

Did you mean:

struct Cnode
{
std::map<char, int> nextmap;
std::map<char, int> prevmap;

Cnode() : 
  nextmap(), prevmap() {}
Cnode(const std::map<char, int>& nextmap2, const std::map<char, int>& prevmap2) : 
  nextmap(nextmap2), prevmap(prevmap2) {}

};

or perhaps this might clear your confusion:

struct Cnode
{
typedef std::map<char, int> MapOfCharToInt;  //defines a new type

MapOfCharToInt nextmap;                      //defines variables
MapOfCharToInt prevmap;                      //of that type

Cnode() : 
   nextmap(), prevmap() {} 
Cnode(const MapOfCharToInt& nextmap2, const MapOfCharToInt& prevmap2) : 
   nextmap(nextmap2), prevmap2(prevmap2) {}

};

Upvotes: 6

Related Questions