C++ vector 复制适用于一个 vector ,但不适用于另一个 vector

标签 c++ vector

这个问题真的很奇怪,我似乎找不到任何导致它的原因。

所以这里有一个赋值运算符重载函数,鸟类和哺乳动物都是 vector 。 (下面是类)

const Register& Register::operator=(const Register& theOther)
{
    if(this != &theOther)
    {
        this->~Register();               // deleted
        birds.resize(theOther.birds.size());
        mammals.resize(theOther.mammals.size());

        birds=theOther.birds;
        mammals=theOther.mammals;
    }
    return *this;
}

(如果您在这里发现任何奇怪的地方,那是因为我多次重写了这些行)。所以问题是在运行时,

birds.operator=(theOther.birds);

工作得很好,每个变量都被正确复制,但是当它到达下一行时,它调用与鸟类相同的 vector 复制函数,但是 ma​​mmals 永远不会得到 theOther.mammals -es 动物部位(物种等)。它获得了唯一的哺乳动物部分(捕食者等)。我只是不明白。下面是我的类(class),正如你所看到的,它们在重要的部分完全相似......

所以,类

class Animal
{
    char* species;
    unsigned int weight;
    char* color;
public:
...
}

鸟类

class Bird: public Animal
{
    bool singing;
    bool waterfowl;
    unsigned int eggs; 

public:
...
const Bird& operator =(const Bird& theOther);
{
    if(this != &theOther)
    {
        Bird copy(theOther);
        this->setSpecies(copy.getSpecies());
        this->setWeight(copy.getWeight());
        this->setColor(copy.getColor());

        singing = theOther.singing;
        waterfowl = theOther.waterfowl;
        eggs = theOther.eggs;
    }
    return *this;
}

哺乳动物类

class Mammal: public Animal
{
    bool predator;
    bool oviparous;
public:
...
const Mammal& Mammal::operator =(const Mammal& theOther)
{
    if(this != &theOther)
    {
        Mammal copy(theOther);
        this->setPredator(copy.getPredator());
        this->setOviparous(copy.getOviparous());

        predator = theOther.predator;
        oviparous = theOther.oviparous;
    }
    return *this;
}

当然还有 Register,它有 vector 。

class Register
{
    std::vector <Bird> birds;
    std::vector <Mammal> mammals;
    std::vector <Reptile> reptiles;

public:
...

编辑:

Register::Register(const Register& newRegister)
{
    birds = newRegister.birds;
    mammals = newRegister.mammals;
    reptiles = newRegister.reptiles;
}

编辑2:

void Animal::setSpecies(char* _species)
{
    this->species = _species;
}

也许我只是太累了,没有发现我犯的错误。请告诉我我做错了什么。

最佳答案

如前所述 - 不要手动调用析构函数。

你的哺乳动物 operator= 没有复制物种、重量和颜色而你的鸟 operator= 复制的原因是哺乳动物版本没有代码设置这些值。鸟类运算符(operator)有这些行:

    this->setSpecies(copy.getSpecies());
    this->setWeight(copy.getWeight());
    this->setColor(copy.getColor());

哺乳动物运算符没有。

要么将这些行复制到 mammal 运算符中,要么更好的是,编写一个执行此操作的 animal 方法并从 bird 和 mammal 运算符中调用它。

我也不确定为什么你需要在调用 getSpecies 等之前制作一个拷贝,你应该能够在原始的 theOther 参数上调用它。

关于C++ vector 复制适用于一个 vector ,但不适用于另一个 vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23621542/

相关文章:

c++ - 使用c++进行几何形状识别

c++ - 如何在两组上做双指针技术

java - 用java实现 vector 的程序

c++ - 如何使用单个 for 循环遍历 std::map<string,int> 和 std::vector<int>?

c++ - 对列表 vector 调用 clear() 会清除列表中每个索引处的所有节点吗?

c++ - 我们如何自定义使用 `std::iterator` 的目的和教程

c++ - 没有用于调用 'std::basic_string<char>::basic_string c++ 的匹配函数

c++ - 在类中传递 vector 而不复制

c++ - 在 C++ 中使用 boost::property_tree 库函数读取 XML 数据

c++ - 访问 4d vector 时出现问题