为什么我的构造函数在结构中的映射不起作用?

sna*_*iii 0 c++ constructor stl map

这是我的结构,我正在尝试为此编写默认构造函数.

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) {}

};
Run Code Online (Sandbox Code Playgroud)

请帮我理解这个错误意味着什么:

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'
Run Code Online (Sandbox Code Playgroud)

Luc*_*ore 6

因为nextmap而且prevmap不是变量,而是类型.由typedef(它定义一种类型)清楚地表明.

你的意思是:

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) {}

};
Run Code Online (Sandbox Code Playgroud)

或许这可能会让你感到困惑:

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) {}

};
Run Code Online (Sandbox Code Playgroud)