gpt4 book ai didi

c++ - 常量错误

转载 作者:行者123 更新时间:2023-11-30 01:31:06 26 4
gpt4 key购买 nike

为什么我会收到此错误:
错误 1 ​​错误 C2662:“Allocator::Allocate”:无法将“this”指针从“const Allocator”转换为“Allocator &”

那是代码:

/*Allocator.h*/
/*Not finished yet but working*/
#pragma once
template<class T>
class Allocator
{
public:
//typedef T value_type;
typedef T* pointer;
pointer Allocate(std::size_t count);
pointer Construct(void* address, const pointer obj);
template<class FwdIter>
void Destroy_(FwdIter first,FwdIter last);
void Deallocate_(void* where);
Allocator();
~Allocator();

private:
void Destroy_(const T* obj);


};
/*Allocator_impl.hpp*/
#pragma once
#include "StdAfx.h"
#include "Allocator.h"

template<class T>
Allocator<T>::Allocator()
{
}


template<class T>
Allocator<T>::~Allocator()
{
/*Destroy();
Deallocate();*/
}

template<class T>
typename Allocator<T>::pointer Allocator<T>::Allocate(std::size_t count)
{
return static_cast<pointer>(::operator new(sizeof(value_type) * count));
}

template<class T>
typename Allocator<T>::pointer Allocator<T>::Construct(void* address, const pointer obj)
{
return new (address) T(*obj);
}

//template<class T>
//void Allocator<T>::Destroy()
//{
// //Destroy_(addressBegin_, addressBegin_ + size_);
//}

template<class T>
void Allocator<T>::Destroy_(const T* obj)
{
obj->~T();
}

template<class T>
template<class FwdIter>
void Allocator<T>::Destroy_(FwdIter first,FwdIter last)
{
while (first != last)
{
Destroy_(&*first);
++first;
}
}

template<class T>
void Allocator<T>::Deallocate_(void* address)
{
::operator delete(address);
}

//template<class T>
//void Allocator<T>::Deallocate()
//{
// //Deallocate_(addressBegin_);
//}
/*Toy.h*/
#pragma once
#include "Allocator_impl.hpp"


/*As a base to managed memory*/
template<class T, class A = Allocator<T>>
class ToyBase
{
typedef T* pointer;
private:
A alloc_;
protected:
//--------------------------------------COMMENT HERE
pointer Allocate(const std::size_t)const;<------------When invoking this fnc from
explicit ToyBase();
virtual ~ToyBase();
};

template<class T, class A>
ToyBase<T,A>::ToyBase()
{}


template<class T, class A>
ToyBase<T,A>::~ToyBase()
{}
//--------------------------------------AND COMMENT HERE
template<class T, class A>
typename ToyBase<T,A>::pointer ToyBase<T,A>::Allocate(const std::size_t count)const
{
return alloc_.Allocate(count);<-----------here
}
/*
But when I remove const from fnc decl. it works. I do not understand it as I do not change an object merely invoke fnc on its member.
*/



template<class T>
class ToyRepresentation : private ToyBase<T>
{
public:
typedef T value_type;
typedef T* pointer;
ToyRepresentation(const std::size_t = 0);
void Push(T*);
void Pop();
void GetSize()const;
void GetCapacity()const;
void SetCapacity(const std::size_t);
void Reset();
private:
pointer data_;
std::size_t size_;
std::size_t capacity_;
static unsigned TOTAL_; //total number of created objects
};

template<class T>
unsigned ToyRepresentation<T>::TOTAL_ = 0;


template<class T>
ToyRepresentation<T>::ToyRepresentation(const std::size_t count = 0): ToyBase<T>(), data_(Allocate(count)), size_(0), capacity_(count)
{
}

/*tmain*/
#include "stdafx.h"
#include "Toy.h"
int _tmain(int argc, _TCHAR* argv[])
{
try
{
ToyRepresentation<int> t;
}
catch(const std::exception&)
{
}
return 0;
}

有趣行的注释在代码中标记。谢谢。

最佳答案

alloc_.Allocate 不是常量方法。您可以通过使 alloc_ 可变来“修复”(或隐藏)此警告,尽管在不了解编译器警告您的原因的情况下不应这样做。

不确定为什么调用它的方法无论如何都需要是 const。分配通常不会保持其上下文不变。

关于c++ - 常量错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3619553/

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