【问题标题】:C++ argument of type * is incompatible with parameter of type *** 类型的 C++ 参数与 ** 类型的参数不兼容
【发布时间】:2017-10-19 04:50:25
【问题描述】:

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

【问题讨论】:

  • 既然是 C++,为什么不直接使用字符串呢?不需要 char 数组,不需要单独发送长度,也不会出现这些错误。在任何情况下修复函数定义。它不需要指向数组的指针,它需要一个指针。

标签: c++


【解决方案1】:

您正在尝试将newArrchar *)传递给isPalindrome()(需要char **)。这就是"'char*' 类型的参数与'char**' 类型的参数不兼容" 的意思。

要解决这个问题,只需传入char **;您可以通过传入newArr 的地址而不是newArr 本身来做到这一点:

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

【讨论】:

  • 这样做会导致未定义的行为,因为代码实际上需要 char* 作为参数。函数定义错误。
  • 是的,OP 需要在 isPalindrome() 中取消引用 newArr 或更改函数定义。
  • 我取消了它的引用,它在在线编译器上工作,但视觉工作室不喜欢它。但它有效:) 谢谢你的建议!
【解决方案2】:

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

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!

$

【讨论】:

    猜你喜欢
    • 2021-07-22
    • 1970-01-01
    • 2018-09-07
    • 1970-01-01
    • 2020-01-20
    • 2020-12-02
    • 1970-01-01
    • 2021-03-08
    • 2012-11-06
    相关资源
    最近更新 更多