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

时间:2015-12-05 20:27:59

标签: c++ arrays string function dynamic

我现在已经在课堂上完成了这个作业约一个月了。它已经过时了,我试图为了自己的利益更多地完成它,所以我理解动态数组和指针是如何工作的。我对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);
}

1 个答案:

答案 0 :(得分:1)

功能声明:

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[]);

并且删除阵列需要更改:

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