gpt4 book ai didi

c++ - 动态字符串数组,编译错误

转载 作者:行者123 更新时间:2023-11-30 05:34:47 25 4
gpt4 key购买 nike

我已经为类完成这项作业大约一个月了。它已经过期了,为了我自己的利益,我正在努力完成它,所以我了解动态数组和指针是如何工作的。我对 C++ 还是很陌生,非常感谢您提供的任何信息。感谢您提前抽出时间。

请注意 id、cstation、lab 是程序其他部分用于创建菜单、搜索和登录功能的其他变量,并且工作正常。

我的代码出现的错误是:

project_9_5.cpp: In function ‘void arraysize(std::string&, int&, int&, std::string***, std::string*)’:
project_9_5.cpp:79:22: error: expected primary-expression before ‘[’ token
{ labs[i] = string [labsize[i]]; }
^
project_9_5.cpp:81:46: error: expected primary-expression before ‘]’ token
fillarrays(id, cstation, lab, labs, labsize[]);

我的代码:

const string LAB1 = "5";
const string LAB2 = "6";
const string LAB3 = "4";
const string LAB4 = "3";

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]);
void fillarrays(string &id, int &lab, int &cstation, string **labs[], string labsize[]);
void printarrays(string &id, int &cstation, int &lab, string **labs[], string labsize[]);
void removearrays(string **labs[]);

int main() {
string **labs[TOTAL_LABS];
string labsize[TOTAL_LABS] = { LAB1,LAB2,LAB3,LAB4 };

arraysize(id, cstation, lab, labs, labsize);
printarrays(id, cstation, lab, labs, labsize);
removearrays(labs);
return 0;
}

void removearrays(string **labs[]) {

delete [] labs;
}

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]) { //TOnnings example

//int labsize[TOTAL_LABS] = { LAB1,LAB2,LAB3,LAB4 };

labs = new string **[TOTAL_LABS];

for(int i=0; i< TOTAL_LABS; i++)
{ labs[i] = string [labsize[i]]; }

fillarrays(id, cstation, lab, labs, labsize[]);
}


void fillarrays(string &id, int &lab, int &cstation, string **labs[], string labsize[]) {

for(int row = 0; row<TOTAL_LABS;row ++) {
for(int col=0; col<TOTAL_LABS; col++) {
**labs[row] = "Empty";
}
}
}

void printarrays(string &id, int &cstation, int &lab, string **labs[], string labsize[]) {

cout << "\nLabs#" << " " << "Computer Stations" << endl;;
for(int rows=0 ;rows<TOTAL_LABS; rows++) {
cout << " " << (rows+1) << ": ";
for(int cols=0; cols<TOTAL_LABS; cols++) {
cout << (cols+1) << ": ";
cout << labs[rows] << " ";
}
cout << endl;
}
//menu(id, cstation, lab, labs);
}

最佳答案

函数声明:

void arraysize(string &id, int &lab, int &cstation, string **labs[], string labsize[]);

你调用这个函数:

arraysize(id, cstation, lab, labs, labsize);

cstationlab 不在他们的位置上.printarraysfillarrays同样的问题

我不知道你想在这一行做什么

{ labs[i] = string [labsize[i]]; }

但是你不能使用像数组这样的类型名

在这一行中,[]labsize 不是必需的。

fillarrays(id, cstation, lab, labs, labsize[]);

并且 removearrays 需要改变:

void removearrays(string **labs[]) {
for (int i = 0; i < TOTAL_LABS; i++)
delete [] labs[i];
delete [] labs;
}

关于c++ - 动态字符串数组,编译错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34110479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com