C ++没有合适的构造函数可供转换

时间:2015-02-23 21:37:33

标签: c++ arrays concatenation

我们为程序提供了一个模板并完成它,这是我们的Main.cpp,在grocery[n++] = Grocery(thisName, no);行我们认为它试图将itemname(thisName)和数字(no)连接成一个数组,但我们得到一个错误,不确定如何将两种不同类型的输入(string + int)输入到一个数组中,以获得如下所示的输出:

Grocery List
Number              Name
--------------------------------------
 Cheese - Cheddar   1002
 Fish - Cod         1011
 Spinach            2374
 Peppercorns        2953
 Cream              8543

Main.cpp的

#include <iostream>
#include <cstring>
using namespace std;
#include "Main.h"
#include "Grocery.h"
void sort(Grocery* grocery, int n);
int main() {
    int no, n = 0;
    Grocery grocery[MAX_GROCERIES];
    cout << "Grocery List Processor\n";
    cout << "======================\n";
    do {
        cout << " Grocery number (0 to quit) ? ";
        cin >> no;
        if (cin.fail() || (cin.get() != '\n')) {
            cin.ignore(2000, '\n');
            cerr << "Bad character. Try again." << endl;
            cin.clear();
        }
        else if (no != 0) {
            cout << " Grocery name ? ";
            char thisName[MAX_LINE_LENGTH + 1];
            cin.getline(thisName, MAX_LINE_LENGTH);
            if (strlen(thisName) > 0)
                    grocery[n++] = Grocery(thisName, no);

        }
    } while (no != 0 && n < MAX_GROCERIES);
    cout << endl;
    sort(grocery, n);
    cout << " Grocery List\n\n";
    cout << " Name Number\n";
    cout << "------------------------------\n";
    for (int i = 0; i < n; i++) {
        grocery[i].display();
    }
    cout << endl;
}
// sort sorts the elements of Grocery[n] in ascending order
//
void sort(Grocery* grocery, int n) {
    int i, j;
    Grocery temp;
    for (i = n - 1; i > 0; i--) {
        for (j = 0; j < i; j++) {
            if (grocery[j].isGreaterThan(grocery[j + 1]))
            {
                temp = grocery[j];
                grocery[j] = grocery[j + 1];
                grocery[j + 1] = temp;
            }
        }
    }
}

grocery.cpp

#include <iostream>
#include <iomanip>
#include "Grocery.h"


using namespace std;

void Grocery::display() const{
        char* grocItem = nullptr;
        int prodID = '\0';

}
void Grocery::display(char *, int){
    grocItem = "";
    prodID = 15;

}

编辑:--------------------------------

Grocery::Grocery(const char *name, const int ID, const int num){
    grocItem = *name;
    prodID = ID;
    numTimes = num;

}

Grocery& Grocery::operator=(const Grocery &rhs){

    grocerylist = new char[numTimes];
    for (int i = 0; i < numTimes; i++){
        grocerylist[i] = grocItem + prodID;
    }
    return *this;
}

这是我们的赋值运算符和我们的标准重载构造函数,但Visual Studio给出了一个奇怪的错误:

Error   2   error LNK2019: unresolved external symbol "public: class Grocery & __thiscall Grocery::operator=(class Grocery const &)" (??4Grocery@@QAEAAV0@ABV0@@Z) referenced in function "void __cdecl sort(class Grocery *,int)" (?sort@@YAXPAVGrocery@@H@Z)


Error   3   error LNK2019: unresolved external symbol "public: bool __thiscall Grocery::isGreaterThan(class Grocery const &)const " (?isGreaterThan@Grocery@@QBE_NABV1@@Z) referenced in function "void __cdecl sort(class Grocery *,int)" (?sort@@YAXPAVGrocery@@H@Z)  C:\Users\Andrew\Documents\Visual Studio 2013\Projects\Workshop5\Workshop5\Main.obj  Workshop5

以及其他一些奇怪的......

0 个答案:

没有答案