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

  • Home
  • SEARCH
  • 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 6652465
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T01:08:31+00:00 2026-05-26T01:08:31+00:00

I have a templated Stack class implemented internally with vector. Here is the content

  • 0

I have a templated Stack class implemented internally with vector.

Here is the content of my (simplified) TStack.h:

#include <vector>
#include <iostream>

template<typename T> class TStack;
template<typename T> TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2);

template<typename T>
class TStack {
    friend TStack<T> operator+<>(const TStack<T> &s1, const TStack<T> &s2);
    private:
        std::vector<T> items;
    public:
        void printAll() {
            std::cout << "The content of the stack is: ";
            typename std::vector<T>::iterator it;
            for(it = items.begin(); it < items.end(); it++) {
                std::cout << *it << " ";
            }
            std::cout << std::endl;
        }
};

template<typename T>
TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2) {
    TStack<T> result = s1;
    typename std::vector<T>::iterator it;
    //below is line 41
    for(it = s2.items.begin(); it < s2.items.end(); it++) {
        result.items.push_back(*it);
    }
    return result;
}

And this is my (simplified) main class:

#include <iostream>
#include "TStack.h"

using namespace std;

int main(int argc, char *argv[]) {
    TStack<int> intStack;
    intStack.push(4);

    TStack<int> secondIntStack;
    secondIntStack.push(10);

    cout << "Addition result: " << endl;
    //below is line 27
    TStack<int> result = intStack + secondIntStack;
    result.printAll();
    return 0;
}

And this is the compilation result:

In file included from main.cpp:2:
TStack.h: In function ‘TStack<T> operator+(const TStack<T>&, const TStack<T>&) [with T = int]’:
main.cpp:27:   instantiated from here
TStack.h:41: error: no match for ‘operator=’ in ‘it = s2->TStack<int>::items.std::vector<_Tp, _Alloc>::begin [with _Tp = int, _Alloc = std::allocator<int>]()’
/usr/include/c++/4.4/bits/stl_iterator.h:669: note: candidates are: __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >& __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >::operator=(const __gnu_cxx::__normal_iterator<int*, std::vector<int, std::allocator<int> > >&)
make: *** [main.exe] Error 1

I have no idea what is the meaning of the error message.

In the operator+ function, I used the same way to get the iterator inside the printAll(), but it doesn’t work properly inside the operator+ function.
I know I can just avoid using the iterator in the operator+ function, but I am just curious on how to fix this.

  • 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-26T01:08:32+00:00Added an answer on May 26, 2026 at 1:08 am

    Use const_iterator instead of iterator:

    typename std::vector<T>::const_iterator it;
    

    Because s1 is a const object. So s1.items will also be const object as well, which means s1.items.begin() will return const_iterator, not non-const iterator .


    Better implementation of operator+()

    You can improve the implementation ofoperator+(). Instead of using a manual loop, and push_back function, you can use insert function as:

    template<typename T>
    TStack<T> operator+(const TStack<T> &s1, const TStack<T> &s2) {
        TStack<T> result(s1); //use direct copy-initialization
        result.insert(result.end(), s2.begin(), s2.end());
        return result;
    }
    

    It completely the avoids the problem of iterator which you face in your code.


    More better implementation of operator+()

    If you accept the first argument by value, instead of const reference, then that is even better:

    template<typename T>
    TStack<T> operator+(TStack<T> s1, const TStack<T> &s2) {
        s1.insert(s1.end(), s2.begin(), s2.end()); //s1 is a copy, after all!
        return s1; 
    }
    

    As the first argument is a copy itself, you don’t need to create a local variable called result explicitly. You simply can add s2 to s1 and return s1.

    • 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>
We have two classes: template<typename T, typename Size, typename Stack, typename Sparse> class Matrix
template <typename T> class Stack { private: std::vector<T> elems; // elements public: Stack(); //
For my compsci class, I am implementing a Stack template class, but have run
I have a templated class defined (in part) as template <class T> MyClass {
I have to write a stack class template using arrays in C++ for my
I have nested iterator in my custom stack template class. The problem I get
I have a custom stack class. Most of the code can be seen here:
I have a templated class template<class U, class V, class W> class S {
I have 3 C++ files: genericStack.h: template <class T> class Stack{ public: Stack (int

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.