"coosa" <coosa76@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:
Quote:
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.
Quote:
>
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.
Quote:
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.
Quote:
}
>
#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
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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.
Quote:
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".)
Quote:
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.