首页 文章

为什么我会收到错误错误C2664:'reverseString'

提问于
浏览
0
#include <iostream> 
#include <string> 
#include <iomanip> 

using namespace std;

typedef char string80[81];      // create a synonym for another type 
void reverseString(string80);       // function prototype 
int main()
{            
  // start program compilation here 
  char string80, name;          // variable to contain the name of the user 

  cout << "Enter your name =====> " ;
  cin >> name,81; 

  cout << "\n\nWelcome to Computer Science 1106 " << name << endl<< endl; 

  reverseString(name);

  cout << "Your name spelled backwards is " << name << endl << endl; 

  return 0;
} // end function main 
  // Function to reverse a string 
  // Pre: A string of size <= 80 Post: String is reversed 

void reverseString(string80 x)
{ 
  int last = strlen(x)- 1;  // location of last character in the string 
  int first = 0;        // location of first character in the string 
  char temp;  

  // need a temporary variable 
  while(first <= last)
  { // continue until last > first 

    temp = x[first];      // Exchange the first and last characters
    x[first] = x[last];         
    x[last] = temp; 
    first++;        // Move on to the next character in the string
    last--;             // Decrement to the next to last character in the string
  }// end while 
}// end reverseString

我收到一个错误

C2664:'reverseString':无法将参数1从'char'转换为'char []'从积分类型转换为指针类型需要reinterpret_cast,C风格的转换或函数式转换

2 回答

  • 1

    reverseString 函数接受 char [81] 作为x参数,但是当你调用它时,它会发送一个 char .

    您可能想要做的是将 string80name 声明为 char [81] 而不是 char

    char string80[81], name[81];
    
  • 0

    您正在声明类型为 charname ,它应该被输入为 string80 (来自您的typedef) .

    您也通过声明 char string80 意外隐藏了typedef,它隐藏了周围范围的typedef .

    您希望声明类型为 string80name ,而不是 char 类型 . 像这样的东西:

    string80 name;
    

相关问题