簡體   English   中英

C++ 3 個重載中沒有一個可以轉換所有參數類型 line 39 1

[英]C++ none of the 3 overloads could convert all the argument types line 39 1

所以在編碼之后我得到一個錯誤: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;
        }
    }
}
}

//信用報表.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;

};

//CreditStatement.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。

我認為問題是你的 call statement[n] = CreditStatement(item, price);

這里, price是一個 double,但有一個構造函數CreditStatement(char*, double*); 但沒有簽名CreditStatement(char*, double);

你可能想解決這個問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM