Jump to content
  • Advertisement
Sign in to follow this  
Quasimojo

Function overloading for different argument types

This topic is 2759 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
Advertisement
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
This is a good thing to know how to do 'from scratch', but note that boost::lexical_cast<>() does essentially the same thing.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!