[英]C++ argument of type * is incompatible with parameter of type **
您好,我是 C++ 新手,無法弄清楚為什么我的代碼會這樣做。 我已經在互聯網上搜索並找不到我需要的解決方案。 我感謝所有的幫助。 給我帶來問題的那一行是當我調用該函數時。 根據visual studios,它指出“'char*' 類型的參數與'char**' 類型的參數不兼容”。 它指的是 newArr。
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include "stdafx.h"
using namespace std;
bool isPalindrome(char *newArr[], int);
//int i = 0;
//char phrase;
//char c;
bool palindrome;
bool tOf;
int numb;
char c;
const int length = 80; //const so that it can't be changed
char inarr[length]; //array set to a const length of 80
char newArr[length]; //array that will have no spaces
string str;
int main()
{
cout << "This program tests if a word/phrase is palindrome.\n\n";
cout << "Please enter your phrase (just letters and blanks,
please):\n\n";
cin.getline(inarr, length);
//cout << array; //spits out the array
str = inarr; //turn into string
numb = str.length();
//cout << numb << "\n"; //how many characters in array
for (int i = 0; i < (numb / 2) + 1; i++)
{
for (int j = 0; j < (numb / 2) + 1; j++)
{
newArr[j] = inarr[i]; //from old array to new array
c = newArr[j];
newArr[j] = toupper(c); //change to all upper case
//cout << newArr[j];
i += 2; //goes to every other index to skip space in string
}
}
tOf = isPalindrome(newArr, numb); //calling of function
if (tOf == true) //the response to true or false
{
cout << "\nYes, the phrase is a palindrome!";
}
else
{
cout << "\nNo, the phrase is not a palindrome!";
}
return 0;
}
bool isPalindrome(char *newArr[], int numb) //function to determine true or
false
{
for (int i = 0; i < (numb / 2) + 1; i++) //within the array...
{
if (newArr[i] != newArr[(numb / 2) - i]) //if first index != last
and etc (iterates)
{
palindrome = false;
}
else
{
palindrome = true;
}
}
return palindrome;
}
您正在嘗試將newArr
(一個char *
)傳遞給isPalindrome()
(它需要一個char **
)。 這就是“'char*' 類型的參數與'char**' 類型的參數不兼容”的意思。
要解決這個問題,只需傳入一個char **
; 你可以通過傳入newArr
的地址而不是newArr
本身來做到這一點:
tOf = isPalindrome(&newArr, numb); //calling of function
Brief將函數的函數簽名(包括定義和聲明)更改為
bool isPalindrome(char* newArr, int numb);
稱之為tOf = isPalindrome(newArr, numb);
詳情
如果您調用isPalindrome(newArr, numb)
。 您正在傳遞第一個元素的地址 &newArr[0] 。 所以你在函數定義中應該可以選擇元素的地址。 因此 *newArr
此外,您的函數將使用數組算術驗證詳細信息。 沒關系。
輸出
$ ./a.out
This program tests if a word/phrase is palindrome.
Please enter your phrase (just letters and blanks, please):
Palindrome
No, the phrase is not a palindrome!
$ ./a.out
This program tests if a word/phrase is palindrome.
Please enter your phrase (just letters and blanks, please):
YeseY
Yes, the phrase is a palindrome!
$
您好,我是C ++的新手,無法弄清楚為什么我的代碼這樣做了。 我在互聯網上進行搜索,找不到所需的解決方案。 我感謝所有的幫助。 給我問題的那一行是當我調用該函數時。 根據視覺工作室,它指出“類型為'char *'的參數與類型為'char **'的參數不兼容”。 它指的是newArr。
#include <iostream>
#include <string>
#include <stdio.h>
#include <ctype.h>
#include "stdafx.h"
using namespace std;
bool isPalindrome(char *newArr[], int);
//int i = 0;
//char phrase;
//char c;
bool palindrome;
bool tOf;
int numb;
char c;
const int length = 80; //const so that it can't be changed
char inarr[length]; //array set to a const length of 80
char newArr[length]; //array that will have no spaces
string str;
int main()
{
cout << "This program tests if a word/phrase is palindrome.\n\n";
cout << "Please enter your phrase (just letters and blanks,
please):\n\n";
cin.getline(inarr, length);
//cout << array; //spits out the array
str = inarr; //turn into string
numb = str.length();
//cout << numb << "\n"; //how many characters in array
for (int i = 0; i < (numb / 2) + 1; i++)
{
for (int j = 0; j < (numb / 2) + 1; j++)
{
newArr[j] = inarr[i]; //from old array to new array
c = newArr[j];
newArr[j] = toupper(c); //change to all upper case
//cout << newArr[j];
i += 2; //goes to every other index to skip space in string
}
}
tOf = isPalindrome(newArr, numb); //calling of function
if (tOf == true) //the response to true or false
{
cout << "\nYes, the phrase is a palindrome!";
}
else
{
cout << "\nNo, the phrase is not a palindrome!";
}
return 0;
}
bool isPalindrome(char *newArr[], int numb) //function to determine true or
false
{
for (int i = 0; i < (numb / 2) + 1; i++) //within the array...
{
if (newArr[i] != newArr[(numb / 2) - i]) //if first index != last
and etc (iterates)
{
palindrome = false;
}
else
{
palindrome = true;
}
}
return palindrome;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.