在const之前预期的primary-expression,与operator []不匹配

时间:2013-12-01 08:06:22

标签: c++

我正在尝试将我的数组(网格)点发送到我的loadGrid函数,但由于某种原因它不起作用。最初是因为我声明我的网格是我的amountofcolumns和amountofrows变量的大小,所以我尝试通过在两个维度中声明网格为100来“修复”,以便我可以测试程序的其余部分,但它仍然不起作用。在我调用loadGrid的行中,我得到错误“const之前的primary-expression”,当我尝试将值赋给网格值的变量然后打印它时,我得到“不匹配operator []” 。有什么建议? 这是我的代码

#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <string>
#include <vector>

using namespace std;

int amountofcolumns = 0;
int amountofrows = 0;
int readGrid(int argc, char** argv);

class car
{
    int carnumber;
    int xpos;
    int ypos;
    int dxvel;
    int dyvel;
    int maxspeed = 5;
    int currentspeed;
};

struct point
{
    int val; //value of the square
    int caroccupied; //what car is on it, 0 if no car
    char given; //what the actual character of the space is
    bool wall; // is the thing a wall?
};

int loadGrid(int argc, char** argv, const point &grid, int amtcol, int amtrow);

int main(int argc, char** argv)
{
    readGrid(argc, argv);
    cout << "Testing to see if this worked" << endl;
    cout << amountofcolumns << " " << amountofrows;
    point grid[100][100];
    loadGrid(argc, argv, const point &grid, amountofcolumns, amountofrows);
}

int loadGrid(int argc, char** argv, const point &grid, int amtcol, int amtrow)
{
    grid[1][3].val = 51;
    cout << grid[1][3].val;
}

int readGrid(int argc, char** argv)
{
    //This code determines how many lines there are
    //in the grid, and how many columns there are.
    string linelengthbeta;
    string lineamountbeta;
    int counter = 0;
    std::string current_exec_name = argv[0];
    std::string filename;
    if (argc > 1) {
        filename = argv[1];
    }
    cout << filename;
    ifstream infile(filename.c_str());
    while(!infile.eof())
    {
        getline(infile, lineamountbeta);
        counter++;
    }
    amountofcolumns = linelengthbeta.length();
    cout << amountofcolumns;
    amountofrows = counter + 1;
    cout << amountofrows;
    infile.close();

    return amountofrows;
    return amountofcolumns;
}

1 个答案:

答案 0 :(得分:1)

根据您的LoadGrid函数签名,您传递的是单个点const point &grid,但根据您的代码,您传递了一个点[100] [100]。

切换到此功能签名,效果更好:

int loadGrid(int argc, char** argv, point grid[][100], int amtcol, int amtrow)

(称之为:loadGrid(argc, argv, grid, amountofcolumns, amountofrows);)