【问题标题】:How to fix this: Argument of type "const char*" is incompatible with the parameter of type "char** [duplicate]如何解决此问题:“const char*”类型的参数与“char**”类型的参数不兼容 [重复]
【发布时间】:2023-04-09 05:25:01
【问题描述】:

我对 C++ 有点陌生,目前正在学习计算机工程。我遇到了这个程序的问题。我在main() 函数中遇到错误:

“const char*”类型的参数与“char**”类型的参数不兼容

在这一行:

loan1.setPersonAndLoan(55000.00, 30, 0.04, "Andres Gomez", "(787)654-3423", "56666",
        "Calle Z #456 Fracc Hidalgo");

这是我的全部代码,如果有帮助的话:

主要:

#include "Mortgage.h"

int main() {

    Mortgage loan1;
    loan1.setPersonAndLoan(55000.00, 30, 0.04, "Andres Gomez", "(787)654-3423", "56666",
        "Calle L #456 Fracc Hidalgo");
    loan1.displayInfo();
    loan1.displayLoan();
    Mortgage loan2(loan1);
    loan2.displayInfo();
    loan2.displayLoan();
    system("pause");
    return 0;
}

目标文件:

#include "Mortgage.h"

Mortgage::Mortgage() {
    loan = 3000.0;
    years = 9;
    iRate = 0.09;
    char tempName[20] = "John Doe";
    char tempPhone[20] = "000-000-0000";
    char tempId[20] = "000-00-0000";
    char tempAddress[20] = "Some Place";
    setName(tempName);
    setPhone(tempPhone);
    setId(tempId);
    setAddress(tempAddress);
}
Mortgage::Mortgage(double l, int y, double r, char* n, char* p, char* id1, char* a) {
    setLoan(l);
    setYears(y);
    setIRate(r);
    setName(n);
    setPhone(p);
    setId(id1);
    setAddress(a);
}
//Mortgage::Mortgage(char* n, char* p, char* id1, char* a) {
// setName(n);
// setPhone(p);
// setId(id1);
// setAddress(a);
//}
Mortgage::Mortgage(Mortgage& aMortgage) {
    setLoan(aMortgage.getLoan());
    setYears(aMortgage.getYears());
    setIRate(aMortgage.getIRate());
    setName(aMortgage.getName());
    setPhone(aMortgage.getPhone());
    setId(aMortgage.getId());
    setAddress(aMortgage.getAddress());
}
//==================================================================
Mortgage::~Mortgage() {
    delete[] name;
    delete[] phone;
    delete[] id;
    delete[] address;
}
//================================================================
void Mortgage::setLoan(double l) {
    loan = l;
}
void Mortgage::setYears(int y) {
    years = y;
}
void Mortgage::setIRate(double r) {
    iRate = r; 
}
void Mortgage::setName(char* n) {
    name = new char[strlen(n) + 1];
    strcpy_s(name, strlen(n) + 1, n);
}
void Mortgage::setPhone(char* p) {
    phone = new char[strlen(p) + 1];
    strcpy_s(phone, strlen(p) + 1, p);
}
void Mortgage::setId(char* id1) {
    id = new char[strlen(id1) + 1];
    strcpy_s(id, strlen(id1) + 1, id1);
}
void Mortgage::setAddress(char* a) {
    address = new char[strlen(a) + 1];
    strcpy_s(address, strlen(a) + 1, a);
}
void Mortgage::setPersonAndLoan(double l, int y, double r, char* n, char* p, char* id1, char* a) {
    setLoan(l);
    setYears(y);
    setIRate(r);
    setName(n);
    setPhone(p);
    setId(id1);
    setAddress(a);
}
//=================================================================
double Mortgage::getLoan() const {
    return loan;
}
int Mortgage::getYears() const {
    return years;
}
double Mortgage::getIRate() const {
    return iRate;
}
char* Mortgage::getName() const {
    return name;
}
char* Mortgage::getPhone() const {
    return phone;
}
char* Mortgage::getId() const {
    return id;
}
char* Mortgage::getAddress() const {
    return address;
}
//===================================================================
double Mortgage::getTerm() const {
    double term;
    term = pow((1 + (getIRate() / 100.0) / 12.0), (12 * years));
    return term;
}
double Mortgage::getMonthlyPayment() const {
    double payment;
    payment = (loan * (getIRate() / 100.0) * (getTerm() / 12.0)) / (getTerm() - 1);
    return payment;
}
double Mortgage::getTotalPaid() const {
    double total;
    total = (12 * years * getMonthlyPayment());
    return total;
}
//======================================================================
void Mortgage::displayInfo() const {
    cout << left << setw(20) << getName() << setw(20) << getPhone()
        << setw(30) << getId() << setw(20) << getAddress() << endl;
}
void Mortgage::displayLoan() const {
    cout << left << setw(20) << "\nMonthly Payment" << setw(20) << "Total Payed to Bank"
        << endl;
    cout << left << setw(20) << getMonthlyPayment() << setw(20)
        << getTotalPaid() << endl;
}

标题:

#pragma once

#include <iostream>
#include <cmath>
#include <string>
#include <iomanip>

using namespace std;

class Mortgage {
private:
    double loan, iRate;
    int years;
    char *name,
        *phone,
        *id,
        *address;
public:
    //constructors
    Mortgage();
    Mortgage(double l, int y, double r, char* n, char* p, char* id1, char* a);
    //Mortgage(char*, char*, char* , char*);
    Mortgage(Mortgage &);
    //destructor
    ~Mortgage();
    //setters
    void setLoan(double);
    void setIRate(double);
    void setYears(int);
    void setName(char*);
    void setPhone(char*);
    void setId(char*);
    void setAddress(char*);
    void setPersonAndLoan(double l, int y, double r, char* n, char* p, char* id1, char* a);
    //getters
    double getLoan() const;
    int    getYears() const;
    double getIRate() const;
    char*  getName() const;
    char*  getPhone() const;
    char*  getId() const;
    char*  getAddress() const;

    double getTerm() const;
    double getMonthlyPayment() const;
    double getTotalPaid() const;
    //display
    void displayInfo() const;
    void displayLoan() const;
};

【问题讨论】:

  • 你需要在这里付出一些努力。请阅读minimal reproducible example 是什么,然后创建一个。这样做,无论如何你都可以解决自己的问题。
  • void setPersonAndLoan(double l, int y, double r, char* n, char* p, char* id1, char* a); 此处的所有char*s 和其他调用应更改为const char*,如果您想接受字符串文字。话虽如此,看起来您需要更改许多功能,而不仅仅是这个。这是自 2011 年和 c++11 标准以来的要求。
  • 显而易见的问题是——你为什么不使用std::string 而不是char *

标签: c++ arrays oop compiler-errors char


【解决方案1】:

让我帮你学习钓鱼。你的错误:

argument of type "const char*" is incompatible with the parameter of type "char**"

这告诉您,您有一个“const char*”类型函数的参数,您正试图将其传递给一个需要“char**”类型参数的函数。

你提到它发生在这一行:

loan1.setPersonAndLoan(55000.00, 30, 0.04, "Andres Gomez", "(787)654-3423", "56666",
        "Calle Z #456 Fracc Hidalgo");

所以我立即开始怀疑任何类型为“const char*”的参数(基本上是您的字符串文字 - 它们是“const”,意味着它们无法更改)。

现在我看一下 setPersonAndLoan 的函数声明(此时主体并不重要):

void Mortgage::setPersonAndLoan(double l, int y, double r, char* n, char* p, char* id1, char* a)

而且你确实有一堆 'char *' 参数,而不是 const char,这可能就是发生错误的原因。

也就是说,您发布的错误有“char **”,我想知道这是不是拼写错误?

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-16
    • 2016-05-19
    • 2020-01-20
    • 1970-01-01
    • 2021-01-07
    • 1970-01-01
    • 2014-07-03
    • 2021-08-06
    相关资源
    最近更新 更多