C++ 常量前应为主表达式,运算符[]不匹配

C++ 常量前应为主表达式,运算符[]不匹配,c++,C++,我正试图将我的点数组(网格)发送到loadGrid函数,但由于某些原因它无法工作。最初是因为我将网格声明为amountofcolumns和amountofrows变量的大小,所以我尝试通过将网格声明为两个维度的100来“修复”,这样我就可以测试程序的其余部分,但它仍然不起作用。在我调用loadGrid的那一行中,我得到了错误“const之前的预期主表达式”,当我尝试为网格值的变量赋值并打印它时,我得到了“与运算符[]不匹配”。有什么建议吗? 这是我的密码 #include <iostrea

我正试图将我的点数组(网格)发送到loadGrid函数,但由于某些原因它无法工作。最初是因为我将网格声明为amountofcolumns和amountofrows变量的大小,所以我尝试通过将网格声明为两个维度的100来“修复”,这样我就可以测试程序的其余部分,但它仍然不起作用。在我调用loadGrid的那一行中,我得到了错误“const之前的预期主表达式”,当我尝试为网格值的变量赋值并打印它时,我得到了“与运算符[]不匹配”。有什么建议吗? 这是我的密码

#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
int amountofcolumns=0;
int amountofrows=0;
int readGrid(int argc,字符**argv);
班车
{
int卡号;
int XPO;
int ypos;
int dxvel;
int-dyvel;
int-maxspeed=5;
电流速度;
};
结构点
{
int val;//平方的值
int caroccumped;//上面有什么车,如果没有车,则为0
char gived;//空间的实际字符是什么
布尔墙;//这东西是墙吗?
};
内部加载网格(内部argc、字符**argv、常量点和网格、内部amtcol、内部amtrow);
int main(int argc,字符**argv)
{
readGrid(argc、argv);

cout根据LoadGrid函数签名,您正在传入一个单点
常量点和网格
,但根据代码,您正在传入一个点[100][100]

切换到此函数签名,它会工作得更好:

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

(将其命名为:
loadGrid(argc、argv、grid、amountofcolumns、amountofrows)
;)

Indentation?这是旧的方式吗?LoadGrid for grid中的arg类型是错误的:const point&grid,
return amountofrows;return amountofcolumns;
您会大声嚷嚷着要注意这里的编译器警告!是的,两个return语句确实预示着代码是正确的。@JoJoBya:您真的需要包含一个。我们不能期望当然,这可能不是您真正想要的签名,但如果您想要点**网格签名,您可能需要传入一个创建方式不同的东西。