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 6686329
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T05:09:33+00:00 2026-05-26T05:09:33+00:00

I’m new to templates in C++, so here is my problem. I’ve a generic

  • 0

I’m new to templates in C++, so here is my problem.

I’ve a generic class ProductItem that will do all the stuff I want, but I need to specialize a part in order to use pointers (for char*).

My code :

typedef unsigned char BYTE;

template<typename T>
class TProductTableItem
{
protected:
    int             Offset;
    int             DataLength;
    T               Value;
public:
    virtual bool LoadFromBuffer(const BYTE* buffer, int count)
    {
        if(Offset + DataLength > count)
            return false;

        Value = buffer[Offset];
        return true;
    }
};

// Specialization (doesn't compile)    
class TProductTableItemString : public TProductTableItem<char*>
{
    bool LoadFromBuffer(const BYTE* buffer, int count)
    {
        if(Offset + DataLength > count)
            return false;

        memset(Value, 0, DataLength);
        memcpy(Value, (void*)&buffer[Offset], DataLength);
        return true;
    }
};

When trying to compile this code, I’ve the following error message:

cannot convert from 'const BYTE' to 'char*'

What I’m doing wrong?

It look like that even for char* type, it tries to use the TProductTableItem::LoadFromBuffer function and not the TProductTableItemString::LoadFromBuffer one.
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-26T05:09:34+00:00Added an answer on May 26, 2026 at 5:09 am

    You are mixing two distinct concepts: inheritance and template specialization.

    Your class is not a template specialization but it derives from a template instantiation. However that template cannot be instantiated because when T = char * the statement Value = buffer[offset]; has a type mismatch error.

    If you want to write a template specialization then the syntax is

    template<>
    class ProductTableItem<char *> {
        ...
    };
    

    Note that two templates instantiations are in general unrelated types from an inheritance point of view…

    May be a solution for what you are trying to do is something like

    // Non-template base class
    class ProducTableItemBase {
        protected:
            ...
        public:
            virtual bool LoadFromBuffer(const BYTE* buffer, int count) = 0;
    };
    
    // Template class for all other types
    template<typename T>
    class ProductTableItem : public ProductTableItemBase {
        T Value;
        bool LoadFromBuffer(const BYTE* buffer, int count) {
            ...
        }
    };
    
    // Template specialization for char*
    template<>
    class ProductTableItem<char *> : public ProductTableItemBase {
        char * Value;
        bool LoadFromBuffer(const BYTE* buffer, int count) {
            ...
        }
    };
    

    I said may be because indeed it’s not clear to me what you are trying to accomplish.

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

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I need a function that will clean a strings' special characters. I do NOT
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want use html5's new tag to play a wav file (currently only supported
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but
I want to construct a data frame in an Rcpp function, but when I
I am doing a simple coin flipping experiment for class that involves flipping a
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.