【发布时间】:2017-10-12 18:17:31
【问题描述】:
getPointer() 返回此实例的原始指针。 getData() 返回实例的原始指针指向的数据。这个返回的数据/类型应该是可修改的。 一次最多允许 3 个引用。
我正在做一些练习任务以更好地理解 c++ :-) 已经做了大约 2 个月了,现在开始对各种其他事情的基础知识很慢,现在我发现这个任务是“重新发明 shared_ptr 的轮子”,我在尝试实现这些功能时卡住了,我决定寻求帮助。
我已将 cpp wiki 用于 shared_ptr operator* 和 -> 并尝试使用函数实现它,但我得到表达式必须具有指针类型 // 表达式必须具有类类型并且没有运算符“
所以我认为这些功能没有返回它们应该提供的功能:-) 所以我想我搞砸了。我尝试使用 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