Sign in to follow this  

Function overloading for different argument types

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have a function for converting a numeric value to string:

string NumToString(long i)
{
std::stringstream out;
out << i;
return out.str();
}


I would like to overload this function to take other numeric types as well, but the function body would be identical for each. Is there a better way of doing this than simply creating multiple identical functions, differing only in the type of argument taken? The numeric type I would be dealing with would include float and double as well.

Share this post


Link to post
Share on other sites
You can use templates
template <typename T>
std::string NumToString(T i)
{
std::stringstream out;
out << i;
return out.str();
}

Share this post


Link to post
Share on other sites
Hmm. If I want to implement that template as a class member function, how would I go about it? I've tried the following with no success:

// utility.h
#ifndef UTILITY_H
#define UTILITY_H

#include <string>
#include <sstream>
#include <iomanip>
#include "utility.h"

class Utility
{
public:
void StringToUpper(std::string&);
void Pad(std::string&, int);
template <class T> string NumToString(T);
};

#endif // UTILITY_H


// utility.cpp
#include utility.h

void Utility::StringToUpper(std::string& value)
{
std::string::iterator i = value.begin();
std::string::iterator end = value.end();

while (i != end)
{
*i = std::toupper((unsigned char)*i);
++i;
}
}

void Utility::Pad(std::string& value, int size)
{
std::stringstream ss;
ss << std::setw(size) << value;
value = ss.str();
}

template <class T> string Utility::NumToString(T i)
{
std::stringstream out;
out << i;
return out.str();
}




I get the following errors when trying to build:

utility.h:15: error: expected constructor, destructor, or type conversion before ‘NumToString’
utility.cpp:22: error: expected constructor, destructor, or type conversion before ‘Utility’

Share this post


Link to post
Share on other sites
Quote:
Original post by Quasimojo
utility.h:15: error: expected constructor, destructor, or type conversion before ‘NumToString’
utility.cpp:22: error: expected constructor, destructor, or type conversion before ‘Utility’
Well first off, you need to put std:: before string.

After that, you need to move the method implementation into the header file, because (apart from a couple of the more experimental compilers), C++ doesn't support exporting templates from .cpp files.

Share this post


Link to post
Share on other sites
Quote:
Original post by swiftcoder
Quote:
Original post by Quasimojo
utility.h:15: error: expected constructor, destructor, or type conversion before ‘NumToString’
utility.cpp:22: error: expected constructor, destructor, or type conversion before ‘Utility’
Well first off, you need to put std:: before string.

After that, you need to move the method implementation into the header file, because (apart from a couple of the more experimental compilers), C++ doesn't support exporting templates from .cpp files.


Done, done and fixed. Thanks!

Share this post


Link to post
Share on other sites

This topic is 2545 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this