错误:令牌之前的预期主表达式']'
error: expected primary-expression before ']' token
我得到错误:
应在']'标记`之前使用主表达式
在线:
berakna_histogram_abs(histogram[], textRad);
有人知道为什么吗?
const int ANTAL_BOKSTAVER = 26; //A-Z
void berakna_histogram_abs(int histogram[], int antal);
int main() {
string textRad = "";
int histogram[ANTAL_BOKSTAVER];
getline(cin, textRad);
berakna_histogram_abs(histogram[], textRad);
return 0;
}
void berakna_histogram_abs(int tal[], string textRad){
int antal = textRad.length();
for(int i = 0; i < antal; i++){
if(textRad.at(i) == 'a' || textRad.at(i) == 'A'){
tal[0] + 1;
}
}
}
In main()函数调用错误:
berakna_histogram_abs(histogram[], textRad);
应该是:
berakna_histogram_abs(histogram, textRad);
您只需要在函数声明中使用[]
,但在调用函数时不需要。
您对函数berakna_histogram_abs
的调用在main()
中是错误的,应该是:
berakna_histogram_abs(histogram, textRad);
// ^
函数声明中的[]
表示它接受一个数组,您不必在函数调用中使用它。
您有另一个错误:
函数berakna_histogram_abs
的原型是:
void berakna_histogram_abs(int histogram[], int antal);
// ^^^
main()
定义和之前
void berakna_histogram_abs(int tal[], string textRad){...}
// ^^^^^^
此外,在您的main中,您正试图传递一个字符串作为参数,因此您的代码应该是:
void berakna_histogram_abs(int histogram[], string antal);
int main()
{
// ...
}
void berakna_histogram_abs(int tal[], string textRad){
//....
}
最后一件事:尝试传递引用或const
引用,而不是值:
void berakna_histogram_abs(int tal[], string& textRad)
// ^
您的最终代码应该看起来像:
const int ANTAL_BOKSTAVER = 26; //A-Z
void berakna_histogram_abs(int histogram[], const string& antal);
int main() {
string textRad = "";
int histogram[ANTAL_BOKSTAVER];
getline(cin, textRad);
berakna_histogram_abs(histogram, textRad);
return 0;
}
void berakna_histogram_abs(int tal[], const string& textRad) {
int antal = textRad.length();
for(int i = 0; i < antal; i++){
if(textRad.at(i) == 'a' || textRad.at(i) == 'A'){
tal[0] + 1;
}
}
}
您将表传递给函数是错误的。你应该简单地:
berakna_histogram_abs(histogram, textRad);
此外,您首先声明的内容:
void berakna_histogram_abs(int histogram[], int antal);
但比你试图定义的:
void berakna_histogram_abs(int tal[], string textRad){}
这就是编译器认为第二个参数是int
而不是string
的方式。函数的原型应该与声明一致。
传递histogram[]
时出错仅通过histogram
在参数中,您已将第二个参数定义为int
,但在定义函数时,您保留了第二个变量为string
类型
更改初始定义
void berakna_histogram_abs(int histogram[], int antal);
至
void berakna_histogram_abs(int histogram[], string textRad);
相关文章:
- holeMenuProgram.cpp:38:1 错误:'}'令牌之前的预期主表达式
- (错误:令牌之前'<<'预期的主表达式)
- 错误:C++中']'令牌之前的预期主表达式
- 错误:令牌之前的预期主表达式'&&'
- 令牌之前']'预期的主表达式?
- 错误:令牌之前的预期主表达式'/'
- 正则表达式使用提升令牌迭代器在单引号和括号之间提取值
- 令牌之前')'预期的主表达式(需要"template"消除歧义器)
- 错误:令牌之前的预期主表达式'<':Arduino RGB 舞灯
- 令牌之前出现错误')'主表达式
- C++错误:错误:令牌之前的预期主表达式'||'
- 令牌问题之前'*'预期的主表达式
- 令牌之前的预期表达式']'?C
- 退出状态 1 令牌之前的预期主表达式'.'
- C++错误:";"之前应为主表达式使用Cmake的令牌
- 向量空Push_back在令牌之前调用'{'预期的主表达式
- 令牌调用之前'...'预期的主表达式 模板化类的模板化方法
- 错误 预期主表达式在 ';" 之前令牌 C++
- 令牌之前的预期主表达式'-'
- 令牌之前的预期主表达式'<'