gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 06:47:19 26 4
gpt4 key购买 nike

#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' : cannot convert parameter 1 from 'char' to 'char []' Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast

最佳答案

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

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

char string80[81], name[81]; 

关于c++ - 为什么我会收到错误错误 C2664 : 'reverseString' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24729277/

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