gpt4 book ai didi

c++ - 程序失败。 sharedPtr vector C++

转载 作者:行者123 更新时间:2023-11-30 05:42:00 25 4
gpt4 key购买 nike

拜托,我真的需要帮助..不要因为 sharedPtr 的实现而 panic
我的 C++ 项目有问题。在我的项目中,我正在创建一家公司,该公司拥有有关其员工(艺术家、程序员、经理)、项目、……的所有信息。为了容纳所有员工,我使用了一个 SharedPtr vector (使用自己的实现)

而且看起来 vector 出现了一些问题:

Unhandled exception at 0x00CC8CA9 in projComp.exe: 0xC0000005: Access violation reading location 0x00000044.

并指出 vector 中的这个断点-

bool _Inside(const value_type *_Ptr) const
{ // test if _Ptr points inside vector
return (_Ptr < this->_Mylast && this->_Myfirst <= _Ptr);
}

这是我的 SharedPtr 实现:

#ifndef _SHARED_PTR_H_
#define _SHARED_PTR_H_

#include "RCObject.h"

template<class T>
class SharedPtr {

public:
SharedPtr(T* realPtr = 0);
SharedPtr(const SharedPtr& rhs);
~SharedPtr();

SharedPtr& operator=(const SharedPtr& rhs);
const T* operator->() const;
T* operator->();
const T& operator*() const;
T& operator*();

template<class newType> // template function for
operator SharedPtr<newType>() // implicit conversion ops.
{
return SharedPtr<newType>(counter->pointee);
}

private:
struct CountHolder : public RCObject {
~CountHolder() { delete pointee; }
void addReference() {
++refCount;
}
void removeReference() {
if (--refCount == 0)delete this;
}
T *pointee;
};

CountHolder *counter;
void init();
void makeCopy();
};


template <class T>
void SharedPtr<T>::init() {
if (counter->isShareable() == false) {
T *oldValue = counter->pointee;
counter = new CountHolder;
counter->pointee = new T(*oldValue);
}
counter->addReference();
}

template <class T>
SharedPtr<T>::SharedPtr(T* realPtr) : counter(new CountHolder) {
counter->pointee = realPtr;
init();
}

template <class T>
SharedPtr<T>::SharedPtr(const SharedPtr& rhs) : counter(rhs.counter) {
init();
}

template <class T>
SharedPtr<T>::~SharedPtr() {
counter->removeReference();
}

template <class T>
SharedPtr<T>& SharedPtr<T>::operator=(const SharedPtr& rhs) {
if (counter != rhs.counter) {
counter->removeReference();
counter = rhs.counter;
init();
}
return *this;
}

template <class T>
void SharedPtr<T>::makeCopy() {
if (counter->isShared()) {
T *oldValue = counter->pointee;
counter->removeReference();
counter = new CountHolder;
counter->pointee = new T(*oldValue);
counter->addReference();
}
}

template <class T>
const T* SharedPtr<T>::operator->() const {
return counter->pointee;
}

template <class T>
T* SharedPtr<T>::operator->() {
makeCopy();
return counter->pointee;
}

template <class T>
const T& SharedPtr<T>::operator*() const {
return *(counter->pointee);
}

template <class T>
T& SharedPtr<T>::operator*() {
makeCopy();
return *(counter->pointee);
}

#endif

这是 RCObject:

#ifndef _RCOBJ_H_
#define _RCOBJ_H_

#include <iostream>
using namespace std;

class RCObject {
//private:
protected:
int refCount;
bool shareable;

protected:
RCObject();
RCObject(const RCObject&);
virtual ~RCObject() = 0;

RCObject& operator=(const RCObject&);

public:
virtual void addReference() = 0;
virtual void removeReference() = 0;
void markUnshareable();
bool isShareable() const;
bool isShared() const;

};

#endif

及其实现:

#include <iostream>
#include "RCObject.h"
using namespace std;


RCObject::RCObject() : refCount(0), shareable(true) {
}

RCObject::RCObject(const RCObject&) : refCount(0), shareable(true) {
}

RCObject& RCObject::operator=(const RCObject&) {
return *this;
}

RCObject::~RCObject() {
}

void RCObject::addReference() {
++refCount;
}

void RCObject::removeReference() {
if (--refCount == 0) delete this;
}

void RCObject::markUnshareable() {
shareable = false;
}

bool RCObject::isShareable() const {
return shareable;
}

bool RCObject::isShared() const {
return refCount > 1;
}

问题在于此代码行:

void Company::addArtist(vector<string> empAfield, string idCmp, string currPrj, double currPrjHrs, double ttlHrs, double exp, bool isEmp, double hrsDay, string name, string lastName, string idPrsn, long phoneNum) {
_employeeList.push_back(SharedPtr<Artist>(new Artist(empAfield, idCmp, currPrj, currPrjHrs, ttlHrs, exp, isEmp, hrsDay, name, lastName, idPrsn, phoneNum)));
}

抱歉,代码太长了,我一直在思考是什么导致了这个问题。也许 vector 在它已经被释放后删除了 SharedPtr?这不应该是,但我想不通了。

感谢所有试图挽救我痛苦的人;)

最佳答案

错误位置是 0x00000044,这通常意味着 this 在方法调用中为 NULL,而它正在尝试访问成员变量(在位置 this+0x44在这种情况下)。

检查您是否在 NULL Company* 指针上调用 addArtist()

关于c++ - 程序失败。 sharedPtr vector C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30884954/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com