类对象不会 push_back 到向量上
Posted
技术标签:
【中文标题】类对象不会 push_back 到向量上【英文标题】:Class object will not push_back onto vector 【发布时间】:2017-01-26 17:33:58 【问题描述】:编译和除了添加“船”之外的所有内容
船的构造函数
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);
我确定这很明显,我在网上搜索并没有发现任何有用的信息。如果需要其他任何内容,我只从我的代码中获取 sn-ps 让我知道。提前致谢!
/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();
我所有的代码都有
【问题讨论】:
你怎么知道它没有添加到向量中?请提供minimal reproducible example。 尝试添加屏幕截图,但我有一个 ship.size(); (船是我的矢量)在我添加一个之后,我运行大小,它出现为 0。我之前运行过它,当我尝试显示它时它会说 1 buuut,名称和显示将为空白,而长度会是-803254 你的构造函数看起来不对。为什么要创建新的局部变量_name
、_length
和 _show
而不是初始化类成员?
@Mikos 没有 minimal reproducible example 我们可以自己运行我们不知道您如何确定您的代码不起作用。也就是说,drescherjm 是正确的,Ship
的构造函数是错误的。您正在为我只能假设应该是数据成员的内容创建局部变量。
Ships 应该是一个“包装”类。构造函数就是这样,所以我可以访问 Ship 的私有成员
【参考方案1】:
要将对象添加到对象向量中,请使用 emplace_back()
并将构造函数参数作为参数传递给 emplace。然后它将在适当位置构建对象,因此调用:
ships.emplace_back(name, length, show);
在适当的位置构建您的船对象。正如@Kevin 所指出的,您的构造函数也不正确。您需要将其更改为:
this->_name = name;
this->_length = length;
this->_show = show;
假设我猜对了你的班级设计。
编辑
试图从中构建一个最小示例,这对我来说很好:
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;
;
和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;
打印Number of ships = 1
。你能把它瘦成这样吗?
【讨论】:
这与问题无关。Ship ship(...); ships.push_back(ship);
应该等同于 ships.emplace_back(...);
。也应该使用初始化列表而不是this->__ = __;
。
好建议,但这并不能解决问题。我们需要 OP 提供一个 mcve 来给出真正的答案
公平的cmets,我刚刚看到了刚才发布的其余代码,所以会再看一下。
@CodingLumis 谢谢你的帮助,我已经按照你上面写的那样做了所有的改变。仍然无法正常工作...我会继续尝试
仍然无法正常工作 我建议您学习使用调试器逐行执行代码。这样你就可以看到你的代码没有像你期望的那样运行的确切点。以上是关于类对象不会 push_back 到向量上的主要内容,如果未能解决你的问题,请参考以下文章