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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:26:28+00:00 2026-06-13T00:26:28+00:00

I have the following code: #include <iostream> #include <stdio.h> using namespace std; template <class

  • 0

I have the following code:

#include <iostream>
#include <stdio.h>
using namespace std;

template <class F>
struct CMPLX {

    F Re, Im;

    struct _printnice {
        F Re, Im;
        string sep;
        _printnice(const F& Re, const F& Im, const string& sep) : Re(Re), Im(Im), sep(sep) {}
    };

    CMPLX <F> (F Re, F Im) : Re(Re), Im(Im) {}

    _printnice PrintNice(const string& sep="\t"){
        return _printnice(Re, Im, sep);
    }

};

template<class F>
ostream& operator << (ostream& os, const CMPLX<F> c){
    cout << c.Re << " + " << c.Im << "i";
}

template<class F>
ostream& operator << (ostream& os, const CMPLX<F> :: _printnice p){
    cout << p.Re << p.sep << p.Im;
}

int main(){
    CMPLX<float> c(2.0,1.0);
    cout << c << endl;
    cout << c.PrintNice() << endl;

}

I introduce a sub-struct _printnice in order to overload the operator << and have differently-formatted output of my CMPLX class. However, this throws an error
expected unqualified-id before ‘p’ and I don’t know how to solve this (my knowledge on templates is very limited).

I try to change the 2nd definition of << to the following which works, but I have to specify the type, which is frowned-upon:

ostream& operator << (ostream& os, const CMPLX <float> :: _printnice p){
    cout << p.Re << p.sep << p.Im;
}
  • 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-13T00:26:29+00:00Added an answer on June 13, 2026 at 12:26 am

    There are two issues with your approach. The first is that _printnice is a dependent name and as such you need to add an extra typename. If that were fixed you would end up with:

    template<class F>
    ostream& operator << (ostream& os, typename CMPLX<F>::_printnice const & p)
    

    The problem with this code, as Dietmar pointed out in a previous answer, is that F is in a non deducible context, and this will fail.

    One simple solution is to define the operator inside the scope of the _printnice code, where the typename:

    template <class F>
    struct CMPLX {
       //...
       struct _printnice {
          friend std::ostream& operator<<( std::ostream& o, _printnice const & p ) {
             // print here
             return o;
          }
       }
       //...
    };
    

    Inside the definition of _printnice, the type is known to be a type and thus the typename is no longer required. The overloaded operator<< will be found by Argument Dependent Lookup, and because it refers to this particular instantiation of the type, there is no template arguments to deduce.

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

Sidebar

Related Questions

I have following code: #include <iostream> using namespace std; template<class T> T max(T *data,int
I have written the following code: #include <iostream> using namespace std; template <class T>
I have the following code: #include <iostream> using namespace std; template <class T> class
I have the following code: #include <iostream> #include <vector> using namespace std; struct A{};
I have the following code compiled by gcc: #include <iostream> using namespace std; class
I have the following code: #include <iostream> using namespace std; struct stud{ int roll;
I have the following code: #include <iostream> using namespace std; class testing{ int test()
I have the following code : #include <cstdarg> #include <iostream> using namespace std; class
i have following code #include <iostream> #include <string> using namespace std; string generate(){ for
I have the following code: #include<iostream> #include<stdio.h> using namespace std; int main(){ char* username,

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.