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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:44:51+00:00 2026-06-07T06:44:51+00:00

Is there a correct way of defining a member function for a template class,

  • 0

Is there a correct way of defining a member function for a template class, that returns an instance of a subclass?

Here’s an example that does not compile in VC++ 2010:

template<class T> class A {
public:
    class B {
    public:
        T i;
    };

    A();
    B* foo();
};

/////////////////////////////////////////////

template<class T> A<T>::A() {}

template<class T> A<T>::B* A<T>::foo() {
    cout << "foo" << endl;
    return new B();
}

I get

Error   8   error C1903: unable to recover from previous error(s); stopping compilation 

at the line where the definition of foo starts.

I have the correct inclusions and namespace declarations for iostream etc.

Thanks guys!

Edit:

As requested, here’s the complete list of errors, all at the same line:

Warning 1   warning C4346: 'A<T>::B' : dependent name is not a type
Error   2   error C2143: syntax error : missing ';' before '*'
Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error   4   error C1903: unable to recover from previous error(s); stopping compilation
Warning 5   warning C4346: 'A<T>::B' : dependent name is not a type
Error   6   error C2143: syntax error : missing ';' before '*'
Error   7   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
Error   8   error C1903: unable to recover from previous error(s); stopping compilation
  • 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-07T06:44:52+00:00Added an answer on June 7, 2026 at 6:44 am

    Name A<T>::B is depedent , you need to hint the compiler that depedented name is type

    template<class T> typename A<T>::B* A<T>::foo() {...}

    same for this line : return new B(); -> return new typename A<T>::B();

    Read: Where and why do I have to put the "template" and "typename" keywords?

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

Sidebar

Related Questions

Is there a correct way to read the arguments to a python application? Example:
Is there a correct way to create private static javascript variables (and functions) that
I am wondering if there is a correct way to implement the anchor tag
Firstly I know that there are many question and solutions to correct thread marshalling
Is there a correct way to getting the file size when uploading an image
Is there a correct way to place an image next to an a tag
Is there a correct way to do lazy sql writes to a database in
Is there a correct way in Windows Forms to flash a titlebar without having
Is there a 'correct' way to get the proper web address for a file
HI, Is there any 'correct' way to convert an XElement to an XmlNode in

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.