By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,435 Members | 1,493 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,435 IT Pros & Developers. It's quick & easy.

member function template question....

P: n/a
I'm working a legecy system and have been tasked with upgrading the
compiler from gcc 2.95.2 to 3.4.2. I have the following class which
contains several std::map's. For simplicity, here I've created two a
map from int to int and int to double. The keys are "schronized", ie
all items are added in the set of maps by way of the add() member
function. There is a template function that basically handles the
error of the items not being in the map when remove is called. This
code compiles with 2.95.2 but does not with 3.4.2....

Here's a representation of the real code..

#include <iostream>
#include <map>

class A
{
private:
std::map<int,int> int_map;
std::map<int,double> double_map;

template< class T >
void helper( std::map<int,T> & t, int value );

public:
void add( int key, int int_value, double double_value )
{
int_map[key] = int_value;
double_map[key] = double_value;
}

void remove( int key )
{
helper( int_map, key);
helper( double_map, key );
}
};

template< class T >
void A::helper( std::map<int,T> & t,int value )
{
std::map<int,T>::iterator i = t.find( value );

if ( i == t.end() )
{
std::cerr << "not found" << std::endl;
}
else
{
t.erase(i);
}
}
int main()
{
A a;

a.add( 1, 2, 5.4 );
a.remove( 1 );

return 0;
}

I get the following compiler error:

error: expected ';' before "i"
error: 'i' undeclared (first use this function)
.....
......

The errors go on to say...
error: dependent-name 'std::map<int,T>::iterator' is parsed as a
non-type but instanciation yields a type.

Thus I've tried putting "typename std::map<int,T>::iterator" on the
line before i is instanciated, but that doesn't help.
I realize that it's probably not the best design to have multiple maps
to store information like this, but I can't change this now.

Any information will be appreciated.

Oct 24 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a

BigBrian wrote:
I'm working a legecy system and have been tasked with upgrading the
compiler from gcc 2.95.2 to 3.4.2. I have the following class which
contains several std::map's. For simplicity, here I've created two a
map from int to int and int to double. The keys are "schronized", ie
all items are added in the set of maps by way of the add() member
function. There is a template function that basically handles the
error of the items not being in the map when remove is called. This
code compiles with 2.95.2 but does not with 3.4.2....

Here's a representation of the real code..

#include <iostream>
#include <map>

class A
{
private:
std::map<int,int> int_map;
std::map<int,double> double_map;

template< class T >
void helper( std::map<int,T> & t, int value );

public:
void add( int key, int int_value, double double_value )
{
int_map[key] = int_value;
double_map[key] = double_value;
}

void remove( int key )
{
helper( int_map, key);
helper( double_map, key );
}
};

template< class T >
void A::helper( std::map<int,T> & t,int value )
{
std::map<int,T>::iterator i = t.find( value );
typename std::map<int, T>::iterator i = t.find(value);

<snip>
I get the following compiler error:

error: expected ';' before "i"
error: 'i' undeclared (first use this function)
....
.....

The errors go on to say...
error: dependent-name 'std::map<int,T>::iterator' is parsed as a
non-type but instanciation yields a type.

Thus I've tried putting "typename std::map<int,T>::iterator" on the
line before i is instanciated, but that doesn't help.


Are you sure you did?
Jonathan

Oct 24 '05 #2

P: n/a
> > Thus I've tried putting "typename std::map<int,T>::iterator" on the
line before i is instanciated, but that doesn't help.


Are you sure you did?


Yes, I'm sure. The compiler then says the following for the line
containing
"typename std::map<int,T>::iterator"

error: declaration does not declare anything

-Brian

Oct 24 '05 #3

P: n/a
BigBrian wrote:
Thus I've tried putting "typename std::map<int,T>::iterator" on the
line before i is instanciated, but that doesn't help.


Are you sure you did?


Yes, I'm sure. The compiler then says the following for the line
containing
"typename std::map<int,T>::iterator"

error: declaration does not declare anything


Well, this compiles fine:

#include <iostream>
#include <map>

class A
{
private:
std::map<int,int> int_map;
std::map<int,double> double_map;

template< class T >
void helper( std::map<int,T> & t, int value );

public:
void add( int key, int int_value, double double_value )
{
int_map[key] = int_value;
double_map[key] = double_value;
}

void remove( int key )
{
helper( int_map, key);
helper( double_map, key );
}

};

template< class T >
void A::helper( std::map<int,T> & t,int value )
{
typename std::map<int,T>::iterator i = t.find( value );

if ( i == t.end() )
{
std::cerr << "not found" << std::endl;
}
else
{
t.erase(i);
}

}

int main()
{
A a;

a.add( 1, 2, 5.4 );
a.remove( 1 );

return 0;

}

on Comeau and Visual C++ 7.0. If it doesn't on your compiler, you may
get more accurate answers in a newsgroup supporting it.
Jonathan

Oct 24 '05 #4

P: n/a

BigBrian wrote:
Thus I've tried putting "typename std::map<int,T>::iterator" on the
line before i is instanciated, but that doesn't help.


Are you sure you did?


Yes, I'm sure. The compiler then says the following for the line
containing
"typename std::map<int,T>::iterator"

error: declaration does not declare anything


The typename keyword must be on the same line and on every line that
declares an iterator:

typename std::map<int,T>::iterator i = t.find( value );

Not on the line before the declaration for i.

Greg

Oct 25 '05 #5

P: n/a
Thank you. That was the problem.

-Brian

Oct 25 '05 #6

P: n/a
Hello Greg,

I have a similar problem as BigBrian there.
I'm using GCC 3.4.2 to recompile jade1.2.1 for SPARC Solaris 2.9.
Anyway, the compilation goes wrong because of the following code.

Initializing ptr_ to 0 for example like this :
typename Vector<RangeMapRange<From,To> >::const_iterator ptr_=0
does not prevent the following error message from stopping the make:

../../include/RangeMap.h:57: error: expected `;' before "ptr_"
../../include/RangeMap.h: In member function `Boolean RangeMapIter<From,
To>::next(From&, From&, To&)':
../../include/RangeMap.h:47: error: `ptr_' undeclared (first use this
function)
../../include/RangeMap.h:47: error: (Each undeclared identifier is
reported only once for each function it appears in.)

Any advice you can give me?

Best regards,
Shérine

__________________________________________________ ______
template<class From, class To>
class RangeMapIter {
public:
RangeMapIter(const RangeMap<From,To> &map);
Boolean next(From &fromMin, From &fromMax, To &toMin) {
if (!count_)
return 0;
else {
fromMin = ptr_->fromMin;
fromMax = ptr_->fromMax;
toMin = ptr_->toMin;
ptr_++;
count_--;
return 1;
}
}
private:
size_t count_;
typename Vector<RangeMapRange<From,To> >::const_iterator ptr_;
};

Greg wrote:
BigBrian wrote:
> Thus I've tried putting "typename std::map<int,T>::iterator" on the
> line before i is instanciated, but that doesn't help.

Are you sure you did?


Yes, I'm sure. The compiler then says the following for the line
containing
"typename std::map<int,T>::iterator"

error: declaration does not declare anything


The typename keyword must be on the same line and on every line that
declares an iterator:

typename std::map<int,T>::iterator i = t.find( value );

Not on the line before the declaration for i.

Greg


Nov 9 '05 #7

P: n/a
sh***********@gmail.com wrote:
Hello Greg,

I have a similar problem as BigBrian there.
I'm using GCC 3.4.2 to recompile jade1.2.1 for SPARC Solaris 2.9.
Anyway, the compilation goes wrong because of the following code.

Initializing ptr_ to 0 for example like this :
typename Vector<RangeMapRange<From,To> >::const_iterator ptr_=0
does not prevent the following error message from stopping the make:

./../include/RangeMap.h:57: error: expected `;' before "ptr_"
./../include/RangeMap.h: In member function `Boolean RangeMapIter<From,
To>::next(From&, From&, To&)':
./../include/RangeMap.h:47: error: `ptr_' undeclared (first use this
function)
./../include/RangeMap.h:47: error: (Each undeclared identifier is
reported only once for each function it appears in.)

Any advice you can give me?

[snip]
typename Vector<RangeMapRange<From,To> >::const_iterator ptr_;
};

C++ is case sensitive. You probably meant "typename
std::vector<Rang...." There is no "Vector" in Standard C++.

Hope this helps,
-shez-

Nov 9 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.