4 Replies - 700 Views - Last Post: 04 January 2010 - 09:10 AM Rate Topic: -----

#1 CodingDesire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

A problem create two functions

Posted 04 January 2010 - 07:44 AM

Hello everyone. I'm trying to create two functions, one which converts a template parameter to string, and another which does the opposite. However, I do not know why I'm getting a big, strange error when trying to compile this:

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

template <class T>
string T_as_string(T param)
{
	ostringstream ost;
	ost << param;

	return ost;
}

template <class T>
T string_as_T(const string param)
{
	T t;
	istringstream ist(param);
	ist >> t;

	return t;
}

int main()
{
	string Str = T_as_string<float>(3.139);
	cout << "Float to string: " << Str << endl;

	int P = string_as_T<int>("3941");
	cout << "String to int: " << P << endl;

	return 0;
}



And the error that I'm getting:
1>c:\documents and settings\admin\my documents\visual studio 2008\projects\test\test\src.cpp(27) : warning C4305: 'argument' : truncation from 'double' to 'float'
1>c:\documents and settings\admin\my documents\visual studio 2008\projects\test\test\src.cpp(12) : error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it)' : cannot convert parameter 1 from 'std::ostringstream' to 'std::basic_string<_Elem,_Traits,_Ax>::_Has_debug_it'
1>		with
1>		[
1>			_Elem=char,
1>			_Traits=std::char_traits<char>,
1>			_Ax=std::allocator<char>
1>		]
1>		No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
1>		c:\documents and settings\admin\my documents\visual studio 2008\projects\test\test\src.cpp(27) : see reference to function template instantiation 'std::string T_as_string<float>(T)' being compiled
1>		with
1>		[
1>			T=float
1>		]



I have no idea what's going on.

This post has been edited by CodingDesire: 04 January 2010 - 07:52 AM


Is This A Good Question/Topic? 0
  • +

Replies To: A problem create two functions

#2 FrozenSnake  Icon User is offline

  • En man från Sverige!

Reputation: 122
  • View blog
  • Posts: 998
  • Joined: 30-July 08

Re: A problem create two functions

Posted 04 January 2010 - 08:11 AM

Check this: http://www.cplusplus...rial/templates/
it's about templates

I got this after reading the first error:
main.cpp(12) : error C2440: 'return' : cannot convert from 'std::ostringstream' to 'double'

Maybe can help you to find out whats wrong :]

This post has been edited by FrozenSnake: 04 January 2010 - 08:16 AM

Was This Post Helpful? 1
  • +
  • -

#3 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3116
  • View blog
  • Posts: 19,153
  • Joined: 14-September 07

Re: A problem create two functions

Posted 04 January 2010 - 08:16 AM

ost is a ostringstream not a std::string, so you want to return the str equivalent of the stringstream:

template <class T>
string T_as_string(T param){
	ostringstream ost;
	ost << param;

	return ost.str(); //here
}


Was This Post Helpful? 1
  • +
  • -

#4 NickDMax  Icon User is offline

  • Can grep dead trees!
  • member icon

Reputation: 2250
  • View blog
  • Posts: 9,245
  • Joined: 18-February 07

Re: A problem create two functions

Posted 04 January 2010 - 08:17 AM

in function T_as_string:

return ost.str();
Was This Post Helpful? 1
  • +
  • -

#5 CodingDesire  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 27
  • Joined: 21-December 09

Re: A problem create two functions

Posted 04 January 2010 - 09:10 AM

And it worked :-D Every problem that I had, someone here helped me to resolve it. Thank you all for this site, it helps me a lot!

This post has been edited by CodingDesire: 04 January 2010 - 09:20 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1