C++ “const”错误之前的预期主表达式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22341516/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 00:03:07  来源:igfitidea点击:

expected primary-expression before ‘const’ errors

c++compiler-errorsexpression

提问by user3330980

Please help. I am getting many errors.

请帮忙。我收到很多错误。

sub2.cpp: In function ‘int main()': sub2.cpp:11:14: error: invalid conversion from ‘const char*' to ‘char' [-fpermissive] sub2.cpp:12:14: error: invalid conversion from ‘const char*' to ‘char' [-fpermissive] sub2.cpp:16:17: error: expected primary-expression before ‘const' sub2.cpp:16:36: error: expected primary-expression before ‘const' sub2.cpp:11:6: warning: unused variable ‘outer' [-Wunused-variable] sub2.cpp:12:6: warning: unused variable ‘inner' [-Wunused-variable] make: *[sub2] Error 1

sub2.cpp:在函数“int main()”中:sub2.cpp:11:14:错误:从“const char*”到“char”的无效转换[-fpermissive] sub2.cpp:12:14:错误:无效从 'const char*' 到 'char' 的转换 [-fpermissive] sub2.cpp:16:17:错误:'const' 之前的预期主表达式 sub2.cpp:16:36:错误:'const' 之前的预期主表达式' sub2.cpp:11:6: 警告:未使用的变量 'outer' [-Wunused-variable] sub2.cpp:12:6: 警告:未使用的变量 'inner' [-Wunused-variable] 制作:*[sub2] 错误1

#include <iostream>
#include <string>
#include <cstring>

using namespace std;

char *Subtract(const char *outer, const char *inner);

int main()
{
    char outer = "Bookkepper";
char inner = "keep";

char *word = new char[50];

word = Subtract(const char &outer, const char &inner);

cout << word << endl;
return 0;
}


char *Subtract(const char *outer, const char *inner)
{
int olen = strlen(outer);
int first_occ_idx = -1;
for(int i=0; i < olen; i++){
    if(strncmp(outer+i, inner,strlen(inner)) == 0){
    first_occ_idx = i;
    }
}
if(first_occ_idx == -1){
    return NULL;
}
int ilen = strlen(inner);
int xx = olen - ilen;
char *newstr = new char[xx];
int idx = 0;
for(int i=0; i < first_occ_idx; i++){
    newstr[idx++] = outer[i];
}
for(int i=first_occ_idx+ilen; i < olen; i++){
    newstr[idx++] = outer[i];
}
newstr[idx] = '
const char *outer = "Bookkeeper"; // Note also spelling
'; return newstr; }

采纳答案by paxdiablo

In C++, string literals like "Bookkepper"(sic)are constcharacter pointers,it's a little stricter than in C. So it should be:

在 C++ 中,像"Bookkepper"(sic)这样的字符串文字是const字符指针,它比 C 中的要严格一些。所以它应该是:

char outer = "Bookkepper";

rather than:

而不是:

word = Subtract(const char &outer, const char &inner);

In addition, you don't include types when callinga function, so:

此外,调用函数时不包括类型,因此:

word = Subtract(outer, inner);

would be better as:

会更好:

delete[] word;


Separately (and these are style suggestions only), the correct type for things that represent sizes(such as number of characters in a string) is size_trather than int.

另外(这些只是样式建议),表示大小(例如字符串中的字符数)的事物的正确类型是size_t而不是int.

And it's usually considered good form to clean up all your dynamic memory explicitly so, before returning from main(), you could put:

并且通常认为明确清理所有动态内存的好形式,因此,在从 返回之前main(),您可以输入:

##代码##