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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T16:29:27+00:00 2026-05-23T16:29:27+00:00

I’m new to C++ and I’m trying to use template but I got problems.

  • 0

I’m new to C++ and I’m trying to use template but I got problems.
What I’m trying to do is: try to calculate square of a number using template, and the number may be basic data types like int, float, as well as complex numbers. I also implemented a complex class using template, and the codes are as follows:

template <typename T>
class Complex {
public:
  T real_;
  T img_;

  Complex(T real, T img) : real_(real), img_(img) { } 
};

template <typename T>
T square(T num) {
  return num * num;
}

template <>
Complex<typename T> square(Complex<typename T> num) {
  T temp_real = num.real_*num.real_ - num.img_*num.img_;
  T temp_img  = 2 * num.img_ * num.real_;
  return Complex(temp_real, temp_img);
}

I tried to use template specialization to deal with the special case, but it gave me error:

using ‘typename’ outside of template

and the error happens on the template specialization method. Please point out my mistakes. Thanks.

  • 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-23T16:29:28+00:00Added an answer on May 23, 2026 at 4:29 pm

    It appears that you’re trying to partially specialize function templates, which isn’t actually possible in C++. What you want instead is to simply overload the function like this:

    template<typename T>
    T square(T num) // Overload #1
    { 
        return num * num;
    }
    
    template<typename T>
    Complex<T> square(Complex<T> num) // Overload #2
    {
        T temp_real = num.real_*num.real_ - num.img_*num.img_;
        T temp_img  = 2 * num.img_ * num.real_;
        return Complex<T>(temp_real, temp_img);
    }
    

    Informally, the compiler will always pick overload #2 over overload #1 when the argument is of type Complex<T> because it’s a better match.


    Another way to make this work is to overload the multiplication operator for the Complex<> class using the definition of multiplication for complex numbers. This has the advantage of being more general and you can extend this idea to other operators.

    template <typename T>
    class Complex
    {
    public:
        T real_; 
        T img_; 
    
        Complex(T real, T img) : real_(real), img_(img) {} 
    
        Complex operator*(Complex rhs) // overloaded the multiplication operator
        {
            return Complex(real_*rhs.real_ - img_*rhs.img_,
                img_*rhs.real_ + real_*rhs.img_);
        }
    };
    
    // No overload needed. This will work for numeric types and Complex<>.
    template<typename T>
    T square(T num)
    {
        return num * num;
    }
    

    Since you are new to C++, I highly recommend that you pick up a good introductory C++ book. Templates and operator overloading aren’t exactly beginner’s topics.

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

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I am trying to understand how to use SyndicationItem to display feed which is
I want use html5's new tag to play a wav file (currently only supported
I'm trying to use string.replace('’','') to replace the dreaded weird single-quote character: ’ (aka
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of 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.