从 'char' 到 'char*' strcat 函数的无效转换
invalid conversion from 'char' to 'char*' strcat function
所以我有一个功能,可以逐个字符地接收文件输入,并将字符组成要修改的句子。要做的修改之一是在本例中对句子添加 运行。它将取两个句子并通过删除它们之间的标点符号并将它们连接起来形成一个 运行-on 句子。
这是我的代码:
void runOn(char sentence, ifstream & fin, int counter)
{
char ch;
int sentCounter = 0;
bool sentenceEnd = false;
while(sentCounter<=2)
{
char tempSent[SENT_LENGTH];;
do
{
fin.get(ch);
for(int i = 0; i<SENT_LENGTH;i++)
{
tempSent[i] = ch;
}
if(ch == '.' || ch == '?' || ch == '!')
{
sentCounter++;
sentenceEnd = true;
}
}while(sentenceEnd == false);
strcat(sentence,tempSent);
}
}
仅使用传递的计数器,因为前两个句子的函数应该只 运行。
当我尝试编译时,出现此错误:
function.cpp:36:29: error: invalid conversion from 'char' to 'char*' [-fpermissive]
strcat(sentence,tempSent);
编辑:我应该补充一点,我只允许使用 C 风格的空字符数组
错误很明显,strcat
被声明为char * strcat ( char * destination, const char * source );
,但是sentence
不是char*
,你必须将sentence
从char
到 char*
.
因为我不知道 sentence
来自哪里,所以我无法提供进一步的建议,也许你应该 post 调用 runOn
的函数。
也许您可以简单地将 void runOn(char sentence, ifstream & fin, int counter)
更改为 void runOn(char* sentence, ifstream & fin, int counter)
参见 strcat
here
的声明
http://www.cplusplus.com/reference/cstring/strcat/ 如您所见,strcat 在您的函数中接受 char *
而不是 char
您需要将句子作为 char*
然后您的函数将起作用。
所以我有一个功能,可以逐个字符地接收文件输入,并将字符组成要修改的句子。要做的修改之一是在本例中对句子添加 运行。它将取两个句子并通过删除它们之间的标点符号并将它们连接起来形成一个 运行-on 句子。
这是我的代码:
void runOn(char sentence, ifstream & fin, int counter)
{
char ch;
int sentCounter = 0;
bool sentenceEnd = false;
while(sentCounter<=2)
{
char tempSent[SENT_LENGTH];;
do
{
fin.get(ch);
for(int i = 0; i<SENT_LENGTH;i++)
{
tempSent[i] = ch;
}
if(ch == '.' || ch == '?' || ch == '!')
{
sentCounter++;
sentenceEnd = true;
}
}while(sentenceEnd == false);
strcat(sentence,tempSent);
}
}
仅使用传递的计数器,因为前两个句子的函数应该只 运行。
当我尝试编译时,出现此错误:
function.cpp:36:29: error: invalid conversion from 'char' to 'char*' [-fpermissive]
strcat(sentence,tempSent);
编辑:我应该补充一点,我只允许使用 C 风格的空字符数组
错误很明显,strcat
被声明为char * strcat ( char * destination, const char * source );
,但是sentence
不是char*
,你必须将sentence
从char
到 char*
.
因为我不知道 sentence
来自哪里,所以我无法提供进一步的建议,也许你应该 post 调用 runOn
的函数。
也许您可以简单地将 void runOn(char sentence, ifstream & fin, int counter)
更改为 void runOn(char* sentence, ifstream & fin, int counter)
参见 strcat
here
http://www.cplusplus.com/reference/cstring/strcat/ 如您所见,strcat 在您的函数中接受 char *
而不是 char
您需要将句子作为 char*
然后您的函数将起作用。