2017-01-26 1 views
-2

Kompiliert und alles außer Zugabe in einem ‚Schiff‘Class-Objekt wird nicht auf dem Vektor push_back

Constructor für Schiff

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); 
} 

Ich bin sicher, es ist etwas sehr offensichtlich, habe ich das Web durchsucht und nichts hilfreich gefunden. Ich habe nur Schnipsel aus meinem Code genommen, wenn noch etwas benötigt wird, lassen Sie es mich wissen. Danke im Voraus!

/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(); 
} 

Es gibt alle meinen Code

+2

Woher wissen Sie, dass es nicht zum Vektor hinzugefügt wird? Bitte geben Sie eine [MCVE] an. – Kevin

+0

Versucht, eine Bildschirmaufnahme hinzuzufügen, aber ich habe eine ships.size(); (Schiffe sind mein Vektor) Nachdem ich einen hinzugefügt habe, starte ich die Größe und es kommt als 0 auf. Ich hatte es vorher laufen lassen und es würde 1 Buut sagen, wenn ich es versuchen würde, würde der Name und die Show leer sein während die Länge würde Be -803254 – Mikos

+0

Ihr Konstruktor sieht falsch aus. Warum erstellen Sie neue lokale Variablen '_name',' _length' und '_show' anstatt die Klassenmitglieder zu initialisieren? – drescherjm

Antwort

0

ein Objekt auf einen Vektor von Objekten emplace_back() verwenden aufzuwerten und die Konstruktor Argumente übergeben als Argumente zurück in Stellung zu bringen. Es wird dann das Objekt an Ort und Stelle so bauen:

, um Ihr Schiff Objekt an Ort und Stelle zu konstruieren. Ihr Konstruktor ist auch falsch, wie von @Kevin gezeigt. Sie müssen es ändern in:

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

vorausgesetzt, ich habe Ihr Klasse-Design richtig geraten.

EDIT

Versucht ein Minimum Beispiel von diesem zu bauen und das funktioniert gut für mich:

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; 
}; 

und 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; 
} 

Drucke Number of ships = 1. Kannst du es auf so etwas reduzieren?

+0

Dies steht in keinem Zusammenhang mit dem Problem. 'Schiffsschiff (...); ships.push_back (ship); 'sollte äquivalent zu ships.emplace_back (...);' sein. Auch die Initialisierungsliste sollte anstelle von 'this -> __ = __;' verwendet werden. – Kevin

+0

Guter Rat, aber das löst das Problem nicht. Wir brauchen das OP, um eine mcve zu bieten, um eine echte Antwort zu geben – NathanOliver

+0

Alle meine obigen Code hinzugefügt – Mikos

Verwandte Themen