gpt4 book ai didi

C++ 3 个重载都不能转换所有参数类型第 39 行 1

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:55 25 4
gpt4 key购买 nike

所以在编码之后我得到了一个错误:C++ 3 个重载中的任何一个都不能转换 w5.cpp 中的所有参数类型第 39 1 行你知道问题出在哪里吗?你能帮我修一下吗?我实际上不知道为什么会显示这个,因为我得到了这段代码的默认构造函数。

//w5.h

 #define MAX_LINE_LENGTH 256
#define MAX_PURCHASES 5

//w5.cpp

#include <iostream>
#include <cstring>
#include "w5.h"
#include "CreditStatement.h"

using namespace std;

void sort(CreditStatement* statement, int n);

int main()
{
double price;
int n = 0;
CreditStatement statement[MAX_PURCHASES];

cout << "Credit Statement Processor\n";
cout << "==========================\n";

do
{
cout << "Item price (0 to quit): ";
cin >> price;
if (cin.fail() || (cin.get() != '\n'))
{
cin.ignore(2000, '\n');
cerr << "Bad character. Try again." << endl;
cin.clear();
}
else if ((int)price != 0)
{
cout << "Statement item: ";
char item[MAX_LINE_LENGTH];
cin.getline(item, MAX_LINE_LENGTH);
if (strlen(item) > 0)
{
statement[n] = CreditStatement(item, price);
n++;
}
}
} while ((int)price != 0 && n < MAX_PURCHASES);

cout << endl;

sort(statement, n);

cout << " Credit Statement\n\n";
cout << " Item Price\n";
cout << "----------------------------------\n";

for (int i = 0; i < n; i++)
{
statement[i].display();
}
cout << endl;

return 0;
}

// sort sorts the elements of Credit Card Statement[n] in ascending order
//
void sort(CreditStatement* s, int n)
{
int i, j;
CreditStatement temp;

for (i = n - 1; i > 0; i--)
{
for (j = 0; j < i; j++)
{
if (s[j].isGreaterThan(s[j + 1]))
{
temp = s[j];
s[j] = s[j + 1];
s[j + 1] = temp;
}
}
}
}

//CreditStatement.h

class CreditStatement{
bool _valid;
double* _price;
char* _item;

public:
CreditStatement();
CreditStatement(char*, double*);
CreditStatement(const CreditStatement&);
CreditStatement& operator=(const CreditStatement&);

//output
void display() const;

//mutators
bool isGreaterThan(const CreditStatement&) const;

};

//信用声明.cpp

#include <iostream>
#include <new>
#include "CreditStatement.h"
using namespace std;


void CreditStatement::display() const{
cout << " Something" << _price << _item;



}


bool CreditStatement::isGreaterThan(const CreditStatement&) const{
return _valid;
}



CreditStatement::CreditStatement(){
_item = NULL;
_price = NULL;
}

CreditStatement::CreditStatement(char* iP, double* pP){
_price = NULL;
_item = NULL;

if (pP != NULL){

int sizepP = sizeof(pP) / sizeof(pP[0]);
_price = new (nothrow) double[sizepP];

if (_price){
for (int i = 0; i <sizepP; i++){
_price[i] = pP[i];
};

}


if (iP != NULL){

int sizeiP = sizeof(iP) / sizeof(iP[0]);
_item = new (nothrow) char [sizeiP];
if (_item){
for (int i = 0; i < sizeiP; i++){
_item[i] = iP[i];
};
}
}


}

}


CreditStatement::CreditStatement(const CreditStatement& otherCS){
*this = CreditStatement(otherCS._item, otherCS._price);
}

CreditStatement& CreditStatement::operator=(const CreditStatement& otherCS){
if (this != &otherCS)
{
if (_item){
delete[] _item;
_item = NULL;
}
if (_price){
delete[] _price;
_price = NULL;
}

else{

if (otherCS._price != NULL){

int sizepP = sizeof(otherCS._price) / sizeof(otherCS._price[0]);
_price = new (nothrow) double[sizepP];
if (_price){
for (int i = 0; i < sizepP; i++){
_price[i] = otherCS._price[i];
};

}


if (otherCS._item != NULL){

int sizeiP = sizeof(otherCS._item) / sizeof(otherCS._item[0]);
_item = new (nothrow) char[sizeiP];
if (_item){
for (int i = 0; i < sizeiP; i++){
_item[i] = otherCS._item[i];
};
}
}
}
}
}
return *this;
}

我也遇到了这个错误

“构造函数“CreditStatement::CreditStatement”的实例与参数列表不匹配 参数类型为:(char [256], double) c:*\Project1\w5.cpp 38 20.

最佳答案

我认为问题出在你的调用 statement[n] = CreditStatement(item, price);

这里,price 是一个 double,但是有一个构造函数 CreditStatement(char*, double*); 但没有一个带有签名 CreditStatement(char*, double );

您可能想要解决这个问题。

关于C++ 3 个重载都不能转换所有参数类型第 39 行 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30880915/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com