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

Ask A Question

Stats

  • Questions 516k
  • Answers 516k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer The pun is in the "not sealed" part: class S… May 16, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer I don't see why your URLs aren't working. You can… May 16, 2026 at 6:47 pm
  • Editorial Team
    Editorial Team added an answer Try adding this line below the first line: -- insert… May 16, 2026 at 6:47 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I'm getting compile error in this code #include<iostream> #include<cstdio> #include<string> using namespace std; void
Getting this error when updating a row via a gridview with a sqldatasource in
Getting this error: System.Data.SqlClient.SqlException : The conversion of a datetime2 data type to a
Im getting this error when I try and make an object. Here is my
I'm getting this error: Only parameterless constructors and initializers are supported in LINQ to
I am getting this error in maybe one out of 50-100 requests. I am
I'm getting this error (see title) while trying to parse an XML file in
I am getting this error when I try to run this MySQL command: CREATE
So I have this code for these Constructors of the Weapon class: Weapon(const WeaponsDB
I have this snippet of code here. The intention is to make a copy

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.