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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:26:10+00:00 2026-06-11T14:26:10+00:00

I am trying to overload operator<< in my code. If I comment out the

  • 0

I am trying to overload operator<< in my code. If I comment out the lines where I try to use the << operator with my custom class, it compiles fine. The error almost looks like it doesn’t like the c++ libraries (?).

All my research on this problem has stated that it is a linking problem. Most suggest using g++ instead of gcc. I am using g++ as my compiler, and I get this error still.

Code:

#include <iostream>
using namespace std;

//prototype the class and the functions
template<class T> class strange;
template<class T> ostream& operator<< (ostream& osObject, strange<T>& sObject);


//begin class
template <class T>
class strange
{
    public:
        // .... function prototypes go here.
            strange(T x,T y);
            friend ostream& operator<< <> (ostream& osObject, strange<T>& sObject);

    private:
    T a;
    T b;
};
// .... your function definitions go here
template <class T>
        strange<T>::strange(T first, T second){
        a = first;
        b = second;
}

template <class T>
ostream& operator<< (ostream& osObject, const strange<T>& sObject){
        osObject << sObject.a << ", " << sObject.b;
        return osObject;
}



int main()
{
    strange<int> x1(4,6) , x2(12,2) ;
    //strange<char> y1('m','n') , y2('m','n') ;
    cout << "x1 = " << x1 << endl;
    return 0;
}

Error:

test.cpp:(.text+0x7a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& operator<< <int>(std::basic_ostream<char, std::char_traits<char> >&, strange<int>&)'
collect2: ld returned 1 exit status

Any idea what is causing 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-06-11T14:26:11+00:00Added an answer on June 11, 2026 at 2:26 pm

    I made two changes, one to the friend definition, and one to the prototype. This should compile:

    #include <iostream>
    using namespace std;
    
    //prototype the class and the functions
    template<class T> class strange;
    template<class T> ostream& operator<< (ostream& osObject, const strange<T>& sObject);
    
    
    //begin class
    template <class T>
    class strange
    {
        public:
            // .... function prototypes go here.
                strange(T x,T y);
                friend ostream& operator<< <> (ostream& osObject, const strange<T>& sObject);
    
        private:
        T a;
        T b;
    };
    // .... your function definitions go here
    template <class T>
            strange<T>::strange(T first, T second){
            a = first;
            b = second;
    }
    
    template <class T>
    ostream& operator<< (ostream& osObject, const strange<T>& sObject){
            osObject << sObject.a << ", " << sObject.b;
            return osObject;
    }
    
    
    
    int main()
    {
        strange<int> x1(4,6) , x2(12,2) ;
        //strange<char> y1('m','n') , y2('m','n') ;
        cout << "x1 = " << x1 << endl;
        return 0;
    }
    

    And this compiles in clang, g++ and at ideone

    To explain the issue, the compiler is looking at link time for a definition for:

     std::ostream & operator<< <int>(std::ostream &, strange<int>&);
    

    When you only have a definition for:

     std::ostream & operator<< <int>(std::ostream &, strange<int> const &);
    

    This is because of the miscommunication between your prototypes (both the explicit one and the friend) and your definition.

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

Sidebar

Related Questions

I'm trying to overload the '--' postfix operator. I have this code: class Counter
I'm trying to overload the dereference operator, but compiling the following code results in
I am trying to overload the subscript operator ([]) on an abstract class, the
Hey, I'm trying to figure out if it's possible to overload a template class
I'm trying to overload operator<< in the standard way. I have a class called
I'm trying to overload the operator << as a friend to a template class
I am trying to overload the c++ operator== but im getting some errors... error
I'm trying to use a List<T> with a custom class of mine, and being
I am trying to overload the << operator, but I get the following error:
I'm trying to overload the instream operator >> for a fraction class. I've created

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.