C++ 无法理解为什么我的向量不';t在函数中保留赋值

C++ 无法理解为什么我的向量不';t在函数中保留赋值,c++,vector,C++,Vector,我一直在做一个程序,我需要使用我在开始时创建的fish对象(我在main中运行一个名为“init”的函数),它创建我的fish对象并将它们添加到一个向量中。此后,我有另一个函数,它获取一个随机生成的数字,并将它们与我的鱼类数据库中的数字进行比较,从而确定捕获了哪条鱼。问题是,无论我做什么,我都无法访问我在函数外部创建的fish对象。该函数在expedition函数启动时被调用,它应该运行算法并计算所有内容 编辑:我添加了我制作的每一个类。对不起,没有事先做,我认为没有必要。谢谢大家花时间尝试破解

我一直在做一个程序,我需要使用我在开始时创建的fish对象(我在main中运行一个名为“init”的函数),它创建我的fish对象并将它们添加到一个向量中。此后,我有另一个函数,它获取一个随机生成的数字,并将它们与我的鱼类数据库中的数字进行比较,从而确定捕获了哪条鱼。问题是,无论我做什么,我都无法访问我在函数外部创建的fish对象。该函数在expedition函数启动时被调用,它应该运行算法并计算所有内容

编辑:我添加了我制作的每一个类。对不起,没有事先做,我认为没有必要。谢谢大家花时间尝试破解这个

Fish.cpp

#include <iostream>
#include "Fish.h"

Fish::Fish(std::string name){
    this->name=name;
}

Fish::Fish(std::string name, int score, int id){
    this->name = name;
    this->score = score;
    this->id = id;
}

int Fish::GetScore() {
    return this->score;
}

std::string Fish::getName() {
    return this->name;
}

Fish sprat("Sprat", 900, 1);
Fish mackerel("Mackerel", 950, 2);
Fish shark("Great White Shark", 990, 3);
Fish treasure("Pirate treasure", 1000, 4);

void Fish::addToList(Fish fish){
    fishTypes.emplace_back(fish);

}

void Fish::init(){

    Fish fish("testas");

    fish.addToList(sprat);
    fish.addToList(mackerel);
    fish.addToList(shark);
    fish.addToList(treasure);

    fish.display();


}

void Fish::display() {
    for(Fish f: fishTypes){
        std::cout << f.GetScore()<<"\n";
    }
}
void Fish::SetValue(int x) {

    unsigned int taskai = fishTypes.at(x).GetScore();

    this->score = taskai;

}

void Fish::toString() {
    std::cout << "This fish is called " <<
              this->name << " and is valued at " <<
              this->score << " points\n";
}
Ship.cpp

//
// Created by Jerome on 2021-02-10.
//
#include <iostream>
#include "Ship.h"

std::string Ship::GetName() {
    return this->name;
}

int Ship::GetStorage() {
    return this->storage;
}

void Ship::SetName(std::string name) { this->name = name; }

void Ship::SetStorage(int storage) { this->storage = storage; }

void Ship::setAll(std::string name, int storage) {
    this->name = name;
    this->storage = storage;
}

Ship::Ship(std::string name, int storage) {
    this->name = name;
    this->storage = storage;
}

Ship::Ship() {
    this->name = "Stock Ship";
    this->storage = 10;
}

void Ship::toString() {
    std::cout << "This ship is called " <<
              this->name << " and has the storage capacity of " <<
              this->storage << " units\n";
}
//
//由杰罗姆于2021-02-10创建。
//
#包括
#包括“Ship.h”
std::string Ship::GetName(){
返回此->名称;
}
int Ship::GetStorage(){
返回此->存储;
}
void Ship::SetName(std::string name){this->name=name;}
void Ship::SetStorage(int存储){this->storage=storage;}
void Ship::setAll(std::字符串名称,int存储){
此->名称=名称;
这个->存储=存储;
}
Ship::Ship(std::字符串名称,int存储){
此->名称=名称;
这个->存储=存储;
}
Ship::Ship(){
此->name=“库存船舶”;
这个->存储=10;
}
void Ship::toString(){
我猜(不跑步)你的

void Fish::SetValue(int x) {
    unsigned int taskai = fishTypes.at(x).GetScore();

    this->score = taskai;
}
调用
rabarbaras.SetValue(1);

基线错误是构造函数中的
rabarbaras
未调用
ìnit

如果
fishTypes
对所有
Fish
fishTypes
应该是相同的,那么对
Init
只调用一次就足够了


另外,您可以通过插入打印的二进制搜索或通过将调试器设置为在抛出时中断(或调试器中的任何调用)来解决这个问题.

您能否开发«我无法访问我在函数外部创建的fish对象»?船的类别在哪里?探险的标题在哪里?请发布最小的代码…抱歉,我不理解问题
fish::fish(std::string name)
让一些字段未初始化。
int-zuvys[存储]无效的C++(VLA扩展名)为<代码>存储>代码>是运行时值。我添加了丢失的类并更改了存储名。谢谢您的回复!不幸的是,不是这样。对象只是为了检查
SetValue
函数是否工作。问题是,从技术上讲,所有代码都按原样运行,但当我尝试从我相信的vector读取时,程序终止。它会引发如下异常:
terminate在抛出'std::out_of_range'what()的实例后调用:vector::_M_range_check:__n(0)>=this->size()(0)
这让我得出了这个结论。我仍在努力解决这个问题。我没有太多的编码经验,所以我似乎无法完成我的工作wrong@JeronimasKlevinskas你有没有试着在你怀疑发生错误的每一行之前和之后打印?
//
// Created by Jerome on 2021-02-10.
//

#ifndef UOSTASGAME_EXPEDITION_H
#define UOSTASGAME_EXPEDITION_H

#include <iostream>
#include <cstdlib>
#include <iostream>
#include "Ship.h"

class Expedition {

private:

public:
    static void StartExpedition(Ship ship);
    static int Fishing();
};


#endif //UOSTASGAME_EXPEDITION_H
int main() {

    srand(time(0));

    Ship ship("Becky",10);
    Ship a;

    ship.toString();
    a.toString();

Expedition::StartExpedition(ship);

    Fish fish("Test");

    fish.init();

    Fish rabarbaras("Barbaras");
    rabarbaras.SetValue(1);
    rabarbaras.toString();

    return 0;
}
//
// Created by Jerome on 2021-02-10.
//
#include <iostream>
#include "Ship.h"

std::string Ship::GetName() {
    return this->name;
}

int Ship::GetStorage() {
    return this->storage;
}

void Ship::SetName(std::string name) { this->name = name; }

void Ship::SetStorage(int storage) { this->storage = storage; }

void Ship::setAll(std::string name, int storage) {
    this->name = name;
    this->storage = storage;
}

Ship::Ship(std::string name, int storage) {
    this->name = name;
    this->storage = storage;
}

Ship::Ship() {
    this->name = "Stock Ship";
    this->storage = 10;
}

void Ship::toString() {
    std::cout << "This ship is called " <<
              this->name << " and has the storage capacity of " <<
              this->storage << " units\n";
}
#ifndef UOSTASGAME_SHIP_H
#define UOSTASGAME_SHIP_H

#include <string>
#include "random"
#include "time.h"
#include <vector>
class Ship {

private:


    std::string name;
    int storage;

public:

    Ship();
    Ship(std::string, int);

    std::string GetName();
    void SetName(std::string name);

    int GetStorage();
    void SetStorage(int storage);

    void setAll(std::string, int);

    void toString();


};


#endif //UOSTASGAME_SHIP_H
void Fish::SetValue(int x) {
    unsigned int taskai = fishTypes.at(x).GetScore();

    this->score = taskai;
}