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

时间:2014-07-14 03:50:25

标签: c++ string

#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 个答案:

答案 0 :(得分:1)

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

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

char string80[81], name[81]; 

答案 1 :(得分:0)

您要将name声明为类型char,应将其键入为string80(来自您的typedef)。

你也意外隐藏了你的typedef,声明char string80,它隐藏了周围范围的typedef。

您要声明name类型string80,而不是类型char。像这样:

string80 name;