【发布时间】: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