简体   繁体   中英

Class object will not push_back onto vector

Compiles and everything except adding in a 'ship'

Constructor for ship

Ship::Ship(std::string name, int length, std::string show) {

    std::string _name = name;
    int _length = length;
    std::string _show = show;
}    

void Ships::buildShip() {
        std::string name, show;
        int length = 0;
        std::cout << "What is the name of the ship?  ";
        std::cin >> name;

        std::cout << "How long is the ship in feet?  ";
        std::cin >> length;

        std::cout << "What show/movie is the ship from?  ";
        std::cin >> show;
        std::cout << std::endl;
        Ship ship(name, length, show);
        addShip(ship);
}


void Ships::addShip(Ship &ship) {
    ships.push_back(ship);
}

I'm sure it's something very obvious, I've searched the web and found nothing helpful. I only took snippets from my code if anything else is needed let me know. Thanks in advance!

    /Ship.h
#pragma once
#include <string>

class Ship {
    std::string _name;
    int _length;
    std::string _show;

public:
    Ship(){
        std::string name = _name;
        int length = _length;
        std::string show = _show;
    };
    Ship(std::string _name, int _length, std::string _show);
    std::string getName();
    int getLength();
    std::string getShow();

};

    /Ship.cpp
#include <string>
#include "Ship.h"

Ship::Ship(std::string name, int length, std::string show) {

    std::string _name = name;
    int _length = length;
    std::string _show = show;
}

std::string Ship::getName() {
    return _name;
}

int Ship::getLength() {
    return _length;
}

std::string Ship::getShow() {
    return _show;
}

    /Ships.h
#pragma once
#include <vector>
#include "Ship.h"

class Ships {
    std::vector<Ship> ships;
public:
    void addShip(Ship &ship);
    int getCount();
    Ship getLongestShip();
    void buildShip();
    int getNumberOfShipsLongerThan(int input);
    void displayShips();
};

    /Ships.cpp
#include "Ships.h"
#include <iostream>

void Ships::addShip(Ship &ship) {
    ships.push_back(ship);
}


int Ships::getCount() {
    return ships.size();
}

Ship Ships::getLongestShip() {
    Ship longestShip = ships[0];
    for (Ship aShip : ships) {
        if (longestShip.getLength() < aShip.getLength())
            longestShip = aShip;
    }
    std::cout << "The longest ship is the " << longestShip.getName() << std::endl;
    std::cout << "From end to end the length is " << longestShip.getLength() << std::endl;
    std::cout << "The show/movie it is from is " << longestShip.getShow() << std::endl;
    return longestShip;
}

int Ships::getNumberOfShipsLongerThan(int input) {
    int longerThan = 0;
    int size = ships.size();
    for (int i = 0; i < size; i++) {
        if (input < ships[i].getLength())
            longerThan++;
    }
    return longerThan;
}

void Ships::displayShips() {
    std::cout << " Complete Bay Manifest " << std::endl;
    std::cout << "***********************" << std::endl;
    for (int i = 0; i < ships.size(); i++) {
        int a = i + 1;
        std::cout << "Ship (" << a << ")" << std::endl;
        std::cout << "Name: " << ships[i].getName() << std::endl;
        std::cout << "Length: " << ships[i].getLength() << std::endl;
        std::cout << "Show: " << ships[i].getShow() << std::endl<<std::endl;
    }
}

void Ships::buildShip() {
        std::string name, show;
        int length = 0;
        std::cout << "What is the name of the ship?  ";
        std::cin >> name;

        std::cout << "How long is the ship in feet?  ";
        std::cin >> length;

        std::cout << "What show/movie is the ship from?  ";
        std::cin >> show;
        std::cout << std::endl;
        Ship ship(name, length, show);
        addShip(ship);
}

    /driver.cpp
#include <iostream>
#include "Ship.h"
#include "Ships.h"

void menu();
Ship buildShip();
void shipsInBay(Ships &ships);
void processDirective(int choice, Ships &ships);
void longerThan(Ships &ships);

int main() {
    std::cout << "Welcome to Daniel Mikos' Ship Bay!" << std::endl << std::endl;
    menu();
    system("PAUSE");
    return 0;
}

void menu() {
    Ships ships;
    int choice = 0;
        std::cout << "Please make a selection" << std::endl;
        std::cout << " (1) Add a ship to the bay" << std::endl;
        std::cout << " (2) How many ships are already in the bay?" << std::endl;
        std::cout << " (3) Which ship is the longest? " << std::endl;
        std::cout << " (4) Ships longer than ___? " << std::endl;
        std::cout << " (5) Manifest of all ships currently logged" << std::endl;
        std::cout << " (6) Exit" << std::endl;
        std::cout << " Choice: ";
        std::cin >> choice;
        processDirective(choice, ships);
}

Ship buildShip() {
    std::string name, show;
    int length = 0;
    std::cout << "What is the name of the ship?  ";
    std::cin >> name;

    std::cout << "How long is the ship in feet?  ";
    std::cin >> length;

    std::cout << "What show/movie is the ship from?  ";
    std::cin >> show;
    std::cout << std::endl;
    Ship ship(name, length, show);
    return ship;
}

void shipsInBay(Ships &ships) {
    int count = ships.getCount();
    std::cout << std::endl;
    std::cout << "There is currently " << count;
    std::cout << " ship(s) in bay" << std::endl << std::endl;
}

void longerThan(Ships &ships) {
    int input = 0;
    std::cout << "Find ships longer than? ";
    std::cin >> input;
    std::cout << "There are " << ships.getNumberOfShipsLongerThan(input) << "longer than " << input << "feet";
}

void processDirective(int choice, Ships &ships) {
    if (choice == 1)
        buildShip();
    if (choice == 2)
        shipsInBay(ships);
    if (choice == 3)
        ships.getLongestShip();
    if (choice == 4)
        longerThan(ships);
    if (choice == 5)
        ships.displayShips();
    menu();
}

There is all of my code

To add an object to a vector of objects use emplace_back() and pass the constructor arguments as arguments to emplace back. It will then build the object in place so call:

ships.emplace_back(name, length, show);

to construct your ship object in place. Your constructor also is incorrect as pointed out by @Kevin. You need to change it to:

this->_name = name;
this->_length = length;
this->_show = show;

assuming I've guessed your class design correctly.

EDIT

Tried to build a minimum example from this and this works fine for me:

Header.h

class Ship {
    std::string _name;
    int _length;
    std::string _show;

public:
    Ship();
    Ship(std::string _name, int _length, std::string _show);
    std::string getName();
    int getLength();
    std::string getShow();

};

class Ships {
public:

    void addShip(Ship &ship);
    void buildShip();

    std::vector<Ship> ships;
};

and Source.cpp

#include "Header.h"

Ship::Ship(std::string name, int length, std::string show) {

    this->_name = name;
    this->_length = length;
    this->_show = show;
}

void Ships::buildShip() {
std::string name = "Name";
std::string show = "Show";
int length = 0;
ships.emplace_back(name, length, show);
}


int main(int argc, char argv[])
{
    Ships ships;
    ships.buildShip();

    std::cout << "Number of ships = " << ships.ships.size() << std::endl;
    return 0;
}

Prints Number of ships = 1 . Can you slim it down to something like this?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM