【问题标题】:'...' is not a type c++ compilation error'...' 不是类型 c++ 编译错误
【发布时间】:2012-05-23 06:55:13
【问题描述】:

我正在为作业编写一个小游戏,它需要使用地图,我已成功将地图放入二维数组,但现在进一步研究作业,我发现我需要访问数组 Map[][]在另一个函数中。我试图让它工作但失败了。我用 g++ 得到的错误是“错误:'地图'不是类型”任何帮助将不胜感激。

我已经搜索过,但要么我不擅长使用搜索引擎,要么找不到任何特定于此错误的信息。

const int MapSZ = 10; //In Global
int Map[MapSZ][MapSZ]; // Also Global

void GetMap(ifstream&, int); //Getting the map (Proto)

GetMap(fin, Map[MapSZ][MapSZ]); //In the main function.

void GetMap(ifstream& fin, Map[MapSZ][MapSZ]) //Inserting the map into an array

【问题讨论】:

    标签: c++ compiler-errors g++


    【解决方案1】:
    void GetMap(ifstream& fin, Map[MapSZ][MapSZ]) 
    

    应该是:

    void GetMap(ifstream& fin, int Map[MapSZ][MapSZ]) 
                               ^^^^
    

    注意,Map 是数组的名称,但您没有提及它的类型

    【讨论】:

    • GetMap 将与 GetMap 的早期原型不兼容。我不认为重载在这里是可取的。
    • 谢谢,我输入了 'int' 但现在我得到一个看起来很奇怪的编译错误 "/tmp/cc5xDEOL.o: In function main': P1P2.cpp:(.text+0x65): undefined reference to GetMap(std::basic_ifstream >&, int)' collect2: ld 返回 1 个退出状态"
    • @Joey 函数调用传递了一个 int,而不是整个 Map。正确的声明是void GetMap(ifstream& fin, int MapElement),因为该函数不知道整个地图。最好从函数定义中复制原型,而不是从调用站点。编辑:等等,你已经有了一个正确的原型。 “将地图插入数组”行应该是什么?
    • Map 的类型是一个二维整数数组。
    • @Potatoswatter 该行后面是将数字输入数组的函数 - 请参见此处 for(int i = 0; i > Map[i][j] ; } }
    【解决方案2】:

    如果Map[MapSZ][MapSZ] 被定义为全局,如您的注释所述(即它在main.cpp 中定义但在主函数之外),则无需将其作为参数传递给GetMap。你可以简单地做类似的事情

    void GetMap(ifstream& fin); //proto
    
    int main(int argc, const char * argv[]) {
        GetMap(fin);
    }
    
    void GetMap(ifstream& fin) {
        //some code that uses Map[MapSZ][MapSZ]
    }
    

    【讨论】:

    • 谢谢,这已经解决了。现在感觉好蠢……无论如何,干杯!
    猜你喜欢
    • 1970-01-01
    • 2017-09-22
    • 1970-01-01
    • 1970-01-01
    • 2014-01-31
    • 2021-09-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多