在c++中定义map类型的map

Define a map of map type in C++?

本文关键字:map 类型 定义 c++      更新时间:2023-10-16

我试图定义从longmaplongFILE *的无序映射,但不断得到编译器错误。知道怎么纠正吗?

#include <string>
#include <iostream>
#include <stdio.h>
#include<unordered_map>
using namespace std;
typedef unsigned long ulong;
typedef unsigned int uint;
typedef unsigned short uint16;
typedef unsigned char uchar;
typedef std::unordered_map<long, std::unordered_map<long, *FILE> > TYPE1;
int main(int argc, char *argv[]) {
    TYPE1 x;
    x[1][2] = fopen(argv[1], "rb");
    return 0;
}

下面是编译错误

$ g++ -std=c++11 te2a.cc
te2a.cc:15:64: error: template argument 2 is invalid
te2a.cc:15:64: error: template argument 5 is invalid
te2a.cc:15:66: error: template argument 2 is invalid
te2a.cc:15:66: error: template argument 5 is invalid
te2a.cc:15:73: error: invalid type in declaration before ‘;’ token
te2a.cc: In function ‘int main(int, char**)’:
te2a.cc:19:5: error: invalid types ‘TYPE1 {aka int}[int]’ for array subscript

这是星号的位置:

typedef std::unordered_map<long, std::unordered_map<long, FILE*>> TYPE1;

注意星号:

typedef std::unordered_map<long, std::unordered_map<long, *FILE>> TYPE1;

改为:

typedef std::unordered_map<long, std::unordered_map<long, FILE*>> TYPE1;