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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:24:22+00:00 2026-06-12T16:24:22+00:00

I am working with stacks and need to check to see if two are

  • 0

I am working with stacks and need to check to see if two are the same. I have overloaded the

bool operator== 

function in my code, and now I need to put the logic in the code. I will check a few things to see if the two stacks are the same: the length, the data type, and the content of each element. Length and content are no problem, its the data type that is giving me issues.

I tried to make a function:

...
    Type getType();
};

template <class Type>
Type getType(){ returnType;}

But this did not work.

I also thought about:

bool operator== (stack<Type> &lhs, stack<Type> &rhs){

    return (lsh.Type == rhs.Type);
//additional conditions will be checked.
}

How to I check if they are the same type?

EDIT: What if I just checked the data type of the top elements of the two stacks? would that be sufficient?

  • 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-12T16:24:23+00:00Added an answer on June 12, 2026 at 4:24 pm

    If you implement operator== like this:

    template<typename Type>
    bool operator== (const stack<Type> &lhs, const stack<Type> &rhs){
        // compare and return.
    }
    

    You already know that both stacks will contain elements of the same type. There’s no need to check for that.

    If you want to allow stacks using different template arguments to be compared, you could do something like this:

    template<typename Type1, typename Type2>
    bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
        // compare
    }
    

    Then compare the elements using operator== and you’ll be done. Of course, if there’s no operator== for parameters of type Type1 and Type2, then the compiler will issue an error.

    Edit: Since you want a nice error message, you could do this(using C++11):

    #include <type_traits>
    
    template<typename Type1, typename Type2>
    bool operator== (const stack<Type1> &lhs, const stack<Type2> &rhs){
        // This is a compile time assert!
        static_assert(std::is_same<Type1, Type2>::value, "Types are not equal!");
        // compare
    }
    

    I’d avoid this anyway. It’s simpler to let the compiler issue its own error, rather than doing this check yourself.

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

Sidebar

Related Questions

I have been spending the day working on come c++ code that I need
I am a Java programmer working with C++ code, and need some help with
In a Silverlight interface I'm working on I have the need to resize a
We have two machines which need to communicate: a Linux based gateway, and a
I've been working with OpenGL for about a year now, and have learned a
In many situation need to rotate the controller and is not working. Right now
I have been working on something that checks an MySQL Database to check something
Working with an undisclosed API, I found a function that can set the number
I am working on a project and need to add an additional image using
I'm currently working on my own PHP Framework, and I need some help figuring

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.