Incorrect template code, or compiler/library bug?

M

Michael T Decerbo

The code below compiles without warnings or errors under g++ 3.2.1 on
Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.

Under g++ (GCC) 3.3.2, it produces the following errors:

% g++ -Wall testme_std.cpp
testme_std.cpp:24: error: no `I LblID<I, MaxInd>::add(const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
member function declared in class `LblID<I, MaxInd>'
testme_std.cpp:24: error: template definition of non-template `I LblID<I,
MaxInd>::add(const std::basic_string<char, std::char_traits<char>,
std::allocator<char> >&)'
testme_std.cpp: In member function `I LblID<I, MaxInd>::add(const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)':
testme_std.cpp:15: error: `typedef typename std::map<std::string, I,
::iterator LblID<I, MaxInd>::lbl_iter' is inaccessible
testme_std.cpp:25: error: within this context

Is the error in the code, or in the compiler?

In general, is there some way I can answer this question in the future--
an online syntax checker, or something?

Thanks, everyone.


Mike Decerbo
(e-mail address removed)

(code follows)

#include <map>
#include <vector>
#include <string>

using namespace std;

template <class I, size_t MaxInd>
class LblID
{
public:

map< string, I > lbl2id;
vector<string> id2lbl;

typedef typename map< string, I >::iterator lbl_iter;

I add(const string& lbl);

};


template <class I, unsigned int MaxInd>
I LblID<I,MaxInd>::add(const string& lbl)
{
lbl_iter lbli = lbl2id.find(lbl);
if (lbli.compare(lbl2id.end()) != 0)
return (*lbli).second;

if (size() >= MaxInd)
return MaxInd;

I id = size();
lbl2id[lbl] = id;
id2lbl.push_back(lbl);
return id;
}

int main (int argc, char *argv[])
{

}
 
?

=?iso-8859-1?Q?Juli=E1n?= Albo

Michael T Decerbo escribió:
The code below compiles without warnings or errors under g++ 3.2.1 on
Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.

Under g++ (GCC) 3.3.2, it produces the following errors:

Compiles fine for me with:

# gcc --version
gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)

Perhaps you are using the standard library corresponding to other
version?

Regards.
 
G

Guest

The code below compiles without warnings or errors under g++ 3.2.1 on
Solaris, gcc 3.2.2 on Linux, and MSVC 6.0 on Windows.

Under g++ (GCC) 3.3.2, it produces the following errors:

% g++ -Wall testme_std.cpp
testme_std.cpp:24: error: no `I LblID<I, MaxInd>::add(const
std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)'
member function declared in class `LblID<I, MaxInd>'
testme_std.cpp:24: error: template definition of non-template `I LblID<I,
MaxInd>::add(const std::basic_string<char, std::char_traits<char>, // cut

using namespace std;

template <class I, size_t MaxInd>
class LblID

// cut
template <class I, unsigned int MaxInd>
I LblID<I,MaxInd>::add(const string& lbl)
{
lbl_iter lbli = lbl2id.find(lbl);
if (lbli.compare(lbl2id.end()) != 0)
return (*lbli).second;
if (size() >= MaxInd)
return MaxInd;
I id = size();
lbl2id[lbl] = id;
id2lbl.push_back(lbl);
return id;
}

int main (int argc, char *argv[])
{

}
is the size_t the same type as unsigned int ?
 
M

Michael T Decerbo

Compiles fine for me with:

# gcc --version
gcc (GCC) 3.3 20030226 (prerelease) (SuSE Linux)

Perhaps you are using the standard library corresponding to other
version?

You are correct. The system I was using is an Apple Mac OS X machine,
which has some version of the standard library installed under
/usr/include/gcc.

On a Linux system with a fresh install of 3.3.2 there is no error.

Thanks for the help!


Mike
 

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
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top