首页 > 解决方案 > 处理智能指针的错误

问题描述

我很难理解智能指针是如何工作的。我必须解决一个练习题,我应该在其中编写三个类:一个“Base”类和两个派生类“File”和“Directory”,以模拟文件系统。'Base' 类是一个带有字符串的抽象类。

我已经编写了所有我想使用的方法,期待 ls() 方法,我刚刚填充了一个简单的打印,并尝试运行一个简单的程序,但编译器返回我无法理解的错误。

我最近开始研究智能指针,所以我可能在处理它们时做错了什么。

这是我的 UDT.h


#ifndef UDT_H
#define UDT_H

#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <memory>

class Base {
    private:
        std::string name;

    public:
        std::string getName() const;                // --> returns name

        explicit Base(std::string name): name(name){};
        virtual int mType () const = 0;             // returns 1 if Dir and 2 if File
        virtual void ls (int indent) const = 0;
};

class File: public Base{
    private:
        uintmax_t sizeFile;

    public:
        int mType () const override{return 2;};

        File(std::string name, uintmax_t size);     // --> class file constructor

        uintmax_t getSize () const;
        void ls (int indent = 0) const override;    // --> print the name of the file with the correct indentation
};

class Directory: public Base{                       // implemented with private constructor in order to have just one root
    public:
        int mType () const override{return 1;};

        static std::shared_ptr<Directory> getRoot();                // --> returns the shared pointer to the root
        std::shared_ptr<Directory> addDirectory(std::string name);  // --> adds one sub directory
        std::shared_ptr<File> addFile(std::string name, uintmax_t size);    // adds one file to the directory

        std::shared_ptr<Base> get (std::string name);               // --> returns shared pointer to the file/dir with name == name
        std::shared_ptr<Directory> getDir (std::string name);       // --> returns shared pointer to the file/dir with name == name
        std::shared_ptr<File> getFile (std::string name);           // --> returns shared pointer to the file/dir with name == name

        void remove (std::string name);
        void ls (int indent = 0) const override;                    // --> print the content of the dir with the correct indentation

    private:
        std::weak_ptr<Directory> myself;                            // --> weak pointer to itself
        std::weak_ptr<Directory> parent;                            // --> weak pointer to parent
        static std::shared_ptr<Directory> root;                     // --> shared pointer to root
        std::vector<std::shared_ptr<Base>> list;                    // --> list of dir/file(s) in the directory
        explicit Directory(std::string name, std::shared_ptr<Directory> parent);    // --> dir constructor
};



#endif //UDT_H

这是 UDT.cpp


#include "UDT.h"

std::shared_ptr<Directory> Directory::root;

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

Directory::Directory(std::string name, std::shared_ptr<Directory> parent): Base(name) {
    this->parent = parent;
    this->myself = std::shared_ptr<Directory>(this);
}

File::File(std::string name, uintmax_t size):Base(std::move(name)) {
    sizeFile = size;
}

std::shared_ptr<Directory> Directory::getRoot() {
    if (root == nullptr){ // --> create root directory if not already created
        root = std::shared_ptr<Directory>(new Directory("/", nullptr));
    }
    return root;
}

std::shared_ptr<Directory> Directory::addDirectory(std::string name) {
    if (name == "." || name == "..")
        throw std::exception();

    for(int i = 0 ; i < list.size() ; i++){
        if(list[i]->getName() == name)
            throw std::exception(); // object with the same name --> throw exception
    }

    // if no object with the same name --> add it!

    std::shared_ptr<Directory> newDir = std::shared_ptr<Directory>(new Directory(name, this->myself.lock()));
    return newDir;
}

std::shared_ptr<File> Directory::addFile(std::string name, uintmax_t size) {
    if (name == "." || name == "..")
        throw std::exception();

    for(int i = 0 ; i < list.size() ; i++){
        if(list[i]->getName() == name)
            throw std::exception(); // object with the same name --> throw exception
    }

    // if no object with the same name --> add it!
    std::shared_ptr<File> newFile = std::make_shared<File>(name, size);
    return newFile;
}

std::shared_ptr<Base> Directory::get(std::string name) {
    if (name == "." )
        return std::shared_ptr<Base>(this->myself);

    if (name == ".." )
        return std::shared_ptr<Base>(this->parent);

    for(int i = 0 ; i < list.size() ; i++){
        if(list[i]->getName() == name)
            return list[i];
    }

    return nullptr;
}

std::shared_ptr<Directory> Directory::getDir(std::string name) {
    if (name == "." )
        return this->myself.lock();

    if (name == ".." )
        return this->parent.lock();

    for(int i = 0 ; i < list.size() ; i++){
        if(list[i]->getName() == name)
            return std::dynamic_pointer_cast<Directory>(list[i]);
    }

    return nullptr;
}

std::shared_ptr<File> Directory::getFile(std::string name) {
    if (name == "." || name == ".." )
        return nullptr;

    for(int i = 0 ; i < list.size() ; i++){
        if(list[i]->getName() == name)
            return std::dynamic_pointer_cast<File>(list[i]);
    }

    return nullptr;
}

void Directory::remove(std::string name) {
    if (name == "." || name == "..")
        throw std::exception();

    for(int i = 0; i < list.size() ; i++){
        if (list[i]->getName() == name){
            list.erase(list.begin() + i);
        }
    }
}

uintmax_t File::getSize() const {
    return sizeFile;
}

void Directory::ls(int indent) const {
    std::cout << "ls" << std::endl;
}

void File::ls(int indent) const {
    std::cout << "ls" << std::endl;
}

和 main.cpp


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

int main() {

    std::shared_ptr <Directory> root = Directory::getRoot();
    auto alfa = root -> addDirectory( "alfa");
    alfa -> addDirectory("beta") -> addFile( "beta1", 100);
    alfa -> getDir("beta") -> addFile( "beta2", 200);
    alfa -> getDir("..") -> ls();
    alfa -> remove("beta");
    root -> ls();

    return 0;
}

我收到此错误: (5486,0x114c015c0) malloc: *** error for object 0x6000000000000000: pointer being freed was not allocated (5486,0x114c015c0) malloc: *** set a breakpoint in malloc_error_break to debug

请帮助我了解问题所在

标签: c++polymorphismsmart-pointers

解决方案


推荐阅读