Два кода; та же логика и строка кода, одна работает, а другая нет - PullRequest
0 голосов
/ 06 июля 2019

Я задал этот вопрос пару часов назад; Я хочу посмотреть, сможет ли кто-нибудь объяснить проблему.

Один код касается разделения продуктов в продуктовом магазине; в итоге у вас будет две (2) сумки; fragileBag и normalBag.

Другой код отделяет пассажиров в зависимости от офиса, в который они отправляются для получения; в итоге у вас будет три (3) типа пассажиров; те, которые идут в Рио, те, которые идут в майю, и те, которые просят в другом месте

Оба кода используют одну и ту же логику, но код пассажира выдает ошибку в строке, которая отлично работает с кодом бакалеи.

Просто чтобы прояснить, ОБА КОДЫ ВОЗВРАЩАЮТ ЗНАЧЕНИЯ STRING.

ОШИБКА ОТ ПАССАЖИРСКОГО КОДА:

Ошибка (активная) E0304 нет экземпляра перегруженной функции "std :: vector <_Ty, _Alloc> :: push_back [with _Ty = trans, _Alloc = std :: allocator ]" соответствует списку аргументов dataPractice2 C: \ Users \ javye \ source \ repos \ dataPractice2 \ dataPractice2 \ main.cpp 82

а также:

Ошибка C2664 'void std :: vector > :: push_back (_Ty &&)': невозможно преобразовать аргумент 1 из 'std :: string' в 'const _Ty &' dataPractice2 c : \ users \ javye \ source \ repos \ datapractice2 \ datapractice2 \ main.cpp 82

// ФУНКЦИЯ Бакалейной лавки

//separate function
void separateItems(vector<myBag>& newMyVector) {
    for (int x = newMyVector.size() - 1; x >= 0; --x) {
        if (newMyVector[x].getItem() == "eggs" || newMyVector[x].getItem() == "bread") {
            fragileBag.push_back(newMyVector[x].getItem()); //NO PROBLEM HERE
            newMyVector.pop_back();
        }
        else {
            normalBag.push_back(newMyVector[x].getItem()); //OR HERE
            newMyVector.pop_back();
        }
    }
}

// ПАССАЖИРСКАЯ ФУНКЦИЯ

//separate function
void separateP(vector<trans>& newMyVector) {
    for (int x = newMyVector.size() - 1; x >= 0; --x) {
        if (newMyVector[x].getXoLoc() == "rio") {
            rioLoc.push_back(newMyVector[x].getXoLoc()); //PROBLEM HERE
            newMyVector.pop_back();
        }
        else
            if (newMyVector[x].getXoLoc() == "maya") {
                mayaLoc.push_back(newMyVector[x].getXoLoc()); //HERE
                newMyVector.pop_back();
            }
            else
                elseLoc.push_back(newMyVector[x].getXoLoc()); //HERE
                newMyVector.pop_back();
    }
}

// ПОЛНЫЙ КОД ПРОДУКТОВ

//HEADER
#pragma once
#include<iostream>
#include<vector>
#include<string>
using namespace std;

#ifndef BAG_H
#define BAG_H
class myBag {
public:
    myBag(); //default constructor
    myBag(string anItemName); //overload constructor
    void addItem(string anItemName); //mutator
    string getItem();//accessor

private:
    string itemName;

};
#endif

//SOURCE
#include"bag.h"

myBag::myBag() {
    addItem("");
}

myBag::myBag(string anItemName) {
    addItem(anItemName);
}

void myBag::addItem(string anItemName) {
    itemName = anItemName;
}

string myBag::getItem() {
    return itemName;
}

//MAIN
#include"bag.h"

void inputItems(vector<myBag>&); //input data function prototype
void displayQuantity(vector<myBag>&); //display data function prototype
void separateItems(vector<myBag>&); //function that separates items; func prototype
void fragBag(vector<myBag>&); //fragile bag function prototype
void norBag(vector<myBag>&); //normal bag function prototype

vector<myBag> myVector; //main vector
vector<myBag> fragileBag, normalBag; //seconday vectors
string item; //global item variable

int main() {
    int option;
    try {
        do {
            cout << "\tMENU"
                << endl << "1) Input Items"
                << endl << "2) Display Quantity"
                << endl << "3) Separate (IMPORTANT)"
                << endl << "4) Display Items in Fragile Bag"
                << endl << "5) Display Items in Normal Bag"
                << endl << "6) Exit Program"
                << endl << endl << "Choose: ";
            cin >> option;

            if (option > 6) {
                throw 404;
            }

            switch (option) {
            case 1: //input
                system("cls");
                inputItems(myVector);
                system("pause");
                system("cls");
                break;
            case 2://display
                system("cls");
                displayQuantity(myVector);
                system("pause");
                system("cls");
                break;
            case 3: //separate
                system("cls");
                separateItems(myVector);
                system("pause");
                system("cls");
                break;
            case 4: //fragile
                system("cls");
                fragBag(myVector);
                system("pause");
                system("cls");
                break;
            case 5: //normal
                system("cls");
                norBag(myVector);
                system("pause");
                system("cls");
                break;
            case 6: //exit
                exit(0);
            }

        } while (option != 6);
    }
    catch(int x){
        cout << "ERROR, OPTION DOESN'T EXITS" << endl;
        system("pause");
    }

}
//input function
void inputItems(vector<myBag>& newMyVector) {
    do {
        cout << "Enter grocery items || enter letter X to stop: ";
        cin >> item;
        if (item != "x")
            newMyVector.push_back(myBag(item));

    } while (item != "x");
}
//display function
void displayQuantity(vector<myBag>& newMyVector) {
    try {
        for (int x = 0; x < newMyVector.size(); ++x) {
            if (x == 0) {
                cout << "Store bag has " << newMyVector.size() << " items in it. These are: " << endl;
            }

            cout << newMyVector[x].getItem() << endl;
        }

        if (newMyVector.empty())
            throw 404;
    }
    catch (int x) {
        cout << "ERROR " << x << " ,QUANTITY NOT FOUND" << endl;
    }
}
//separate function
void separateItems(vector<myBag>& newMyVector) {
    for (int x = newMyVector.size() - 1; x >= 0; --x) {
        if (newMyVector[x].getItem() == "eggs" || newMyVector[x].getItem() == "bread") {
            fragileBag.push_back(newMyVector[x].getItem()); //PROBLEM WOULD APPEAR HERE, BUT DOESN'T, UNLIKE THE OTHER CODE
            newMyVector.pop_back();
        }
        else {
            normalBag.push_back(newMyVector[x].getItem());
            newMyVector.pop_back();
        }
    }
}
//fragile bag function
void fragBag(vector<myBag>& newMyVector) {
    try {
        for (int x = 0; x < fragileBag.size(); ++x) {
            if (x == 0) {
                cout << "The fragile bag has " << fragileBag.size() << " items in it. These are: " << endl;
            }
            cout << fragileBag[x].getItem() << endl;
        }
        if (fragileBag.empty()) {
            throw 404;
        }
    }
    catch (int x) {
        cout << "ERROR " << x << " ,FRAGILE BAG EMPTY" << endl;
    }
}
//normal bag function
void norBag(vector<myBag>& newMyVector) {
    try {
        for (int x = 0; x < normalBag.size(); ++x) {
            if (x == 0) {
                cout << "The normal bag has " << normalBag.size() << " items in it. These are: " << endl;
            }
            cout << normalBag[x].getItem() << endl;
        }

        if (normalBag.empty()) {
            throw 404;
        }
    }
    catch (int x) {
        cout << "ERROR " << x <<" , NORMAL BAG EMPTY" << endl;
    }
}

// ПАССАЖИРСКИЙ ПОЛНЫЙ КОД

//HEADER
#pragma once
#include<iostream>
#include<vector>
#include<string>
using namespace std;

#ifndef TRANSPORT_H
#define TRANSPORT_H

class trans {
public:
    trans();
    trans(string aName, string anXoLoc, string anXfLoc, string aTime, string aCellNum);
    void setName(string aName);
    void setXoLoc(string anXoLoc);
    void setXfLoc(string anXfLoc);
    void setTime(string aTime);
    void setCellNum(string aCellNum);
    string getName();
    string getXoLoc();
    string getXfLoc();
    string getTime();
    string getCellNum();

private:
    string name;
    string xoLoc; //offices
    string xfLoc; //destination
    string time;
    string cellNum;
};
//SOURCE
#include"transport.h"

trans::trans() {
    setName("");
    setXoLoc("");
    setXfLoc("");
    setTime("");
    setCellNum("");
}

trans::trans(string aName, string anXoLoc, string anXfLoc, string aTime, string aCellNum) {
    setName(aName);
    setXoLoc(anXoLoc);
    setXfLoc(anXfLoc);
    setTime(aTime);
    setCellNum(aCellNum);
}

void trans::setName(string aName) {
    name = aName;
}

void trans::setXoLoc(string anXoLoc) {
    xoLoc = anXoLoc;
}

void trans::setXfLoc(string anXfLoc) {
    xfLoc = anXfLoc;
}

void trans::setTime(string aTime) {
    time = aTime;
}

void trans::setCellNum(string aCellNum) {
    cellNum = aCellNum;
}

string trans::getName() {
    return name;
}

string trans::getXoLoc() {
    return xoLoc;
}

string trans::getXfLoc() {
    return xfLoc;
}

string trans::getTime() {
    return time;
}

string trans::getCellNum() {
    return cellNum;
}

#endif

//MAIN
#include"transport.h"

void inputInfo(vector<trans> &);
void displayInput(vector<trans>&);
void separateP(vector<trans>&);
void rio(vector<trans>&);
void maya(vector<trans>&);
void elsewhere(vector<trans>&);


vector<trans> myVector;
vector<trans> rioLoc, mayaLoc, elseLoc;

string newName;
string newXoLoc; //offices
string newXfLoc; //destination
string newTime;
string newCellNum;

//main not ready. Creating each function one by one to them make it look nice
int main() {
    int option;
    do {
        cout << "MENU"
            << endl << "1) input "
            << endl << "2) output "
            << endl << "3) separate"
            << endl << "4) rio passengers"
            << endl << "5) maya passengers"
            << endl << "6) elsewhere passengers";
        cin >> option;

        switch(option){
        case 1:
            inputInfo(myVector);
            break;
        case 2:
            displayInput(myVector);
            break;
        case 3:
            separateP(myVector);
            break;
        case 4:
            rio(myVector);
            break;
        case 5:
            maya(myVector);
            break;
        case 6:
            elsewhere(myVector);
            break;
        case 7:
            exit(0);
        }
    } while (option != 7);
    system("pause");
}

void inputInfo(vector<trans> &newMyVector) {
    int charSize;

    cout << "How many passangers to register: ";
    cin >> charSize;

    for (int x = 0; x < charSize; ++x) {
        cout << "Name of passanger: ";
        cin >> newName;

        cout << "Office: ";
        cin >> newXoLoc;

        cout << "Destination: ";
        cin >> newXfLoc;

        cout << "Time of pickup: ";
        cin >> newTime;

        cout << "Cellphone: ";
        cin >> newCellNum;

        if (charSize != 0)
            newMyVector.push_back(trans(newName, newXoLoc, newXfLoc, newTime, newCellNum));
    }
}

void displayInput(vector<trans>& newMyVector) {
    for (int x = 0; x < newMyVector.size(); ++x) {
        if (x == 0) {
            cout << "There are " << newMyVector.size() << " passengers. These are: " << endl;
        }
        cout << "-----------------------------Passenger #" << x + 1 << endl;
        cout << newMyVector[x].getName() << endl;
        cout << newMyVector[x].getXoLoc() << endl;
        cout << newMyVector[x].getXfLoc() << endl;
        cout << newMyVector[x].getTime() << endl;
        cout << newMyVector[x].getCellNum() << endl;
    }
}

void separateP(vector<trans>& newMyVector) {
    for (int x = newMyVector.size() - 1; x >= 0; --x) {
        if (newMyVector[x].getXoLoc() == "rio") {
            rioLoc.push_back(newMyVector[x]);
            newMyVector.pop_back();
        }
        else
            if (newMyVector[x].getXoLoc() == "maya") {
                mayaLoc.push_back(newMyVector[x]);
                newMyVector.pop_back();
            }
            else
                elseLoc.push_back(newMyVector[x]);
                newMyVector.pop_back();
    }
}

void rio(vector<trans>& newMyVector) {
    for (int x = 0; x < rioLoc.size(); ++x) {
        if (x == 0) {
            cout << "Num. of passangers to pickup in Rio Piedras is " << rioLoc.size() << " , these are: " << endl;
        }
        cout << rioLoc[x].getName() << endl;
        cout << rioLoc[x].getXoLoc() << endl;
        cout << rioLoc[x].getXfLoc() << endl;
        cout << rioLoc[x].getTime() << endl;
        cout << rioLoc[x].getCellNum() << endl;
    }
}

void maya(vector<trans>& newMyVector) {
    for (int x = 0; x < mayaLoc.size(); ++x) {
        if (x == 0) {
            cout << "Num. of passangers to pickup in Mayaguez is " << mayaLoc.size() << " , these are: " << endl;
        }
        cout << mayaLoc[x].getName() << endl;
        cout << mayaLoc[x].getXoLoc() << endl;
        cout << mayaLoc[x].getXfLoc() << endl;
        cout << mayaLoc[x].getTime() << endl;
        cout << mayaLoc[x].getCellNum() << endl;
    }
}

void elsewhere(vector<trans>& newMyVector) {
    for (int x = 0; x < elseLoc.size(); ++x) {
        if (x == 0) {
            cout << "Num. of passangers to pickup in elsewhere is " << elseLoc.size() << " , these are: " << endl;
        }
        cout << elseLoc[x].getName() << endl;
        cout << elseLoc[x].getXoLoc() << endl;
        cout << elseLoc[x].getXfLoc() << endl;
        cout << elseLoc[x].getTime() << endl;
        cout << elseLoc[x].getCellNum() << endl;
    }
}

Ответы [ 2 ]

2 голосов
/ 06 июля 2019

Чтобы объяснить, почему второй код не работает, я сначала должен объяснить, почему первый код работает.

myBag::myBag(string anItemName)

может сделать сумку из string.Это Конструктор преобразования .Поэтому, когда

fragileBag.push_back(newMyVector[x].getItem());

компилируется, компилятор незаметно вставляет вызов в конструктор myBag(string), и вы получаете что-то более похожее на

fragileBag.push_back(myBag(newMyVector[x].getItem()));

, что не имеет смысла логически.Там написано, что нужно превратить предмет в сумке в сумку с одним предметом и вставить эту новую сумку в еще одну сумку, fragileBag.

Когда вы посмотрите на myBag более внимательно, вы увидите, что это не так.Сумка вообще.Это отдельный элемент, который должен быть переименован в myItem или полностью отброшен в пользу совершенно нового myBag, который является оберткой вокруг vector из string, где string s представляютПредметы.Это делает

myBag fragileBag;

настоящей сумкой.

Другими словами, единственная причина, по которой работает рабочий код, состоит в том, что он фактически не делает то, что подразумевает присвоение имен.Код компилируется и выдает ожидаемый результат, но семантически затруднен.

Это приводит к путанице с

rioLoc.push_back(newMyVector[x].getXoLoc());

rioLoc, равным vector<trans> и может содержать только trans,Нет trans::trans(string), чтобы преобразовать string в trans, чтобы раскрыть ошибочную логику продуктового кода.Так как сумка и предмет переплетены в продуктовом магазине, пассажиры и транспорт объединяются здесь.

Описанное выше решение для бакалеи относительно просто.Пассажиру понадобится немного другое решение с классом passenger для описания пассажиров и классом transport для описания транспортных средств.У transport будет vector<passenger> член, который будет содержать своих пассажиров, а также методы добавления и удаления пассажиров и, возможно, бухгалтерию для отслеживания местоположения транспорта, детали, не полностью указанные в вопросе.

0 голосов
/ 06 июля 2019

Оба кода помещают string значения в vector, который не содержит string значений.

Ваш продуктовый код использует вектор myBag объектов.Код работает, потому что myBag имеет неявный конструктор, который принимает один string в качестве входных данных, поэтому компилятор может неявно создать временный объект myBag для вставки в вектор.

Вашпассажирский код использует вектор trans объектов.Код не выполняется, потому что trans не имеет конструктора, который принимает один string в качестве входных данных, поэтому компилятор не может создать временный trans для вставки в вектор.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...