Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1028319
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T12:22:09+00:00 2026-05-16T12:22:09+00:00

Why am I getting this error: Error 1 error C2662: ‘Allocator::Allocate’ : cannot convert

  • 0

Why am I getting this error:
Error 1 error C2662: ‘Allocator::Allocate’ : cannot convert ‘this’ pointer from ‘const Allocator’ to ‘Allocator &’ ?

Thats code:

/*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;
}

Comments to interesting lines are marked in code. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T12:22:09+00:00Added an answer on May 16, 2026 at 12:22 pm

    ToyBase<T,A>::Allocate is const qualified which means that you cannot invoke any non-const methods on any members of this as they too are const-qualified now.

    Try FAQ 18.10.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Getting this error with jquery & jquery.form. Site has been live for awhile..upgraded to
I am getting this error from eway token payment API, soap:ReceiverServer was unable to
Getting this error when I follow the simple code example from jsoncpp, that basically
getting this error: C:\CodeBlocks\kool\praks3\vector.h|62|error: passing 'const Vector<2u>' as 'this' argument of 'std::string Vector::toString() [with
I getting this error when retrieve xml request from http://www.cbr.ru/scripts/XML_daily.asp XML code like this:
Anyone getting this error when using the new free chart controls MS bought from
Getting this error while trying to compile a copied code from a Fortran 77
Getting this error: 2009-09-03 12:44:02.307 xcodebuild[307:10b] warning: compiler 'com.apple.compilers.llvm.clang.1_0.analyzer' is based on missing compiler
Getting this error when try to add an item to my repositories/context: Collection has
Am getting this error message and matched my brackets and couldn't find anything wrong.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.