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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T10:53:45+00:00 2026-06-11T10:53:45+00:00

I’ve been scratching my head on this one, and after sifting through all the

  • 0

I’ve been scratching my head on this one, and after sifting through all the other postings I think I’ve come up at a loss.

I decided I wanted to practice some template coding and operator overloading in C++, and so I figured a quick and dirty LinkList implementation would go fairly far. I quickly hacked up a node and list class set, as follows:

template <class T>
class Node{
public:
  Node* getNext(){ return Next; }

private:
  Node* Next;
  T data;
};

template <class T>
class List{
public:
  List& operator++();

private:
  Node<T>* nptr;
};

template <class T>
List<T>& List<T>::operator++(){
  if( nptr->getNext() ){
    nptr = nptr->getNext();
  }
  return *this;
}

main call:
int main(){
  List<int>* listObj = new listObj<int>();
  ...push on some nodes with random values...
  ++listObj;
  KABOOM
}

I’m keeping the code brief for readability. Assume that I always initialize Next to NULL when I make a node, that there’s a push function to add more nodes to the end which also sets the last node’s ‘Next’ variable to that new node, and that nptr also starts off initialized as NULL. I’ve tested all the “assumed” functionality (construction, initialization, pushing nodes) and I can even pack the operator++ function as just a increment function…

void Increment(){
  if( nptr->getNext() ){
    nptr = nptr->getNext();
  }
}

call:
listObj->Increment();

…and that works just fine. In otherwords: the compiler isn’t assigning my version of ++foo to any uses in the code! I can even prove this with the debugger, watching it increment the address of the List object rather then updating nptr like I want.

Now, I saw a suggestion of friending the function here that I gave a try:

friend List& operator++(){
  if( nptr->getNext() ){
    nptr = nptr->getNext();
  }
  return *this;
}

However when I do, I get a compilation error:

List& operator++() must have an argument of class or enumerated type

Hard-headed programmer that I am, I figured it wanted me to scope out the incoming RHS, which naturally also didn’t work. I’m forgoing writing out that broken path of reasoning because I’m fairly confident it wasn’t the right direction.

So given that the compiler seems happy with my code (or at least isn’t upset about it because its not actually assigning it anywhere), why is it still grabbing the non-templated version of the operator and treating it like an int? How would I go about correcting the code to get the compiler to use the provided function instead?

Thanks in advance!

  • 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-11T10:53:46+00:00Added an answer on June 11, 2026 at 10:53 am

    User-defined operators are never applicable to built-in types like pointers. If anything, you need to dereference the pointer to your List before using the increment operator:

    ++*listObj;
    

    Without the * the built-in increment operator is used which is clearly not what you want. I’m assuming that your KABOOM actually dereferences your pointer, though: You generally want to show the exact code where things go wrong an you want to be more precise about what actually goes wrong. I’m also assuming that you created actually compile code rather than what you quoted…

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

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I have this code to decode numeric html entities to the UTF8 equivalent character.
This could be a duplicate question, but I have no idea what search terms

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.