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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T05:06:46+00:00 2026-05-19T05:06:46+00:00

For the following code: #include <map> #include <iostream> #include <string> using namespace std; template

  • 0

For the following code:

#include <map>
#include <iostream>
#include <string>

using namespace std;

template <class T>
class Foo{
  public:
    map<int, T> reg;
    map<int, T>::iterator itr;

    void add(T  str, int num) {
      reg[num] = str;
    }

    void print() {
      for(itr = reg.begin(); itr != reg.end(); itr++) {
        cout << itr->first << " has a relationship with: ";
        cout << itr->second << endl;
      }
    }
};

int main() {
  Foo foo;
  Foo foo2;
  foo.add("bob", 10);
  foo2.add(13,10);
  foo.print();
  return 0;
}

I get the error:

 type std::map<int, T, std::less<int>, std::allocator<std::pair<const int, T> > > is not derived from type Foo<T>

I’ve never used C++ templates – What does this mean?

  • 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-19T05:06:46+00:00Added an answer on May 19, 2026 at 5:06 am

    You’re missing the type when you declare instances of Foo.

    In your case, you would want:

      Foo<std::string> foo;
      Foo<int> foo2;
    

    You will also need to add the keyword typename to the line:

        typename map<int, T>::iterator itr;
    

    See here for why you’ll need typename.

    Edit, here’s a modified version of your code that compiles and runs locally:

    #include <map>
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    template <class T>
    class Foo{
    public:
        map<int, T> reg;
        typename map<int, T>::iterator itr;
    
        void add(T  str, int num) {
            reg[num] = str;
        }
    
        void print() {
            for(itr = reg.begin(); itr != reg.end(); itr++) {
                cout << itr->first << " has a relationship with: ";
                cout << itr->second << endl;
            }
        }
    };
    
    int main() {
        Foo<std::string> foo;
        Foo<int> foo2;
        foo.add("bob", 10);
        foo2.add(13,10);
        foo.print();
        return 0;
    }
    
    • 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> #include <string> using namespace std; string generate(){ for
I have the following code: #include <map> #include <string> class policy1 { public: struct
Consider the following code: #include <stdio.h> namespace Foo { template <typename T> void foo(T
The following code prints 20, i.e. sizeof(z) is 20. #include <iostream.h> class Base {
The following code: #include <vector> #include <algorithm> struct myStructDim { int nId; int dwHeight;
I have written the following code #include <iostream> #include <boost/asio.hpp> #include <boost/date_time/posix_time/posix_time.hpp> #include <boost/filesystem.hpp>
Test the following code: #include <stdio.h> #include <stdlib.h> main() { const char *yytext=0; const
I have the following bit of legacy C++ code that does not compile: #include
I have a C# application that includes the following code: string file = relativePath.txt;
The following code doesn't compile with gcc, but does with Visual Studio: template <typename

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.