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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T08:53:12+00:00 2026-06-17T08:53:12+00:00

I tried to overload the ‘+’ operator and ‘=’ operator like below: template<class T>

  • 0

I tried to overload the ‘+’ operator and ‘=’ operator like below:

template<class T>
class LinkedList {     
 public:
  LinkedList();
  LinkedList(const LinkedList<T> &list);        //Copy Constructor! Allocating new memory!
  ~LinkedList();
  void PushNode(T new_data);
  void Delete(LinkedNode<T> *pnode);
  bool CopyDelete(LinkedNode<T> *pnode);
  bool CopyDeleteMiddle(); 
  void Show();
  int get_length();
  void Clear();
  LinkedList<T> operator+(LinkedList<T> &list);  //Overloading '+' operator
  LinkedList<T> &operator=(LinkedList<T> &list); //Overloading '=' operator
  LinkedNode<T> *head;
  LinkedNode<T> *tail;

 private:
  int length;
};

template <class T>
LinkedList<T>& LinkedList<T>::operator=(LinkedList<T> &list)
{
  if (&list != this) {
    Clear();
    LinkedNode<T> *pnode = list.head;
    while(pnode)
    {
      PushNode(pnode->data);
      pnode = pnode->next;
    }
  }
  return *this;
}

template <class T>
LinkedList<T> LinkedList<T>::operator+(LinkedList<T> &list)
{
  int carry = 0;
  LinkedList<T> sum_list;
  LinkedNode<T> *pnode = list.head;
  LinkedNode<T> *pnode_this = head;
  if (!pnode || !pnode_this)
    throw("Invalid list!");

  T digit, digit_this, digit_sum;
  while(pnode || pnode_this)
  {
    if (!pnode) {
      digit = 0;
      digit_this = pnode_this->data;
      pnode_this = pnode_this->next;
    } else if (!pnode_this) {
      digit = pnode->data;
      digit_this = 0;
      pnode = pnode->next;
    } else {
      digit = pnode->data;
      digit_this = pnode_this->data;
      pnode = pnode->next;
      pnode_this = pnode_this->next;
    }
    digit_sum = digit + digit_this + carry;
    if (digit_sum >=10) {
      digit_sum-=10;
      carry = 1;
    } else {
      carry = 0;
    }
    sum_list.PushNode(digit_sum);

  }
  return sum_list;
}

In my main function, I created three objects like this:

LinkedList<int> list1;
LinkedList<int> list2;
LinkedList<int> sum;

And then I added the two lists and stored the result in sum:

sum = list1+list2; //error occurred here!

All my declarations and definitions about my LinkedList class are in MySinglyLinkedList.h, and my main function is in 2_4.cc.

I compiled 2_4.cc using GCC 4.5.2 in Linux(Ubuntu), and encountered the following error:

2_4.cc: In function ‘int main(int, char**)’:

2_4.cc:39:15: error: no match for ‘operator=’ in ‘sum = LinkedList<T>::operator+(LinkedList<T>&) [with T = int](((LinkedList<int>&)(& list2)))’

MySinglyLinkedList.h:171:16: note: candidate is: LinkedList<T>& LinkedList<T>::operator=(LinkedList<T>&) [with T = int]

Is there anything wrong with my overloaded functions?

  • 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-17T08:53:12+00:00Added an answer on June 17, 2026 at 8:53 am

    The argument tot operator= should be a const reference. A temporary (the result of operator+) can’t be bound to a non-const reference.

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

Sidebar

Related Questions

I've defined a Person class (name, age). I've tried to overload += operator on
What's wrong with this C# code? I tried to overload the + operator to
I wanted to overload an operator in a class , and have it private
This is a followup question to Scala constructor overload? I'd like a case class
I tried to overload in VBA within a class module: Dim a As Integer
I tried to do something like this but this doesn't work: class Garage {
I want to overload << operator in a Line class so I can print
I tried to overload the std::find function, in this way: include <list> include <string>
I recently had a WinRT class definition with methods like this: public void Foo(string
I was a bit surprised a few minutes ago when I tried to overload

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.