2 compile problems

V

Victor Hannak

I have the following 2 files as part of my project:

GenFuncs.h
------------
#ifndef GenFuncsH
#define GenFuncsH
#include <iostream.h>
#include <strstream.h>
using namespace std;

//template <class T> string str(T In);
string Int2Hex(int In);

template <class T> string str(T In) {
ostrstream oss;
oss << In;
return(oss.str());
}

#endif

GenFuncs.cpp
------------
#pragma hdrstop
#include "GenFuncs.h"
#pragma package(smart_init)
string Int2Hex(int In) {
ostrstream oss;
oss << hex << In;
return(oss.str());
}


Now, if I compile this code "as is" in Borland, it compiles OK, and I am
able to use both the Int2Hex() and str() functions in my other code that
#includes GenFuncs.h.

Problem #1

when I try to compile in gnu with:

g++ -g -c GenFuncs.cpp

I get

GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex

It seems that it does not recognize the string identifier (which I don't
understand, because I am using namespace std)

Problem #2

Ideally, I would like to have the declaration for the str() function in the
GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
this (uncomment the declaration and move the body to .cpp), then I get
linker errors in Borland like thus:

[Linker Error] Unresolved external 'std::basic_string<char,
std::char_traits<char>, std::allocator<char> > str<double>(double)'
referenced from C:\PROGRAMS\PROJ\TESTCLASS.OBJ.

I get a linker error for each function call to the template function. Note
that if I replace the template declaration and body with overloaded
non-template versions, then it compiles fine.

Any help is appreciated...

Vic
 
G

Gianni Mariani

Victor said:
I have the following 2 files as part of my project:

GenFuncs.h

#include said:
#include <strstream.h>
using namespace std;

//template <class T> string str(T In);
string Int2Hex(int In);

template <class T> string str(T In) {
ostrstream oss;
oss << In;
return(oss.str());
}

#endif

GenFuncs.cpp
------------
#pragma hdrstop
#include "GenFuncs.h"
#pragma package(smart_init)
string Int2Hex(int In) {
ostrstream oss;
oss << hex << In;
return(oss.str());
}


Now, if I compile this code "as is" in Borland, it compiles OK, and I am
able to use both the Int2Hex() and str() functions in my other code that
#includes GenFuncs.h.

Problem #1

when I try to compile in gnu with:

g++ -g -c GenFuncs.cpp

I get

GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex

It seems that it does not recognize the string identifier (which I don't
understand, because I am using namespace std)

Problem #2

Ideally, I would like to have the declaration for the str() function in the
GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
this (uncomment the declaration and move the body to .cpp), then I get
linker errors in Borland like thus:

[Linker Error] Unresolved external 'std::basic_string<char,
std::char_traits<char>, std::allocator<char> > str<double>(double)'
referenced from C:\PROGRAMS\PROJ\TESTCLASS.OBJ.

I get a linker error for each function call to the template function. Note
that if I replace the template declaration and body with overloaded
non-template versions, then it compiles fine.

Any help is appreciated...

Try this. It may solve problem 2, if not then I suggest you contact a
borland NG.

::::::::::::::
GenFuncs.h
::::::::::::::
#ifndef GenFuncsH
#define GenFuncsH
#include <iostream>
#include <sstream>
using namespace std;

//template <class T> string str(T In);
string Int2Hex(int In);

template <class T> string str(T In) {
ostringstream oss;
oss << In;
return(oss.str());
}

#endif

::::::::::::::
GenFuncs.cpp
::::::::::::::
#pragma hdrstop
#include "GenFuncs.h"
#pragma package(smart_init)
string Int2Hex(int In) {
ostringstream oss;
oss << hex << In;
return(oss.str());
}
 
K

Kevin Goodsell

Victor said:
I have the following 2 files as part of my project:

GenFuncs.h

Non-standard header. Use said:
#include <strstream.h>

Non-standard header. Use said:
using namespace std;

Bad idea in a header file. The person #including your header may not
want the entire std namespace included.
//template <class T> string str(T In);
string Int2Hex(int In);

What is 'string'? If you mean std::string, you'd better include the
appropriate header ( said:
template <class T> string str(T In) {
ostrstream oss;

Use std::eek:stringstream instead.
oss << In;
return(oss.str());
}

#endif

GenFuncs.cpp

This is non-standard.
#include "GenFuncs.h"
#pragma package(smart_init)

This is non-standard.
string Int2Hex(int In) {
ostrstream oss;
oss << hex << In;
return(oss.str());
}


Now, if I compile this code "as is" in Borland, it compiles OK,

That would be luck.
and I am
able to use both the Int2Hex() and str() functions in my other code that
#includes GenFuncs.h.

Problem #1

when I try to compile in gnu with:

g++ -g -c GenFuncs.cpp

I get

GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex

It seems that it does not recognize the string identifier (which I don't
understand, because I am using namespace std)

That isn't the issue. 'using namespace std' does not tell the compiler
what 'string' is. You do that by #including said:
Problem #2

Ideally, I would like to have the declaration for the str() function in the
GenFuncs.h file and the body in the GenFuncs.cpp file.

Read a little bit about templates. Unless your compiler supports
'export' (I only know of 1 that does), you are out of luck.

http://www.parashift.com/c++-faq-lite/containers-and-templates.html#faq-34.13

-Kevin
 
A

Artie Gold

Victor said:
I have the following 2 files as part of my project:

GenFuncs.h

No such header in standard C++ (iostream.h is a holdover from
pre-standard days).

#include said:
#include <strstream.h>

Similarly.

#include <sstream>

For the error below:

#include said:
using namespace std;

Putting a using directive like this in a header is a Bad Idea, as it
pollutes the namespace in a way that might not be immediately
apparent. It would be better to use fully qualified names.
//template <class T> string str(T In);
string Int2Hex(int In);

You didn't include said:
template <class T> string str(T In) {
ostrstream oss;
oss << In;
return(oss.str());
}

#endif

GenFuncs.cpp

Non standard. Please remove non standard things from code you post
to
#include "GenFuncs.h"
#pragma package(smart_init)
string Int2Hex(int In) {
ostrstream oss;
oss << hex << In;
return(oss.str());
}


Now, if I compile this code "as is" in Borland, it compiles OK, and I am
able to use both the Int2Hex() and str() functions in my other code that
#includes GenFuncs.h.

Problem #1

when I try to compile in gnu with:

g++ -g -c GenFuncs.cpp

I get

GenFuncs.h:8 syntax error before '(' // This is the declaration for Int2Hex

It seems that it does not recognize the string identifier (which I don't
understand, because I am using namespace std)

But you didn't `#include said:
Problem #2

Ideally, I would like to have the declaration for the str() function in the
GenFuncs.h file and the body in the GenFuncs.cpp file. However, if I do
this (uncomment the declaration and move the body to .cpp), then I get
linker errors in Borland like thus:

[Linker Error] Unresolved external 'std::basic_string<char,
std::char_traits<char>, std::allocator<char> > str<double>(double)'
referenced from C:\PROGRAMS\PROJ\TESTCLASS.OBJ.

I get a linker error for each function call to the template function. Note
that if I replace the template declaration and body with overloaded
non-template versions, then it compiles fine.

Since your compilers do not support the `export' keyword (few do,
only Comeau comes to mind, though I think there may be others), all
templated code must be visible in any translation unit in which it
is used.

HTH,
--ag
 
V

Victor Hannak

Thanks for the great suggestions, they helped me get over those hurdles.

However, there is something that is unclear to me in all of this...

You all told me to use the <sstream> header in conjunction with the
ostringstream type. This worked fine in Borland, but gnu choked. However,
when I switched back to <strstream> and ostrstream, both compilers accepted
it just fine. Of course my C++ textbook (C++ How to Program by
Deitel/Deitel) still uses the <strstream.h> notation. Can someone just
comment on the difference between <strstream> and <sstream>.

After looking at bok reviews on www.accu.org, I am thinking that "The C++
Programming Language Special Edition", by Bjarne Stroustrup seems like my
best bet for an updated C++ general reference book. Does this sound right?
 
P

Peter van Merkerk

After looking at bok reviews on www.accu.org, I am thinking that "The
C++
Programming Language Special Edition", by Bjarne Stroustrup seems like my
best bet for an updated C++ general reference book. Does this sound
right?

Yes.
 
?

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

Victor Hannak escribió:
You all told me to use the <sstream> header in conjunction with the
ostringstream type. This worked fine in Borland, but gnu choked. However,
when I switched back to <strstream> and ostrstream, both compilers accepted

If you have an old version of gcc you can download a sstream file from
gcc.gnu.org (the address is in the faq of the site, I think), is not a
complete standard implementation but works in many uses.

Regards.
 
P

Pete Becker

Gianni said:
#include <iostream> // iostream.h is deprecated - don't used them

Actually, it's not deprecated. It's ignored. <g> In the standard,
"deprecated" means that something is currently part of the language, but
you're warned that it may go away in a subsequent version of the
standard. Since iostream.h was never part of the C++ standard, there's
nothing to deprecate.
 

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,141
Messages
2,570,817
Members
47,362
Latest member
ChandaWagn

Latest Threads

Top