两个代码;相同的逻辑和代码行,一个可行,但其他却不行

时间:2019-07-05 23:13:13

标签: c++ string visual-studio vector

几个小时前,我问了this question;我想看看是否有人现在可以解释这个问题。

一个代码是关于在杂货店中分离物品的;最后,您将有两(2)个袋子; fragileBagnormalBag

其他代码根据旅客去接送的办公室来分隔乘客;最后,您将拥有三(3)种类型的乘客;进入rio的人,前往maya的人以及在其他地方提出要求的人。

两种代码都使用相同的逻辑,但是乘客代码在与杂货店代码完全匹配的一行上给出了错误。

请注意,两个代码都返回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'无效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 个答案:

答案 0 :(得分:2)

要解释为什么第二个代码不起作用,我首先必须解释为什么第一个代码似乎起作用。

myBag::myBag(string anItemName)

可以用string制成袋子。它是Conversion Constructor。所以当

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

已编译,编译器悄悄地插入对myBag(string)构造函数的调用,您会得到类似

的信息。
fragileBag.push_back(myBag(newMyVector[x].getItem()));

这在逻辑上没有意义。它说将一个袋子中的一个项目变成一个带有袋子的袋子,然后将这个新袋子插入另一个袋子fragileBag

更仔细地观察myBag时,您会发现它根本不是一个包。它是单个项目,应重命名为myItem或一起丢弃,以支持全新的全不同myBag,它是vector的{​​{1}}的包装},其中string代表项目。这使得

string

真正的包包

换句话说,工作代码起作用的唯一原因是它实际上并没有按照命名所暗示的那样工作。该代码可以编译并产生预期的结果,但是在语义上存在问题。

这导致与

的混淆
myBag fragileBag;

rioLoc.push_back(newMyVector[x].getXoLoc()); rioLoc,只能容纳vector<trans>。没有trans可以将trans::trans(string)转换为string,因此暴露了杂货店代码的错误逻辑。由于杂货袋和物品已经交织在一起,因此旅客和运输在这里结合在一起。

上述针对杂货店的解决方法相对简单。旅客将需要一个稍有不同的解决方案,既可以使用trans类来描述旅客,也可以使用passenger类来描述运输方式。 transport将有一个transport成员来容纳其乘客以及添加和删除乘客的方法,并可能通过簿记来跟踪运输工具的位置,问题未完全指定细节。 >

答案 1 :(得分:0)

这两个代码都将html tags的值压入不包含string值的vector

您的杂货店代码使用string个对象的向量。该代码之所以有效,是因为myBag具有一个非显式构造函数,该构造函数采用单个myBag作为输入,因此编译器能够隐式构造一个临时string对象以推入向量。 / p>

您的乘客代码使用myBag个对象的向量。代码失败是因为trans没有一个以单个trans作为输入的构造函数,因此编译器无法构造临时string来插入向量。