【问题标题】:C++ implementing function for sharedpointer with reference counter具有引用计数器的共享指针的 C++ 实现函数
【发布时间】:2017-10-12 18:17:31
【问题描述】:

getPointer() 返回此实例的原始指针。 getData() 返回实例的原始指针指向的数据。这个返回的数据/类型应该是可修改的。 一次最多允许 3 个引用。

我正在做一些练习任务以更好地理解 c++ :-) 已经做了大约 2 个月了,现在开始对各种其他事情的基础知识很慢,现在我发现这个任务是“重新发明 shared_ptr 的轮子”,我在尝试实现这些功能时卡住了,我决定寻求帮助。

我已将 cpp wiki 用于 shared_ptr operator* 和 -> 并尝试使用函数实现它,但我得到表达式必须具有指针类型 // 表达式必须具有类类型并且没有运算符“

  1. 所以我认为这些功能没有返回它们应该提供的功能:-) 所以我想我搞砸了。我尝试使用 this->pData 和 pData.get() 但仍然出现相同的错误:-(

这是我的 main.cpp

#include "sharedPtr.hpp"
#include "Cat.hpp"
#include <iostream>

    int main(void)
    {
        sharedPtr<Cat> newCat(new Cat(0, "Ferrari"));

        // should print "Ferrari"
        std::cout << newCat.getPointer()->getName() << std::endl;

        // should also print "Ferrari"
        std::cout << newCat.getData().getName() << std::endl;

        newCat.getData().score = 50;

        // should printf "50", as it was just assigned before
        std::cout << newCat.getPointer()->score << std::endl;

        // make some copies
        sharedPtr<Cat> copyOfnewCat = newCat;
        sharedPtr<Cat> copyOfnewCat2 = newCat;

        // this copy should fail
        sharedPtr<Cat> copyOfnewCat3 = newCat;

        // should be nullptr
        std::cout << copyOfnewCat3.getPointer() << std::endl;

        // should be something other than 0 and equal
        std::cout << "copy2 pointer: " << copyOfnewCat2.getPointer() << " copy1 pointer: " << copyOfnewCat.getPointer() << std::endl;
        copyOfnewCat2.getData().score = 40;

        // should be 40 now 
        std::cout << newCat.getPointer()->score << std::endl;
        sharedPtr<Cat> copyOfnewCat4(newCat);

        // should be nullptr
        std::cout << copyOfnewCat4.getPointer() << std::endl;
    }

我的猫.cpp

#include <string>

class Cat
{
public:
    Cat(unsigned int w_score, const std::string& w_name) :
        score(w_score),
        name(w_name)
    {}
    std::string getName()
    {
        return name;
    }

    unsigned int score;
private:
    std::string name;
};

更新确切的错误信息

Severity    Code    Description Project File    Line    Suppression State
Error (active)  E0349   no operator "<<" matches these operands main.cpp    31  
Error (active)  E0044   expression must have pointer type   main.cpp    10  
Error (active)  E0153   expression must have class type main.cpp    13  
Error (active)  E0153   expression must have class type main.cpp    15  
Error (active)  E0044   expression must have pointer type   main.cpp    18  
Error (active)  E0349   no operator "<<" matches these operands main.cpp    28  

这些错误指的是 main.cpp 中的这一行

// should also print "Ferrari"
    std::cout << newCat.getPointer()->getName() << std::endl;

// should also print "Ferrari"
    std::cout << newCat.getData().getName() << std::endl;

问题

嗯,我想我注意到我应该在我的函数开头有我的类名,所以它应该是 sharedptr& getData 和 sharedptr* getPointer 吗?

【问题讨论】:

  • 请提供准确的编译器错误信息,并在代码中注明错误发生的行。并且请将代码减少到重现错误所需的最低限度。 minimal reproducible example
  • 请将相关部分放入问题中(编辑它),而不是放入 cmets。谢谢。
  • @WernerHenze 我用错误消息更新了主要问题抱歉没有真正使用过stackoverflow,所以很抱歉我还在学习
  • 您需要替换所有“getData()”。通过“getData()->”。然后它在 MSVC 2013 和 MSVC 2017 上编译。你使用的是什么编译器?你可以试试“sharedPtr newCat = new Cat(0, "Ferrari");"!?
  • @WernerHenze 我正在使用 VS2017 pro,是的,如果我将 getData() 替换为 getData()-> 并将 getPointer()->score 替换为 getPointer().score 我没有看到错误。但是我注意到这不是正确的方法,因为我必须按原样使用 main.cpp,只有 sharedPtr.cpp 是可更改的。所以我认为问题在于使用 operator-> 和 operator* ,我认为我有冗余,不应该使用这些,而是​​只使用 getData 和 getPointer。

标签: c++ c++11 shared-ptr


【解决方案1】:

Werner Henze 已经在他的 cmets 中说了所有相关的内容,所以这只不过是把它放在正确的位置。

该错误是由getData() 返回一个指针并在main 中用作引用引起的。由于getPointer() 已经返回指针,解决方法是更改​​getData() 以返回引用:

T& getData() {
    return *pData;
}

但您的代码仍有一些可能的改进:

  • 您没有实现对 3 个引用的限制,因此您的 cmets 不一致
  • 计算指向 NULL 的指针(默认构造函数)的引用并没有实际意义,因为在任何对象上都没有任何引用
  • operator = 的常见实现是 复制和交换 习惯用法。它通常会导致更健壮和更简单的代码

除了第一条评论和修复错误之后,您可能会要求在 Code Review 上对您的代码进行彻底审查 - 请注意,损坏的代码或仍未编写的代码明显不在主题范围内......

【讨论】:

  • 是的,我对代码进行了更改,但新的错误与包含 Cats.hpp 相关,而我不应该包含 Cats.hpp,而现在我应该包含 #pragma,现在代码编译并运行:-) 问题是所说的更改返回 *pData
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-03-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-09-25
  • 2018-02-11
  • 1970-01-01
相关资源
最近更新 更多