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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:07:32+00:00 2026-05-19T17:07:32+00:00

Ahh, C++ templates… The code I see, makes sense to me, but GCC… it

  • 0

Ahh, C++ templates…

The code I see,
makes sense to me,
but GCC…
it disagrees.

The following code compiles and runs as expected, but if you uncomment that #define, you get the error, which I don’t understand. The symbol iterator still has only one thing it can refer to: the typedef in the superclass. So I guess I have two questions: 1. What do the errors mean? 2. What is the best way to fix them.

#include <map>
#include <string>
#include <cstdio>

using namespace std;

// #define WITH_TEMPLATE 1

#ifdef WITH_TEMPLATE
template <class C>
struct MyClass : public map<string, C>
#else
struct MyClass : public map<string, int>
#endif
{
    bool haskey(const string &s)
    {
        iterator it = find(s);
        return (it != end());
    }
};

int main()
{
#ifdef WITH_TEMPLATE
    MyClass<int> m;
#else
    MyClass m;
#endif
    m["test"] = 10;    
    printf("%d %d\n", m.haskey("test"), m.haskey("no"));
}

Errors from GCC:

temp.cc: In member function ‘bool MyClass::haskey(const std::string&)’:
temp.cc:18: error: missing template arguments before ‘it’
temp.cc:18: error: expected `;’ before ‘it’
temp.cc:19: error: ‘it’ was not declared in this scope
temp.cc:19: error: there are no arguments to ‘end’ that depend on a template parameter, so a declaration of ‘end’ must be available
temp.cc:19: error: (if you use ‘-fpermissive’, G++ will accept your code, but allowing the use of an undeclared name is deprecated)

  • 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-19T17:07:33+00:00Added an answer on May 19, 2026 at 5:07 pm

    You need to change your MyClass::haskey method too.

    bool haskey(const string &s)
    {
        typename MyClass<C>::iterator it = this->find(s);
        return (it != this->end());
    }
    

    Explanation of such a behavior is in section “Name lookup, templates, and accessing members of base classes” on http://physics.ucsd.edu/students/courses/winter2008/physics141/manuals/rhel-gcc-en-4/c—misunderstandings.html (link from another answer’s comment, just in case).

    Whole fixed example code: http://ideone.com/G7Rty

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

Sidebar

Related Questions

the code I'm dealing with has loops like the following: bistar = zeros(numdims,numcases); parfor
Ahh, don't you just love a good ternary abuse? :) Consider the following expression:
I had a image but cant up load it yet AHH I was able
This makes me fee like a knucklehead but how do I say previousError =
So I think I am almost there conceptually but need some missing pointers. Objective
Ahh forgive my Zend newbness, I'm trying to access this form, stored in: layouts/scripts/layout.phtml
I have a few ideas, but thought Im likely touching on a common topic
Ok the below problem is solved but now its creating leak for this block
I am running the following scenerio: SafeFileHandle handle = Win32API.CreateFile((deviceName + \\ + pipeName),
I'm new to Drupal, and the Drupal website is currently down, ahh! I need

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.