gpt4 book ai didi

c++ - 从 'char'到 'char*' strcat函数的无效转换

转载 作者:行者123 更新时间:2023-12-02 11:13:15 25 4
gpt4 key购买 nike

因此,我有一个函数,它逐个字符地接收文件输入,并将字符形成为句子以进行修改。在这种情况下,要进行的修改之一是对句子进行处理。这将需要两个句子并通过删除它们之间的标点并将它们连接起来而形成一个连续句子。

这是我的代码:

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样式以null终止的字符数组

最佳答案

错误非常明显,strcat声明为char * strcat ( char * destination, const char * source );,但是sentence不是char*,您必须将sentencechar转换为char*

由于我不知道sentence的来源,因此我无法提供进一步的建议,可能是您应该发布称为runOn的函数。

也许您可以简单地将void runOn(char sentence, ifstream & fin, int counter)更改为void runOn(char* sentence, ifstream & fin, int counter)
参见strcat的声明here

关于c++ - 从 'char'到 'char*' strcat函数的无效转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43316317/

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