I'm trying to convert int x and y arrays to string using a sample code found online.

string Square::int2string (int x[])
{
    string returnstring = "";
    for (int i=0; i < 4; i++)
    {   
        returnstring += itoa(x[i]);
        return returnstring;
    }
}

but hit with the following error.

Square.cpp:30:8: error: prototype for ‘std::string Square::int2string(int*)’ does not match any in class ‘Square’
Square.h:21:10: error: candidate is: std::string Square::int2string()

I declared the following in header file.

string int2string(); 

The error is due to variable type does not match. Is there a better way to convert int array to string, please?

What I'm trying to achieve is a string printed in the following manner:

Point[0] (x1,y1)
Point[1] (x2,y2)
and so on.

I'd suggest using a std::vector instead of a raw pointer to array since the size of the array is not known within the int2string function which is a bad thing. I'd also suggest more C++ facilities instead of itoa. Also, if you are not using any parts of the class in the function it doesn't make much sense (in you example, anyway) to have the functionality there. Just make a general function. Something like:

std::string convert(std::vector< int >& xs) {
    std::stringstream ss;
    for (int i = 0; i < xs.size(); ++i) {
        ss << xs[i];
    }
    return ss.str();
}

That doesn't add a space to the resulting string, but neither does your original example.

@Ricky65: That does not address the fact that the input is an array.

Using the to_string approach would still require you to iterate over the input array. Given that, you will still want to convert to std::vector or provide a finite size to the input.

The errors say it all. The parameters of your function int2string() in your cpp file do not match with the parameters in your header file.

Just give a parameter to int2string() in your header file.

Hi all, thanks for your advice. I end up using stringstream and this is solved. :)

give the parameter to int 2string() in your header file

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.