类型*的C ++参数与类型**的参数不兼容

时间:2017-10-19 04:50:25

标签: c++

您好我是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;
}

3 个答案:

答案 0 :(得分:2)

您尝试将newArrchar *)传递给isPalindrome()(需要char **)。这就是&#34;类型&#39; char *&#39;的论点与&#39; char **&#39;&#34; 的参数不相容。

要解决此问题,只需传入char **;您可以通过传递newArr而不是newArr本身的地址来执行此操作:

tOf = isPalindrome(&newArr, numb); //calling of function

答案 1 :(得分:1)

<强>简 将函数的函数签名(定义和声明)更改为

bool isPalindrome(char* newArr, int numb);

打电话给它   tOf = isPalindrome(newArr, numb);

<强>详细

如果您致电isPalindrome(newArr, numb)。你要传递第一个元素的地址&amp; 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!

$

答案 2 :(得分:-2)

只需在 L"CHARACTER" 等字符前使用 L