How To Convert Template To String C++
#1 Guest_Fuwex*
Template string conversion
Posted 17 December 2010 - 09:49 AM
Hey,
I'm currently writing some elementary utility functions for use in various projects. I'g struggling a little bit with an attempt to make a generic template function to convert from a string to whatsoever given type for which information technology makes sense (mostly int, double etc.).
Ideally, this part, which I telephone call fromstring(), would be called like this:
int i = fromstring<int>(mystring);
Currently, this is what I take:
template <class T> T fromstring(const std::cord& s) { std::istringstream ss(s); T t; ss >> t; return t; } The compile error I get is: undefined reference to 'int fromstring<int>(std::string const&)'
I'm adequately new to C++ and accept little feel, particularly with template functions. Can anyone shed some light upon how I tin can attain what I'yard trying to practise? Or would I perhaps be better off defining functions for each type? Thank you.
#2
Re: Template cord conversion
Posted 17 December 2010 - 10:02 AM
Mail the snippet you're running, the following works for me:
#include <iostream> #include <string> #include <sstream> using namespace std; template <grade T> T fromString(const string& south){ stringstream ss(s); T t; ss >> t; return t; } int primary(){ int exam = fromString<int>("100"); cout << test << endl; system("pause"); return 0; } #3
Re: Template string conversion
Posted 17 December 2010 - 10:32 AM
likewise if y'all have boost look at the lexical cast library.
#four Guest_Fuwex*
Re: Template string conversion
Posted 18 December 2010 - 08:30 AM
ishkabible, on 17 December 2010 - 09:32 AM, said:
also if you accept boost look at the lexical cast library.
Aye, give thanks you. This would commonly exist ideal, however I'd similar to create a set of utility functions written in pure C++ that I can carry over from project to project, so that they comprise no dependencies on external libraries.
KYA, on 17 Dec 2010 - 09:02 AM, said:
Post the snippet you're running, the following works for me:
#include <iostream> #include <string> #include <sstream> using namespace std; template <form T> T fromString(const string& due south){ stringstream ss(southward); T t; ss >> t; render t; } int main(){ int exam = fromString<int>("100"); cout << test << endl; system("pause"); return 0; } Yeah, this does indeed work for me also. At present, I've somewhat isolated my trouble. When putting the declaration in the same source file as my class using the office, and putting the function body in the same source file as the class calling the function, information technology doesn't complain when compiling.
Obviously I do however desire the declaration and definition of this function to be contained along with my other utility functions, which is what I'm struggling to do now.
I am using Qt4, however the problematic code involves very picayune or perhaps no Qt code at all (depending on how far the definition of problematic reaches).
I've got a header file (wrapperconfig.h) and a corresponding source file (wrapperconfig.cpp) for the configuration handling in my application. Information technology's very incomplete, as yous'll see.
Furthermore, I've got a header file (utilities.h) and a respective source file (utilities.cpp) for my utility functions, which includes the part in question, fromstring();
For the sake of completeness, I'll post the contents of these files here (information technology is not much):
wrapperconfig.h
#ifndef WRAPPERCONFIG_H #ascertain WRAPPERCONFIG_H #include <QString> #include "config.h" class WrapperConfig { public: WrapperConfig(config&); // Become QString getWM() const { return WM; } QString getBgImage() const { return BgImage; } int getIconHeight() const { return IconHeight; } int getIconWidth() const { return IconWidth; } // Set up void setWM(QString& due south) { WM = southward; } void setBgImage(QString& south) { BgImage = south; } void setIconHeight(int i) { IconHeight = i; } void setIconWidth(int i) { IconWidth = i; } private: void parseConfig(config&); QString WM; QString BgImage; int IconHeight; int IconWidth; }; void setIntValueByString(int& i, const std::cord& s);

0 Response to "How To Convert Template To String C++"
Post a Comment