473,378 Members | 1,309 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

template function problem

Dear all,

I have the following code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int str_to_int (const string str)
{
istringstream str_s (str);
int num;
str_s >num;
return num;
}

template <typename T>
string num_to_str (const T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}

int main (void)
{
int i = 3, j = 5;
string str = num_to_str (j);
i += str_to_int (str);
cout << i << endl;
getchar();
return 0;
}

As you can see from the function "num_to_str", it has been tested with
various numeric data types such as short, float, double, ..etc. and it
works just fine.
However, the function "str_to_int" can also be replaced with
"str_to_float", "str_to_double" and so third and so fourth with just
replacing the int to float, int to double, ..etc.
So i thought of making it a template function as well and changed the
function "str_to_int" into:

template <typename T>
T str_to_num (const string str)
{
istringstream str_s (str);
T num;
str_s >num;
return num;
}
but i get the following error:
28 no matching function for call to `str_to_num(std::string&)'

Can any one see what's wrong?

Oct 1 '06 #1
8 3014
In article <11**********************@h48g2000cwc.googlegroups .com>,
"coosa" <co*****@gmail.comwrote:
Dear all,

I have the following code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int str_to_int (const string str)
{
istringstream str_s (str);
int num;
str_s >num;
return num;
}

template <typename T>
string num_to_str (const T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}

int main (void)
{
int i = 3, j = 5;
string str = num_to_str (j);
i += str_to_int (str);
cout << i << endl;
getchar();
return 0;
}

As you can see from the function "num_to_str", it has been tested with
various numeric data types such as short, float, double, ..etc. and it
works just fine.
However, the function "str_to_int" can also be replaced with
"str_to_float", "str_to_double" and so third and so fourth with just
replacing the int to float, int to double, ..etc.
So i thought of making it a template function as well and changed the
function "str_to_int" into:

template <typename T>
T str_to_num (const string str)
{
istringstream str_s (str);
T num;
str_s >num;
return num;
}
but i get the following error:
28 no matching function for call to `str_to_num(std::string&)'

Can any one see what's wrong?
You need to tell it, at the place it is being called, what type to
convert to. As in:

double d = str_to_num<double>( myString );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 1 '06 #2

Daniel T. wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
"coosa" <co*****@gmail.comwrote:
Dear all,

I have the following code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int str_to_int (const string str)
{
istringstream str_s (str);
int num;
str_s >num;
return num;
}

template <typename T>
string num_to_str (const T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}

int main (void)
{
int i = 3, j = 5;
string str = num_to_str (j);
i += str_to_int (str);
cout << i << endl;
getchar();
return 0;
}

As you can see from the function "num_to_str", it has been tested with
various numeric data types such as short, float, double, ..etc. and it
works just fine.
However, the function "str_to_int" can also be replaced with
"str_to_float", "str_to_double" and so third and so fourth with just
replacing the int to float, int to double, ..etc.
So i thought of making it a template function as well and changed the
function "str_to_int" into:

template <typename T>
T str_to_num (const string str)
{
istringstream str_s (str);
T num;
str_s >num;
return num;
}
but i get the following error:
28 no matching function for call to `str_to_num(std::string&)'

Can any one see what's wrong?

You need to tell it, at the place it is being called, what type to
convert to. As in:

double d = str_to_num<double>( myString );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
Thanks alot, it worked; i just thought that specifying the type is only
required by class templates and not function templates!

Oct 1 '06 #3
Daniel T. wrote:
In article <11**********************@h48g2000cwc.googlegroups .com>,
"coosa" <co*****@gmail.comwrote:
Dear all,

I have the following code:

#include <iostream>
#include <string>
#include <sstream>

using namespace std;

int str_to_int (const string str)
{
istringstream str_s (str);
int num;
str_s >num;
return num;
}

template <typename T>
string num_to_str (const T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}

int main (void)
{
int i = 3, j = 5;
string str = num_to_str (j);
i += str_to_int (str);
cout << i << endl;
getchar();
return 0;
}

As you can see from the function "num_to_str", it has been tested with
various numeric data types such as short, float, double, ..etc. and it
works just fine.
However, the function "str_to_int" can also be replaced with
"str_to_float", "str_to_double" and so third and so fourth with just
replacing the int to float, int to double, ..etc.
So i thought of making it a template function as well and changed the
function "str_to_int" into:

template <typename T>
T str_to_num (const string str)
{
istringstream str_s (str);
T num;
str_s >num;
return num;
}
but i get the following error:
28 no matching function for call to `str_to_num(std::string&)'

Can any one see what's wrong?

You need to tell it, at the place it is being called, what type to
convert to. As in:

double d = str_to_num<double>( myString );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
By the way, shouldn't the same apply to the function "num_to_str" as:
string str = num_to_str <int(j); ?
but it works fine with: string str = num_to_str (j);

Oct 1 '06 #4
"coosa" <co*****@gmail.comwrote:
By the way, shouldn't the same apply to the function "num_to_str" as:
string str = num_to_str <int(j); ?
but it works fine with: string str = num_to_str (j);
No, the same doesn't apply because the compiler can infer the type by
looking at the parameter's type.

You could set your function up like iostream's op>and then you
wouldn't have to specify the type.

template < typename T >
void string_to_num( const string& str, T& num )
{
//...
}

Now you would call it like:

double d;
string_to_num( myString, d );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 1 '06 #5

Daniel T. wrote:
"coosa" <co*****@gmail.comwrote:
By the way, shouldn't the same apply to the function "num_to_str" as:
string str = num_to_str <int(j); ?
but it works fine with: string str = num_to_str (j);

No, the same doesn't apply because the compiler can infer the type by
looking at the parameter's type.

You could set your function up like iostream's op>and then you
wouldn't have to specify the type.

template < typename T >
void string_to_num( const string& str, T& num )
{
//...
}

Now you would call it like:

double d;
string_to_num( myString, d );

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
Ok Daniel,
Thanks for the help so far; I'd like to disturb you a little more! :-)
Have a look at this header file:

//Begin of File converter.hpp
#ifndef converterHPP
#define converterHPP

#include <iostream>

using namespace std;

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (string);
};

class number
{
public:
template <typename T>
static string to_string (T);
};
}

#endif
//End of File converter.hpp

and then at this source file:

//Begin of File converter.cpp
#include "converter.hpp"
#include <sstream>

using namespace std;

namespace convert
{
template <typename T>
T string::to_number (string str)
{
T num;
istringstream str_s (str);
str_s >num;
return num;
}

template <typename T>
string number::to_string (T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}
}
//End of File converter.cpp

and finally at this mail file:

//Begin of File main.cpp
#include "converter.hpp"
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
float f1 = 3.4;
int i1 = 5;
string s1 = "7.023";
double d1 = 12345.80346;
float f2 = convert::string::to_number <float(s1); // causes error
string s2 = convert::number::to_string <double(d1); //causes
error
system("PAUSE");
return EXIT_SUCCESS;
}
//End of File main.cpp

I get the folllowing error log

Compiler: Default compiler
Building Makefile: "C:\Dokumente und
Einstellungen\Administrator\Desktop\test3\Makefile .win"
Executing make...
make.exe -f "C:\Dokumente und
Einstellungen\Administrator\Desktop\test3\Makefile .win" all
g++.exe -D__DEBUG__ -c main.cpp -o main.o
-I"C:/Dev-Cpp/lib/gcc/mingw32/3.4.2/include"
-I"C:/Dev-Cpp/include/c++/3.4.2/backward"
-I"C:/Dev-Cpp/include/c++/3.4.2/mingw32"
-I"C:/Dev-Cpp/include/c++/3.4.2" -I"C:/Dev-Cpp/include" -g3

main.cpp: In function `int main(int, char**)':

main.cpp:14: error: no matching function for call to
`convert::string::to_number(std::string&)'
converter.hpp:14: note: candidates are: static T
convert::string::to_number(convert::string) [with T = float]

main.cpp:15: error: conversion from `convert::string' to non-scalar
type `std::basic_string<char, std::char_traits<char>,
std::allocator<char' requested

make.exe: *** [main.o] Error 1

Execution terminated

Could you please check it out and tell me what's wrong in the code?

Best regards

Oct 1 '06 #6
coosa schrieb:
Have a look at this header file:

//Begin of File converter.hpp
#ifndef converterHPP
#define converterHPP

#include <iostream>

using namespace std;

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (string);
};

class number
{
public:
template <typename T>
static string to_string (T);
};
}
The problem is, that you declare a class named string and use a string as
parameter, which should be std::string but instead refers to your string class.

So my advice:
1) _never_ put "using namespace std;" in a header file.
2) don't name your own classes like standard library classes.

Make it string_to_number() or just toString() and fromString().

Also, take a look at the FAQ:
http://www.parashift.com/c++-faq-lit...al-issues.html

--
Thomas
http://www.netmeister.org/news/learn2quote.html
Oct 1 '06 #7
"coosa" <co*****@gmail.comwrote:

The error in your code has to do with the 'string' class you have
created in your header file. You are using std::string in your main
function, but convert::string in your 'convert' namespace.

Notes in the code:
Ok Daniel,
Thanks for the help so far; I'd like to disturb you a little more! :-)
Have a look at this header file:

//Begin of File converter.hpp
#ifndef converterHPP
#define converterHPP

#include <iostream>

using namespace std;
Don't put using declarations in headers, qualify each name instead.
>
namespace convert
{
class string
{
public:
template <typename T>
static T to_number (string);
};
Above you created your own string class "convert::string". Now the
compiler is assuming that any reference to "string" within this
namespace is really a reference to "convert::string" and not
"std::string".

Also, templated functions should be defined in the header file so the
definition is available at all points of use.
class number
{
public:
template <typename T>
static string to_string (T);
};
Above you created a class called 'number'. I get the impression you are
coming from Java? You don't need to put static functions inside classes.
}

#endif
//End of File converter.hpp
Try this for a header file instead:

#ifndef converterHPP
#define converterHPP

#include <iostream>

template < typename T >
T to_number( const std::string& s ) {
T result;
std::istringstream iss( s );
iss >result; // what if this fails?
// Currently the result will be undefined.
return result;
}

template < typename T >
std::string to_string( const T& val ) {
std::string result;
std::ostringstream oss;
oss << val; // what if this fails?
return oss.str();
}

#endif // converterHPP
and then at this source file:

//Begin of File converter.cpp
#include "converter.hpp"
#include <sstream>

using namespace std;

namespace convert
{
template <typename T>
T string::to_number (string str)
{
T num;
istringstream str_s (str);
str_s >num;
return num;
}

template <typename T>
string number::to_string (T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}
}
//End of File converter.cpp
The above assumes convert::string for both references to 'string'. Your
code won't work with std::string as a result.
and finally at this mail file:

//Begin of File main.cpp
#include "converter.hpp"
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
float f1 = 3.4;
int i1 = 5;
string s1 = "7.023";
double d1 = 12345.80346;
float f2 = convert::string::to_number <float(s1); // causes error
Your attempting to pass in a std::string instead of a convert::string.
string s2 = convert::number::to_string <double(d1); //causes
error
Your attempting to assign a convert::string to a std::string, but have
defined no way to do that.
system("PAUSE");
return EXIT_SUCCESS;
}
//End of File main.cpp

I get the folllowing error log

main.cpp:14: error: no matching function for call to
`convert::string::to_number(std::string&)'
The compiler can't find the above function, because one doesn't exist.
converter.hpp:14: note: candidates are: static T
convert::string::to_number(convert::string) [with T = float]
The above is what you do have (note "convert::string" inside the parens
instead of "std::string".)
main.cpp:15: error: conversion from `convert::string' to non-scalar
type `std::basic_string<char, std::char_traits<char>,
std::allocator<char' requested
And here, the compiler says you are trying to convert from a
"convert::string" to a "std::string" (which is a typedef for
"std::basic_string<char, std::char_traits<char>, std::allocator<char.)

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer*have anyone to discuss your doubts with,
nor any ability to discuss them.
Oct 1 '06 #8

Daniel T. wrote:
"coosa" <co*****@gmail.comwrote:

The error in your code has to do with the 'string' class you have
created in your header file. You are using std::string in your main
function, but convert::string in your 'convert' namespace.

Notes in the code:
Ok Daniel,
Thanks for the help so far; I'd like to disturb you a little more! :-)
Have a look at this header file:

//Begin of File converter.hpp
#ifndef converterHPP
#define converterHPP

#include <iostream>

using namespace std;

Don't put using declarations in headers, qualify each name instead.

namespace convert
{
class string
{
public:
template <typename T>
static T to_number (string);
};

Above you created your own string class "convert::string". Now the
compiler is assuming that any reference to "string" within this
namespace is really a reference to "convert::string" and not
"std::string".

Also, templated functions should be defined in the header file so the
definition is available at all points of use.
class number
{
public:
template <typename T>
static string to_string (T);
};

Above you created a class called 'number'. I get the impression you are
coming from Java? You don't need to put static functions inside classes.
}

#endif
//End of File converter.hpp

Try this for a header file instead:

#ifndef converterHPP
#define converterHPP

#include <iostream>

template < typename T >
T to_number( const std::string& s ) {
T result;
std::istringstream iss( s );
iss >result; // what if this fails?
// Currently the result will be undefined.
return result;
}

template < typename T >
std::string to_string( const T& val ) {
std::string result;
std::ostringstream oss;
oss << val; // what if this fails?
return oss.str();
}

#endif // converterHPP
and then at this source file:

//Begin of File converter.cpp
#include "converter.hpp"
#include <sstream>

using namespace std;

namespace convert
{
template <typename T>
T string::to_number (string str)
{
T num;
istringstream str_s (str);
str_s >num;
return num;
}

template <typename T>
string number::to_string (T num)
{
ostringstream o_str;
o_str << num;
return o_str.str();
}
}
//End of File converter.cpp

The above assumes convert::string for both references to 'string'. Your
code won't work with std::string as a result.
and finally at this mail file:

//Begin of File main.cpp
#include "converter.hpp"
#include <cstdlib>
#include <iostream>
#include <string>

using namespace std;

int main(int argc, char *argv[])
{
float f1 = 3.4;
int i1 = 5;
string s1 = "7.023";
double d1 = 12345.80346;
float f2 = convert::string::to_number <float(s1); // causes error

Your attempting to pass in a std::string instead of a convert::string.
string s2 = convert::number::to_string <double(d1); //causes
error

Your attempting to assign a convert::string to a std::string, but have
defined no way to do that.
system("PAUSE");
return EXIT_SUCCESS;
}
//End of File main.cpp

I get the folllowing error log

main.cpp:14: error: no matching function for call to
`convert::string::to_number(std::string&)'

The compiler can't find the above function, because one doesn't exist.
converter.hpp:14: note: candidates are: static T
convert::string::to_number(convert::string) [with T = float]

The above is what you do have (note "convert::string" inside the parens
instead of "std::string".)
main.cpp:15: error: conversion from `convert::string' to non-scalar
type `std::basic_string<char, std::char_traits<char>,
std::allocator<char' requested

And here, the compiler says you are trying to convert from a
"convert::string" to a "std::string" (which is a typedef for
"std::basic_string<char, std::char_traits<char>, std::allocator<char.)

--
There are two things that simply cannot be doubted, logic and perception.
Doubt those, and you no longer have anyone to discuss your doubts with,
nor any ability to discuss them.
Thanks alot, that was very helpful,

I do like java alot indeed, but i started learning C++ before Java;
it's just that i didn't tough C++ for quite some time, so my brain have
got rusted! :-)

Oct 1 '06 #9

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: C. Carbonera | last post by:
/* Hi, I have a problem with explicit instantiation of templates in Visual C++ 6.0. I have provided the source below. I have an example of a function template that produces incorrect output in...
0
by: Chris F Clark | last post by:
In our C++ project we have some internal bug reporting macros that we use to get useful information when the program does something unexpected. Essentially at the point of the error, we invoke an...
4
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline. However, as soon as I try to seperate the class into a...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: Chris | last post by:
I am having a very strange problem involving virtual functions in template classes. First of all, here is an extremely simplified structure of the two classes I am having problems with. ...
7
by: hopangtsun | last post by:
Hi all, I have encountered a problem on using the function template my goal is to add two numbers, which they can be int or double if I do this in this way template<class T> T addition(T a, T...
19
by: aaragon | last post by:
Hi everyone. A very simple question. I would like to know what is better in terms of performance. I want to use a simple function to obtain the minimum of two values. One way could be using a...
3
by: Hamilton Woods | last post by:
Diehards, I developed a template matrix class back around 1992 using Borland C++ 4.5 (ancestor of C++ Builder) and haven't touched it until a few days ago. I pulled it from the freezer and...
5
by: krishnaroskin | last post by:
Hey all, I've been running into a problem with default values to template'd functions. I've boiled down my problem to this simple example code: #include<iostream> using namespace std; //...
3
by: toton | last post by:
Hi, I want to specialize template member function of a template class . It is creating some syntax problem .... Can anyone say how to do it ? The class is something like this template<typename...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.