This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: error: 'cit' was not declared in this scope


You are focusing on the second error, but that was just a consequence of the first.

During the initial parsing, the compiler doesn't know that types from the base class are types, because it doesn't know what the base class will be until it has the template actuals.

You can tell it that types are types using the typename keyword in each place where you use them.

I think that is a bad style, and it is better to bring such types into the current class with a typedef as in

      typedef std::map<Key, Value> base_type;
      typedef typename base_type::const_iterator const_iterator;
...
      const_iterator cit = base_type::find(key);

But in similar style, I also think "using" is better than individual overrides for functions that should be inherited from the base class:

      typedef std::map<Key, Value> base_type;
      typedef typename base_type::const_iterator const_iterator;
      using base_type::find;
...
      const_iterator cit = find(key);

Gerry Sweeney wrote:
Hi,

I am trying to port some code from VC9 to Linux using "gcc (Ubuntu 4.4.1-4ubuntu8) 4.4.1" and I am getting an error with GCC that I don't get when compiling under VC9.

test.cpp: In member function 'const Value& enums_map<Key, Value>::operator[](const Key&) const':
test.cpp:30: error: expected ';' before 'cit'
test.cpp:31: error: 'cit' was not declared in this scope


The problem is I need to create a local variable of type <the base class>::const_iterator but I cant work out how I can do this. As I mentioned, the code below works when compiling under VC9 but not with g++. I can't work out how to solve this problem, could anyone help? This is the code sample (just compiling with "g++ test.cpp"

---------------------------------<snip>-------------------------------------

#include <stdio.h>
#include <string>
#include <iostream>
#include <tr1/regex>
#include <inttypes.h>
#include <stdarg.h>

#include <map>


// Just the std::map, plus a const version of operator[] which returns a const reference to a const member template<typename Key, typename Value> class enums_map : public std::map<Key, Value> {
typedef std::map<Key, Value> base_type;


public:
       enums_map()
               : default_value_()
       {
       }

       enums_map(const Value& default_value)
               : default_value_(default_value)
       {
       }

       const Value& operator[] (const Key& key) const
       {
               base_type::const_iterator cit = base_type::find(key);
               if (cit == base_type::end())
                       return default_value_;
               else
                       return cit->second;
       }

using base_type::operator[];

private:
const Value default_value_;
};
---------------------------------<snip>-------------------------------------



Any help very much appreciated


Thanks,

Gerry






Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]