C++ vector 中不存在的字符串成员变量

C++ string member variable not present in vector

本文关键字:字符串 成员 变量 不存在 vector C++      更新时间:2023-10-16

我正在创建一个包含指向基类的指针的向量。在这个向量中,我动态存储指向派生类的指针,这些派生类包含一些成员变量,其中一个是字符串变量名称。

#include "stdafx.h"
#include <iostream>
#include <vector>
#include <string>
#include <cstdlib>
bool hasDirection = false;
bool hasDiameter = false;
int direction;
float diameter;
int starDimension = 0;
int animalDimension = 0;
int fishDimension = 0;
 class MovingObject
{
protected:
    std::string name;
    int direction;
    float diameter;
    int dimension;
    float movingSpeed;
public:
    std::string getName(){ return name;};
    int getDirection(){ return direction;};
    float getDiameter(){ return diameter;};
    float getMovingSpeed(){ return movingSpeed;};
    int getDimension(){ return dimension;};
    void setName(std::string v){ name = v;};
    void setDirection(int d){ direction = d;};
    void setDiameter(float f){ diameter = f;};
    void setMovingSpeed(float s){ movingSpeed = s;};
    void setDimension (int d){ dimension = d;};
    virtual void PrintContents()=0;
};
static std::vector<MovingObject*> data;
class starObject : public MovingObject
{
public:
    void PrintContents()
    {
        std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ")";
    }
};
class animalObject : public MovingObject
{
public:
    void PrintContents()
    {
        std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ")";
    }
};
class fishObject : public MovingObject
{
public:
    void PrintContents()
    {
        std::cout << "(" << getName() << "," << getDiameter() << "," << getDirection() << ", [" << getDimension() << "], " << getMovingSpeed() << ")";
    }
};

后来我在 main 函数中设置了所有这些成员变量。问题是当我尝试输出成员变量的内容时,除了字符串名称外,所有成员变量都显示出来。现在,我已经检查以确保在调用 PrintContent() 方法之前设置了字符串,它显示该值在向量中。但是,当我通过代码进行调试时,该值不再存在,而是包含一个空字符串。

具有更好c ++知识的人可以向我解释为什么会发生这种情况吗?这是主类:

int main()
{
        std::string type;
        Reader reader;
        while (!std::cin.eof())
        {
            try
            {
                std::string type;
                std::cin >> type;
                if (type =="int")
                {
                    reader.ReadDirection();
                }
                else if (type =="float")
                {
                    reader.ReadDiameter();
                }
                else if (type == "string")
                {
                    std::string name;
                    std::cin >> name;
                    if (hasDirection && hasDiameter)
                    {
                        int dimension;
                        if (diameter > 0 && diameter < 10)
                        {   
                            //fish
                            fishObject fish;
                            fish.setName(name);
                            fish.setDiameter(diameter);
                            fish.setDirection(direction);
                            dimension = fishDimension;
                            fishDimension += 50;
                            fish.setDimension(dimension);
                            fish.setMovingSpeed(0.1);
                            data.push_back(&fish);
                        }
                        else if (diameter >= 10 < 500)
                        {
                            //animal
                            animalObject animal;
                            animal.setName(name);
                            animal.setDiameter(diameter);
                            animal.setDirection(direction);
                            dimension = animalDimension;
                            animalDimension += 800;
                            animal.setDimension(dimension);
                            animal.setMovingSpeed(5.0); 
                            data.push_back(&animal);
                        }
                        else if (diameter >=500)
                        {
                            //star
                            starObject star;
                            star.setName(name);
                            star.setDiameter(diameter);
                            star.setDirection(direction);
                            dimension = starDimension;
                            starDimension += 5000;
                            star.setDimension(dimension);
                            star.setMovingSpeed(30.0);
                            data.push_back(&star);
                        }
                    }
                    else
                    {
                        throw (IncompleteData(name));
                    }
                }
            }
            catch (IncompleteData e)
            {
                std::cerr << "No diameter or direction given for object " << e.objectName << "n";
            }
        }

你推送到data向量的对象是局部的,因为它们是在 if/else 块中声明的(参见 fishanimal 的声明)。

当您将此类对象的地址推送到向量时,它将继续指向本地对象,该对象在本地范围的末尾不复存在。您需要创建超出本地范围的对象。一种方法是在堆上创建本地对象的副本,并将其推送到向量:

data.push_back(new fishObject(fish));
当然,这意味着

你会得到内存泄漏,除非你确保在程序结束前的某个时间显式删除向量的元素。避免考虑这一点的通常建议是使用 std::unique_ptr<MovingObject> 向量而不是裸指针向量。