来自' char'的无效转换到#char;' strcat函数

时间:2017-04-10 06:32:30

标签: c++ compiler-errors char strcat

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

这是我的代码:

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风格的空终止字符数组

2 个答案:

答案 0 :(得分:0)

错误非常明确,strcat声明为char * strcat ( char * destination, const char * source );,但sentence不是char*,您必须从{{1}转换sentence转到char

由于我不知道char*来自何处,我无法提供进一步的建议,您可能应该发布调用sentence的函数。

也许您只需将runOn更改为void runOn(char sentence, ifstream & fin, int counter)

即可

请参阅void runOn(char* sentence, ifstream & fin, int counter) here

的声明

答案 1 :(得分:0)

http://www.cplusplus.com/reference/cstring/strcat/你可以看到strcat在你的函数中接受char *而不是char,你需要将句子作为char*,然后你的函数就可以了。

相关问题