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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T22:58:41+00:00 2026-06-14T22:58:41+00:00

Possible Duplicate: overloading friend operator<< for template class I’m trying to overload operator<< for

  • 0

Possible Duplicate:
overloading friend operator<< for template class

I’m trying to overload operator<< for a template class but I’m getting errors…


Final(fixed) code:

template<class T>
class mytype
{
    T atr;
public:
    mytype();
    mytype(T);
    mytype(mytype&);
    T getAtr() const;
    T& operator=(const T&);
    template<class U> friend ostream& operator<<(ostream&,const mytype<U>&);
};

template<class T>
mytype<T>::mytype()
{
    atr=0;
}

template<class T>
mytype<T>::mytype(T value)
{
    atr=value;
}

template<class T>
mytype<T>::mytype(mytype& obj)
{
    atr=obj.getAtr();
}


template<class T>
T mytype<T>::getAtr() const
{
    return atr;
}

template<class T>
T& mytype<T>::operator=(const T &other)
{
    atr=other.getAtr();
    return *this;
}

template<class U>
ostream& operator<<(ostream& out,const mytype<U> &obj)
{
    out<<obj.getAtr();
    return out;
}

(All in the header file)


VS2012 errors:

1)

Error 1 error LNK2019: unresolved external symbol “public: __thiscall mytype::mytype(int)” (??0?$mytype@H@@QAE@H@Z) referenced in function _wmain

2)

Error 2 error LNK2019: unresolved external symbol “class std::basic_ostream > & __cdecl operator<<(class std::basic_ostream > &,class mytype const &)” (??6@YAAAV?$basic_ostream@DU?$char_traits@D@std@@@std@@AAV01@ABV?$mytype@H@@@Z) referenced in function _wmain

3)

Error 3 error LNK1120: 2 unresolved externals


What’s wrong with my code?

  • 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-14T22:58:42+00:00Added an answer on June 14, 2026 at 10:58 pm

    You told the compiler to expect a free non-template function:

    friend ostream& operator<<(ostream&,const mytype<T>&);
    

    …but then you defined a function template instead:

    template<class T>
    ostream& operator<<(ostream& out,const mytype<T> &obj)
    {
        out<<obj.getAtr();
        return out;
    }
    

    Tell the compiler instead to expect a function template:

    template<class T> friend ostream& operator<<(ostream&,const mytype<T>&);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Java operator overload In c++, we can perform the operator overloading. But
Possible Duplicate: overload operator<< within a class in c++ Operator overloading Is there any
Possible Duplicate: Operator Overloading with C# Extension Methods How can i overload those operators,
Possible Duplicate: Operator Overloading in C++ as int + obj I have a class
Possible Duplicate: Operator Overloading in PHP Is there a way to overload the =
Possible Duplicate: Operator Overloading in PHP I am trying to create a library that
Possible Duplicate: Overloading += in c++ Do I need to overload the += operator
Possible Duplicate: Operator overloading I was wonder how can I over load the Conditional
Possible Duplicate: Overloading assignment operator in C# I remember I saw this question somewhere
Possible Duplicate: Operator Overloading in C++ as int + obj I override the *

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.