member function template question....

B

BigBrian

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.
 
J

Jonathan Mcdougall

BigBrian said:
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);

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
 
B

BigBrian

Thus I've tried putting "typename std::map said:
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
 
J

Jonathan Mcdougall

BigBrian said:
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
 
G

Greg

BigBrian said:
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
 
S

sherinekhoury

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_;
};
 
S

Shezan Baig

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-
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,677
Members
48,796
Latest member
Greg L.

Latest Threads

Top