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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:45:30+00:00 2026-05-26T07:45:30+00:00

template <class T> class Test{ public: Test(T){ } template <class U> U to() const{

  • 0
template <class T>
class Test{

public:
  Test(T){ }

  template <class U>
  U to() const{ return U(0); }
};


template <class Real>
class Base{

  typedef Test<Real> TST;

protected:
  Base(const TST& t){

    _i = t.to<int>();  // <- but this gives error
  }

  int _i;
};


template <class Real, class T> class Child;

template <class Real>
class Child<Real, int> : public Base<Real>{

  typedef Base<Real> F;
  typedef Test<Real> TST;

public:
  Child() : F(TST(23.0)){ }
};



int main(int argc, char* argv[]){

  Child<double, int> rw;

  Test<double> t1(3.3);
  int i = t1.to<int>();   // <- this works

  return 0;
}

Calling to in the main works, but I can’t figure out why it doesn’t when called in Base(). The error I get is:

t1.cpp: In constructor ‘Base<Real>::Base(const TST&)’:
t1.cpp:20:15: error: expected primary-expression before ‘int’
t1.cpp:20:15: error: expected ‘;’ before ‘int’
  • 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-26T07:45:31+00:00Added an answer on May 26, 2026 at 7:45 am

    Because t is of type Test<Real> where Real is a template argument, so the type of t is actually a dependent type. You need to let the compiler know to is a template function by using:

    _i = t.template to<int>();
    

    Otherwise you could be attempting to use the operator< on a member of t called to (causing some errors), which is what the compiler assumes unless you mark it template. When called from main, the type of t1 is Test<double> which is not dependent on any template argument so the compiler can figure on its own its a template function.

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

Sidebar

Related Questions

check this out: template <class T> class Test: public T { public: void TestFunc()
Consider the following code: template<int* a> class base {}; int main() { base<(int*)0> test;
I have a class: class foo { private: std::string data; public: foo &append(const char*
How do I inherit from a virtual template class, in this code: // test.h
Could someone explain me why this code: class safe_bool_base { //13 protected: typedef void
I have this sample code: struct A { bool test() const { return false;
I have a template class like below. template<int S> class A { private: char
I have got a template class as follows: class MyClass<T> { T field; public
I have a template class MyTemplate. It works fine. But as soon as I
Why is this code not valid? #include <vector> template <typename T> class A {

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.