convert int array to string

Nov 5, 2013 at 1:47pm
I'm trying to convert int x and y arrays to string using a sample code found online.

1
2
3
4
5
6
7
8
9
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.

Appreciate your advice :)
Nov 5, 2013 at 1:54pm
Well your function declaration in the header is:

 
string int2string();


But the source for your class does not match this declaration, i.e. in your source you have an int in the argument list whereas you don't have any arguments in the function proto-type declared in the header.
Nov 5, 2013 at 2:12pm
@ajh32 thanks for the reply. I'm actually looking for an alternate way of converting int array to string. Any advice?
Nov 5, 2013 at 2:12pm
also fix the logic of your function - seems like you should place the return statement outside of the for loop - having it inside will cause you for loop to only execute once and not four times.

you may also want to separate the integers added into [returnstring] with a comma so as to still preserve the original ints added.
Nov 5, 2013 at 2:22pm
okay. i've changed declaration to :

string int2string(int[], int[]);

now hit with error:

Square.cpp: In member function ‘std::string Square::int2string(int*, int*)’:
Square.cpp:37:34: error: ‘itoa’ was not declared in this scope

I have already added #include <stdlib.h>.

@ SIK:

you may also want to separate the integers added into [returnstring] with a comma so as to still preserve the original ints added.

sorry, can i know how to do this? is this correct?

1
2
3
4
5
6
7
8
9
string Square::int2string (int x[], int y[])
{
	string returnstring = "";
	for (int i=0; i < 4; i++)
	{	
		returnstring += itoa(x[i], y[i]);
	}
	return returnstring;
}
Last edited on Nov 5, 2013 at 2:32pm
Nov 5, 2013 at 4:54pm
Woo hoo. I used stringstream instead and it works now. :)
Topic archived. No new replies allowed.