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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T05:24:24+00:00 2026-05-15T05:24:24+00:00

When I try to compile this: #include <map> #include <string> template <class T> class

  • 0

When I try to compile this:

#include <map>
#include <string>

template <class T>
class ZUniquePool
{               
        typedef std::map< int, T* > ZObjectMap;
        ZObjectMap      m_objects;
public:
    T * Get( int id )
    {
        ZObjectMap::const_iterator it = m_objects.find( id );
        if( it == m_objects.end() )
        {
            T * p = new T;
            m_objects[ id ] = p;
            return p;
        }
        return m_objects[ id ];
    }
};

int main( int argc, char * args )
{
    ZUniquePool< std::string > pool;
    return 0;
}

I get this:

main.cpp: In member function ‘T* ZUniquePool<T>::Get(int)’:
main.cpp:12: error: expected `;' before ‘it’
main.cpp:13: error: ‘it’ was not declared in this scope

I’m using GCC 4.2.1 on Mac OS X.
It works in VS2008.

I’m wondering whether it might be a variation of this problem:
Why doesn't this C++ template code compile?
But as my error output is only partially similar, and my code works in VS2008, I am not sure.

Can anyone shed some light on what I am doing wrong?

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

    You need typename:

    typename ZObjectMap::const_iterator it = m_objects.find( id )
    

    Since the type of ZObjectMap is dependent on the template parameter, the compiler has no clue what ZObjectMap::const_iterator is (it could be a member variable). You need to use typename to inform the compiler to assume it will be some sort of type.

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

Sidebar

Related Questions

When I try to compile this: import java.awt.* ; class obj { public static
When I try to compile this: public static Rand searchCount (int[] x) { int
When i try to compile this: public static int compareCardhl (Card c1, Card c2)
This compiles: class Ex1 { public int show() { try { int a=10/10; return
I've try to compile this code: #include <iostream> #include <cstdlib> using namespace std; #define
When I try and compile the following code... #include <vector> template <class T> void
when I try to compile this code: #include <boost/filesystem/path.hpp> #include <boost/filesystem/fstream.hpp> using namespace std;
#include <iostream> using namespace std; void initializeMap(int mapSizeX, int mapSizeY, int map[][10]) { //
When I try to compile this code: #include <windows.h> namespace cmb1 { } void
I compile this program: #include <stdio.h> int main() { printf(Hello World!); return 0; }

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.