我的程序从指针列表中删除对象并添加到指针列表的问题

Posted

技术标签:

【中文标题】我的程序从指针列表中删除对象并添加到指针列表的问题【英文标题】:Problems with my program deleting objects from pointer list and adding to pointerlist 【发布时间】:2017-10-13 17:27:23 【问题描述】:

我想知道有人可以帮我完成我的程序吗?我的 dragon.cpp 文件有一些问题,我似乎无法创建不同类型的龙。

我还是 C++ 新手,不知道如何解决这些错误

更新

我从 dragon.cpp 中删除了第二个构造函数,现在我得到了新的错误。我还在 Dragon 类的头文件中添加了一个虚拟解构器。

错误

 Severity   Code    Description Project File    Line    Suppression State
Error   LNK2019 unresolved external symbol "class std::basic_ostream<char,struct std::char_traits<char> > & __cdecl operator<<(class std::basic_ostream<char,struct std::char_traits<char> > &,class DragonCave const &)" (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABVDragonCave@@@Z) referenced in function _main  main.obj    1   
Error   LNK1120 5 unresolved externals  Dragon.exe  1   
Error   LNK2019 unresolved external symbol "public: class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const & __thiscall Dragon::getName(void)const " (?getName@Dragon@@QBEABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@XZ) referenced in function _main   main.obj    1   
Error   LNK2019 unresolved external symbol "public: void __thiscall DragonCave::accommodateDragon(class Dragon *)" (?accommodateDragon@DragonCave@@QAEXPAVDragon@@@Z) referenced in function _main  main.obj    1   
Error   LNK2019 unresolved external symbol "public: void __thiscall DragonCave::evictDragon(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > const &)" (?evictDragon@DragonCave@@QAEXABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z) referenced in function _main        main.obj    1   
Error   LNK2019 unresolved external symbol "public: __thiscall DragonCave::DragonCave(void)" (??0DragonCave@@QAE@XZ) referenced in function _main   main.obj    1   

我的 main.cpp

https://pastebin.com/dKwu***7

我的龙.hpp

https://pastebin.com/cbJ2Z80D

我的dragon.cpp

#include "dragon.hpp"
 
#include <string>
#include <list>
#include <iostream>
 
/* Dragon class functions here*/
Dragon::Dragon(std::string const& name, size_t age, size_t size) 
/* DragonCave class functions here*/
DragonCave::DragonCave() 
 
    std::cout << "Copy constructor called" << std::endl;
    for (auto i : d.dragons) 
        Dragon *c = i->clone();
        dragons.push_back(c);
    

 
// Made for you, as it uses a tiny bit of dynamic memory here! You will see more of this in the next round.
DragonCave::~DragonCave()

    for (std::list<Dragon*>::iterator it = dragons.begin(); it != dragons.end(); it++)
        delete *it;

 
/* ChineseDragon class functions */
//Constructor here
 
ChineseDragon::ChineseDragon(std::string const& name, size_t age, size_t size) : Dragon(name, age, size)  
 
void ChineseDragon::eat(std::list<Food>& food)

    for (std::list<Food>::iterator it = food.begin(); it != food.end();)
        if (it->type == PeopleFood)
        
            std::cout << "Chinese dragon ate: " << it->name << std::endl;
            it = food.erase(it);
            size++;
        
        else it++;
 

 
void ChineseDragon::hoard(std::list<Treasure>& treasures)

    for (std::list<Treasure>::iterator it = treasures.begin(); it != treasures.end();)
        if (it->type == Potions)
        
            std::cout << "Chinese dragon received: " << it->name << std::endl;
            this->treasures.push_back(*it);
            it = treasures.erase(it);
        
        else it++;
 
 

 
 
FantasyDragon::FantasyDragon(std::string const& name, size_t age, size_t size) : Dragon(name, age, size)  
 
void FantasyDragon::eat(std::list<Food>& food)

    for (std::list<Food>::iterator it = food.begin(); it != food.end();)
        if (it->type == PeopleFood | People)
        
            std::cout << "Fantasy dragon ate: " << it->name << std::endl;
            it = food.erase(it);
            size++;
        
        else it++;
 
 

 
void FantasyDragon::hoard(std::list<Treasure>& treasures)

 
    for (std::list<Treasure>::iterator it = treasures.begin(); it != treasures.end();)
        if (it->type == Potions)
        
            std::cout << "Fantasy dragon received: " << it->name << std::endl;
            this->treasures.push_back(*it);
            it = treasures.erase(it);
        
        else it++;
 

 
 
/* MagicDragon class functions */
MagicDragon::MagicDragon(std::string const& name, size_t age, size_t size) : Dragon(name, age, size)  
 
void MagicDragon::eat(std::list<Food>& food)

    for (std::list<Food>::iterator it = food.begin(); it != food.end();)
        if (it->type == Herbs)
        
            std::cout << "Magic dragon ate: " << it->name << std::endl;
            it = food.erase(it);
            size++;
        
        else it++;

 
void MagicDragon::hoard(std::list<Treasure>& treasures)

    for (std::list<Treasure>::iterator it = treasures.begin(); it != treasures.end();)
        if (it->type == Potions)
        
            std::cout << "Magic dragon received: " << it->name << std::endl;
            this->treasures.push_back(*it);
            it = treasures.erase(it);
        
        else it++;

【问题讨论】:

您在头文件和源文件中都实现了 Dragon 构造函数。你应该只做其中一个。 请提供minimal reproducible example 并将所有相关代码直接发布到问题中。因为您提供的一个文件中有很多不必要的代码。 @Galik 感谢您的关注,现在我遇到了新错误,我已经更新了问题。 这可能是您没有为其编写实现的函数列表 - 或者您可能给了它们错误类型的参数? 【参考方案1】:

这些错误告诉您,您的某些函数缺少实现。特别是 operator&lt;&lt;(std::ostream&amp; os, DragonCave const&amp; cave)Dragon::getNameDragonCave::accommodateDragonDragonCave::evictDragon 已声明但从未定义。

最后一个错误是指DragonCave::DragonCave(),根据您发布的代码,它实际上是定义的。然而,它似乎曾经是一个复制构造函数,并且有一个变量d 突然出现。我怀疑这个错误与此有关。

【讨论】:

以上是关于我的程序从指针列表中删除对象并添加到指针列表的问题的主要内容,如果未能解决你的问题,请参考以下文章

从函数返回一个指针,然后释放指针

从指针数组中删除一个对象

删除数组指针C++时堆损坏

在 Parse 中将对象添加到现有指针

从列表返回指向对象的指针

list.count 是不是物理地遍历列表以对其进行计数,或者它是不是保留一个指针