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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:03:06+00:00 2026-05-14T21:03:06+00:00

I’m working on an container class template (for int,bool,strings etc), and I’ve been stuck

  • 0

I’m working on an container class template (for int,bool,strings etc), and I’ve been stuck with this error

cont.h:56: error: expected initializer before '&' token

for this section

template <typename T>
const Container & Container<T>::operator=(const Container<T> & rightCont){

what exactly have I done wrong there?.

Also not sure what this warning message means.

cont.h:13: warning: friend declaration `bool operator==(const Container<T>&, const Container<T>&)' declares a non-template function
cont.h:13: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning

at this position

template <typename T>
class Container{
    friend bool operator==(const Container<T> &rhs,const Container<T> &lhs);
public:
  • 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-14T21:03:06+00:00Added an answer on May 14, 2026 at 9:03 pm

    In the first case you’ve done things backwards. When you specify the return type, you have to include the template parameter list into the template identifier (Container<T>), but when you specify parameter type, you don’t need to do it (just Container is enough)

    template <typename T>
    const Container<T> & Container<T>::operator=(const Container & rightCont){
       ...
    

    You did it the other way around for some reason.

    In the second case, when you declare operator == as a friend it simply warns you that that in this case operator == you are referring to is an ordinary function. It can’t be a specialization of a template. I.e. for the class Container<int> the function

    bool operator==(const Container<int> &rhs, const Container<int> &lhs) {
      // ...
    }
    

    will be a friend. But specialization of function template

    template <class U> 
    bool operator==(const Container<U> &rhs, const Container<U> &lhs) {
      // ...
    }
    

    for U == int will not be a friend of Container<int>. If that’s your intent, you are OK.

    If you wanted to befriend a specific specialization of the above template, you’d have to say

    template <typename T>
    class Container {
      friend bool operator==<T>(const Container<T> &rhs, const Container<T> &lhs);
      ...
    

    If you wanted to befriend all specialization of the above template, you’d have to say

    template <typename T>
    class Container {
      template <class U> 
      friend bool operator==(const Container<U> &rhs, const Container<U> &lhs);
      ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

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.