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