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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T10:54:27+00:00 2026-06-14T10:54:27+00:00

I have a template Node below for storing some data in an array. Before

  • 0

I have a template Node below for storing some data in an array. Before adding I want to check if an entry is present with the same value (my insert logic needs it). For string type, i want to implement a specific method for comparison.

template <class T> class Node
{
private:
    short noOfEntries;
    T data[MAX_VALUES];
public:
    Node () { noOfEntries = 0; }
    int Compare(int index, T *key);
    int Insert(T *key);
};

template <class T>
int Node<T>::Compare(int index, T *key)
{
    if(data[index] > *key)
        return 1;
    else if(data[index] == *key)
        return 0;
    return -1;
}

template <>
class Node <string> {
  public:
    int Compare(int index, string *key)
    {
        return (data[index].compare(*key));
    }
};

This gives error as the attributes ‘data’ and ‘noOfEntries’ are not in class Node <string>.
It seems that I will have to put ALL the attributes from Node to the specialized version for string. Same thing for methods too.

Is there a better way so that I will have just one insert method defined for Node which will invoke correct compare() method depending on the actual type of T ? I want to avoid duplication of the methods.

  • 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-06-14T10:54:28+00:00Added an answer on June 14, 2026 at 10:54 am

    Just specialize the one member:

    template <> int Node<std::string>::Compare(int index, std::string *key)
        {
            return (data[index].compare(*key));
        }
    

    A more idiomatic way to do this would be to use a comparison policy template argument, or possibly a traits describing the default comparison policy for the element type.

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

Sidebar

Related Questions

I have template function compare defined as below. #include<iostream> using namespace std; template<typename T>
i have template base.html and some children templates. I don't know how to set
I have an XSL template that is selected for execution (below). What I would
I have the template <xsl:template match=node mode=some_mode> <xsl:value-of select=child1 /> <xsl:value-of select=child2 /> <xsl:value-of
i want to write a urldecode template filter and i have a template tag
I've created a custom template tag and want to have a helper method in
I have defined a generic tree-node class like this: template<class DataType> class GenericNode {
I have a statement in my template.php file that directs to a custom node-myccktype.tpl.php.
I have an array of node IDs with which loop through and run node_load($nid)
I have some code below. This code is a basic push/pop stack class that

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.