S
sleepydj
Hello,
I have a simple program to create strings from the corresponding
double/integer values. For any data type similar to int, I have a
template set up in a file misc.h:
*************************************************************************************
//misc.h
#ifndef __MISC_H__
#define __MISC_H__
#include <string>
//!Convert any numerical quantity (int, long, uint*_t, time_t, etc) to
a string
template <class X> string numtostr(X number);
//I have explicitly overloaded it for double:
//!Convert a double to a string
string numtostr(double number);
#endif
I have their corresponding bodies in the file misc.cpp. In test.cpp ,
I have the main() function (quite simple - see below).
***********************************************************************************
//test.cpp
#include <iostream>
#include <string>
#include "misc.h"
using namespace std;
int main()
{
int a=30;
cout<<numtostr(a)<<endl;
return 0;
}
************************************************************************************
//misc.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include "misc.h"
using namespace std;
template <class X> string numtostr(X number)
{
//get the length of the string
char junk;
size_t len = snprintf(&junk,1,"%d",number);
//temporary buffer to hold the string
vector<char> temp(len);
sprintf(&(temp.at(0)), "%d",number);
//put the data in the string
string result(temp.begin(), temp.end());
return result;
}
string numtostr(double number)
{
//get the length of the string
char junk;
size_t len = snprintf(&junk,1,"%f",number);
//temporary buffer to hold the string
vector<char> temp(len);
sprintf(&(temp.at(0)), "%f",number);
//put the data in the string
string result(temp.begin(), temp.end());
return result;
}
**************************************************************************************
When I try to compile this program using "g++ -o test test.cpp
misc.cpp", I get the following error:
/tmp/cc6tDQzg.o: In function `main':
test.cpp.text+0x18): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> > numtostr<int>(int)'
collect2: ld returned 1 exit status
I'm not sure why it's unable to find that template. If in main(), I
change it to float a=35.5; then it works perfectly!
I also tried to put everything in just one file. And then when I tried
to compile it, it works fine for both int and double!
I'm completely at a loss as to what is happening. Your help will be
appreicated! Just for some more details, g++ --version gives:
g++ (GCC) 4.0.1 (Debian 4.0.1-2)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Thanks,
Jimmy
I have a simple program to create strings from the corresponding
double/integer values. For any data type similar to int, I have a
template set up in a file misc.h:
*************************************************************************************
//misc.h
#ifndef __MISC_H__
#define __MISC_H__
#include <string>
//!Convert any numerical quantity (int, long, uint*_t, time_t, etc) to
a string
template <class X> string numtostr(X number);
//I have explicitly overloaded it for double:
//!Convert a double to a string
string numtostr(double number);
#endif
I have their corresponding bodies in the file misc.cpp. In test.cpp ,
I have the main() function (quite simple - see below).
***********************************************************************************
//test.cpp
#include <iostream>
#include <string>
#include "misc.h"
using namespace std;
int main()
{
int a=30;
cout<<numtostr(a)<<endl;
return 0;
}
************************************************************************************
//misc.cpp
#include <iostream>
#include <cstdlib>
#include <string>
#include <vector>
#include "misc.h"
using namespace std;
template <class X> string numtostr(X number)
{
//get the length of the string
char junk;
size_t len = snprintf(&junk,1,"%d",number);
//temporary buffer to hold the string
vector<char> temp(len);
sprintf(&(temp.at(0)), "%d",number);
//put the data in the string
string result(temp.begin(), temp.end());
return result;
}
string numtostr(double number)
{
//get the length of the string
char junk;
size_t len = snprintf(&junk,1,"%f",number);
//temporary buffer to hold the string
vector<char> temp(len);
sprintf(&(temp.at(0)), "%f",number);
//put the data in the string
string result(temp.begin(), temp.end());
return result;
}
**************************************************************************************
When I try to compile this program using "g++ -o test test.cpp
misc.cpp", I get the following error:
/tmp/cc6tDQzg.o: In function `main':
test.cpp.text+0x18): undefined reference to `std::basic_string<char,
std::char_traits<char>, std::allocator<char> > numtostr<int>(int)'
collect2: ld returned 1 exit status
I'm not sure why it's unable to find that template. If in main(), I
change it to float a=35.5; then it works perfectly!
I also tried to put everything in just one file. And then when I tried
to compile it, it works fine for both int and double!
I'm completely at a loss as to what is happening. Your help will be
appreicated! Just for some more details, g++ --version gives:
g++ (GCC) 4.0.1 (Debian 4.0.1-2)
Copyright (C) 2005 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is
NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE.
Thanks,
Jimmy