指向派生类对象的指针存储在指向基类对象的指针向量中

时间:2021-02-17 23:48:56

标签: c++ inheritance vector

我必须从文件中读取数据并根据我正在读取的文件所在行实例化对象。

文件格式为:

num1 RandomString RandomString num2 num3 num4<br>
num1 RandomString RandomString<br>
num1 RandomString RandomString num2 num3 num4<br>

num1 标识我需要实例化的对象类型。例如,如果 num1 是 1,那么我需要创建一个狗对象,如果是其他任何对象,那么我需要创建一个通用的动物对象。

下面的代码已经完成了一半。

注意:目前我还没有为将成员设为私有而烦恼。目前不是优先事项。

#include <vector>
etc...

class Animal {
    public:
        int num1;
        string 1;
        string 2;
        int num2;
        int num3;
        int num4;
        std::string restOfTheLine;
        Animal(fLine){
            std::stringstream stream(fLine)
            stream >> num1;
            std::getline(stream, restOfTheLine);
       getFunctions etc...
}

class Dog : public Animal{


}


int main(){

    ifstream file("file.txt");
    std::string line;
    std::vector<Animal*> animals;

    while(!file.eof()){
        std::getline(file,line);
        if(line[0]=='1'){
            Dog* dog = new Dog(line);
            animals.push_back(dog);
        } 
        else{
            Animal* animal = new Animal(line);
            animals.push_back(animal);
        }
    std::cout << animals[2]->getNum2();
       

}

所以我正在检查该行的第一个字符是否为 1。如果是,则必须实例化狗对象。

当我编译它时,它给了我一个错误 - error:no matching function for call dog::dog,这显然是因为我没有在派生的狗类中写任何东西,因为我不确定如何处理它。

我有一个指向动物的指针类型的向量,那么如何使用继承添加指向狗的指针?

1 个答案:

答案 0 :(得分:1)

您正在尝试调用不存在的 Dog() 构造函数,就像错误消息告诉您的那样。所以只需添加这样一个构造函数。

您还需要在 virtual 中使用 Animal 析构函数,以便通过 Dog 指针正确销毁 Animal* 对象。

试试这个:

#include <fstream>
#include <vector>
#include <string>
...
using namespace std;

class Animal {
    public:
        ...
        Animal(const string &fLine) {
            ...
        }

        virtual ~Animal() {}

        ...
};

class Dog : public Animal{
    public:
        Dog(const string &fLine) : Animal(fLine) {}
};

int main() {
    ifstream file("file.txt");
    std::string line;
    std::vector<Animal*> animals;

    while (std::getline(file, line)) {
        if (line.empty()) continue;
        if (line[0] == '1') {
            animals.push_back(new Dog(line));
        } 
        else {
            animals.push_back(new Animal(line));
        }
        ...
    }       

    for(size_t i = 0; i < animals.size(); ++i) {
        delete animals[i];
    }

    return 0;
}

如果您使用的是 C++11 或更高版本,请考虑使用 std::unique_ptr 为您管理对象销毁:

#include <fstream>
#include <vector>
#include <string>
#include <memory>
...
using namespace std;

class Animal {
    public:
        ...
        Animal(const string &fLine) {
            ...
        }

        virtual ~Animal() {}

        ...
};

class Dog : public Animal{
    public:
        Dog(const string &fLine) : Animal(fLine) {}
};

int main() {
    ifstream file("file.txt");
    std::string line;
    std::vector<std::unique_ptr<Animal>> animals;

    while (std::getline(file, line)) {
        if (line.empty()) continue;
        if (line[0] == '1') {
            // if using C++11:
            animals.push_back(std::unique_ptr<Animal>(new Dog(line)));
            // if using C++14 and later:
            animals.push_back(std::make_unique<Dog>(line));
        } 
        else {
            // if using C++11:
            animals.push_back(std::unique_ptr<Animal>(new Animal(line)));
            // if using C++14 and later:
            animals.push_back(std::make_unique<Animal>(line));
        }
        ...
    }       

    return 0;
}