Dumb STDLib Deprecation Question

J

JShrager

I just upgraded my server to a recent version of redhat, and now old
code no longer compiles. I'm not a C++ coder, but C++ coders who use
my server are now complaining. Here's an example:


#include <iostream>
#include <list>
#include <string>

using namespace std;

template<typename T>
void writeList(const list<T>& alist, const string& separator = " ")
{
list<T>::const_iterator iter;
for(iter = alist.begin(); iter != alist.end(); iter++)
cout << *iter << separator;
cout << endl;
}


int main()
{
return 0;
}

And I get this error:

t1.cpp: In function âvoid writeList(const std::list<T,
std::allocator<_CharT> >&, const std::string\
&)â:
t1.cpp:11: error: expected `;' before âiterâ
t1.cpp:12: error: âiterâ was not declared in this scope

The user who gave me this example claims that STD lib is somehow not
installed correctly on the new server, but I can't figure out what I
could install additionally. My (completely naive) guess is that his
code is deprecated.

Here's what I can find that might be relevant:

$ cd /usr/lib
$ find . -print | grep libstd
../libstdc++.so.5
../libstdc++-3-libc6.2-2-2.10.0.so
../gcc/x86_64-redhat-linux/4.1.1/libstdc++.a
../gcc/x86_64-redhat-linux/4.1.1/32/libstdc++.a
../gcc/x86_64-redhat-linux/4.1.1/32/libstdc++.so
../gcc/x86_64-redhat-linux/4.1.1/libstdc++.so
../libstdc++.so.6
../libstdc++.so.5.0.7
../libstdc++.so.6.0.8
../libstdc++-libc6.2-2.so.3

Thanks in advance!
 
S

Salt_Peter

I just upgraded my server to a recent version of redhat, and now old
code no longer compiles. I'm not a C++ coder, but C++ coders who use
my server are now complaining. Here's an example:

#include <iostream>
#include <list>
#include <string>

using namespace std;

template<typename T>
void writeList(const list<T>& alist, const string& separator = " ")
{

// the following is a dependant type.
list<T>::const_iterator iter;

// fix:
typename list said:
for(iter = alist.begin(); iter != alist.end(); iter++)
cout << *iter << separator;
cout << endl;

}

int main()
{
return 0;

}

And I get this error:

t1.cpp: In function âvoid writeList(const std::list<T,
std::allocator<_CharT> >&, const std::string\
&)â:
t1.cpp:11: error: expected `;' before âiterâ
t1.cpp:12: error: âiterâ was not declared in this scope

The user who gave me this example claims that STD lib is somehow not
installed correctly on the new server, but I can't figure out what I
could install additionally. My (completely naive) guess is that his
code is deprecated.

The code is deprecated but considering how long dependant types have
been ignored, its really not his fault. You might consider suggesting
that he look up the use of 'typename' in the context of dependant
types. Yours is a classic example.
 
A

Aries Sun

// the following is a dependant type.


// fix:
typename list<T>::const_iterator iter;






The code is deprecated but considering how long dependant types have
been ignored, its really not his fault. You might consider suggesting
that he look up the use of 'typename' in the context of dependant
types. Yours is a classic example.

GOOD ONE. "typename" is used to clarify that const_iterator is a type
defined within class list<T>. I tried
JShrager's code on my fedora 4, the same error produced.
 
J

Jerry Coffin

I just upgraded my server to a recent version of redhat, and now old
code no longer compiles. I'm not a C++ coder, but C++ coders who use
my server are now complaining. Here's an example:

#include <iostream>
#include <list>
#include <string>

using namespace std;

template<typename T>
void writeList(const list<T>& alist, const string& separator = " ")
{
list<T>::const_iterator iter;

list<T>::const_iterator is a dependent type -- it depends on a template
parameter (T, in this case). Older compilers incorrectly accepted code
like this, but (most) more recent ones correctly require that you tell
them this is intended to be the name of a type:

typename list said:
for(iter = alist.begin(); iter != alist.end(); iter++)
cout << *iter << separator;

Just FWIW, your loop appears equivalent to:

std::copy(alist.begin(), alist.end(),
std::eek:stream_iterator<T>(cout, " "));
 
J

JShrager

list said:
list<T>::const_iterator is a dependent type -- it depends on a template
parameter (T, in this case). Older compilers incorrectly accepted code
like this, but (most) more recent ones correctly require that you tell
them this is intended to be the name of a type:

typename list<T>::const_iterator iter;

Thanks! (And thanks to the others who replied to this.) I passed on
your adivce to the user who wrote the code, and he has (I think) got
things working now.
Just FWIW, your loop appears equivalent to:

std::copy(alist.begin(), alist.end(),
std::eek:stream_iterator<T>(cout, " "));

Ain't my loop, but thanks! :)
 

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

Forum statistics

Threads
473,772
Messages
2,569,591
Members
45,102
Latest member
GregoryGri
Top