Doubt about function overloading (C++)

I have a problem while creating a "string" module (I'm creating some personal string functions).

In this moment I try to create an overloaded function called strremove.

1
2
3
char * strremove(char * source, int start_index, int end_index);
char * strremove(char * source, int start_index);
char * strremove(char * sorce, const char * key, char & save);


Before the creation of the 3rd alias of "strremove" no problems (the 2 functions work correctly).

But when I try to use the 3rd function...

1
2
char * save = new char[10];
string = strremove(string,"help",save);


the compiler (Gcc 3.4.5 on MingW 5.1.4 Under XP) returs this error


error: invalid conversion from 'const char *' to 'int'
error: inizializing argument 2 of char * strremove(char*, int, int)
...


so it seems he tries to execute the 1st alias instead of 3rd one.
Why?

Note:

overload strremove;

returns error


'overload' does not name a type
Last edited on
save is of type char*, not char&.

Char & save Is the address of 1 char.

Have you tried using the C++ string library before you write your own? Character arrays are very much a C convention.
Last edited on
Ehm...

I'm sure you know it, but, to be sure...

When I use a declaration like

 
void sum (int & a) { a=a+1; ... }


'int &a' is a "Reference" (and not an address like you said... only available on C++ and not in C), more or less an alias of the variable passed, as I understood.

1
2
3
char x[50];
char * xptr;
xptr = &x; //THIS (&x) is an address 


So, returning to usage of "Reference", if I do

1
2
 
int b = 5; sum(b); //sum() is declared as -> void sum (int & a) { a=a+1; ... } 


the result is that 'b' increase by 1


Have you tried using the C++ string library before you write your own? Character arrays are very much a C convention.

I don't know all C string function (I can use well only strcpy, strlen, strstr. I never understand strtok() ).
But I introduce strremove to add a function that works like string.Remove(int start_index, int end_index) on C#.
In this moment I try to add also other functions like the lastone I show to you (returs a truncated string and save the erased part by truncation into "save")

I also tryed to change Reference to pointer

1
2
3
4
5
6
7
8
char * strremove(char * source, int start_index, int end_index);
char * strremove(char * source, int start_index);
char * strremove(char * sorce, const char * key, char * save);

//In module mani.cpp:

char * save = new char[10];
string = strremove(string,"help",&save)


but problem persists
(the same error message).

Any other suggestions?
Last edited on
I know you have declared your function prototypes as you have shown us, but when you wrote out the functions themselves do the function parameters types actually match the prototypes?
Yes the problem is that one you said, as I can see. the problem is related to the last param.

If I define the last param in funct as a reference (char & save)
the compiler does not recognize if I pass a char *

If I define the last param in funct as a pointer (char * save)
the compiler does not recognize if I pass an address to char *
the compiler recognize if I pass a false-casting, but occurs a runtime error

Perhaps the solution is where you said (and I have to controll better my own code).

----------------------

SOLVED!

if I declare
strremove(char*, const char*, char *)

and I use simply

1
2
3
4
char * source;
//....
char * save = new char [100];
source = strremove(source,"string",save);

the code works correctly (and "save" saves what I want).

Thank a lot to all people who replied :)
Last edited on
I don't know all C string function (I can use well only strcpy, strlen, strstr. I never understand strtok() ).


Hopefully you understand why functions like strcpy are bad? And the correct alternatives that should be used for memory-safety :)

But cool getting the problem fixed.

1
2
3
char x[50];
char * xptr;
xptr = &x; //THIS (&x) is an address 


That is the address of the first element x[0]; Not the address of the whole array as it were. Your char array would typically be null-terminated as a way to determine it's length. x[0] is also treated as an int, which is why your original function declaration didn't work and the last param had to become char*.


I mentioned the C++ string object, similar to the C# one.
1
2
3
4
5
6
7
#include <string>

using namespace std;

int main() {
 string blah = "hello World";
}


Using char arrays is going to cause you a fair bit of hassle with memory allocation and de-allocation.

e.g You declare save as new char [100]. Does strremove modify and re-allocate? or simply move characters along and null terminate?

Last edited on
Hopefully you understand why functions like strcpy are bad? And the correct alternatives that should be used for memory-safety :)

Unluncly you are overstimating me...
I simply need more specific functions, so this is the reason of my own functions. Some of the alias of "strremove" calls strcpy to make a copy of "source" to a "temp".
I didn't know that sctrcpy can be expansive for memory... seeing the fact my program requires accuracy of usage of memory (I need to allocate the memory to a great quantoty of datas) I will consider to write my own strcpy function

1
2
3
char x[50];
char * xptr;
xptr = &x; //THIS (&x) is an address  

That is the address of the first element x[0]; Not the address of the whole array as it were.

Thank you very much for info :)
I knew that &x means the address of 1st element of array, but I dind't consider this fact you marked.


Using char arrays is going to cause you a fair bit of hassle with memory allocation and de-allocation.

Yes I know it, but, seeing the fact that C++ does not have a "string" type, I prefer to use directly arrays.


e.g You declare save as new char [100]. Does strremove modify and re-allocate? or simply move characters along and null terminate?

obliouvsly the 2nd. I know that you will vaste some memory, but it doesn't matter, becouse I require to use 1-2 strings... not much more...
I can probably reallocate memory, but I think it can be dangerous.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
char * string = new char [100];
...
//string = "hello world"
strremove(string,"ld"); //an other alias of strremove

//------------------

// if I need resizing string I can do a think like

strremove(char * source, const char * key) {
  char * temp = new char[strlen(source)+1];
  strcpy(temp, source)
  //here routine for truncation in temp (I truncate temp in this example)
  delete [] source; //it can be dangerous
  source = new char[strlen(temp)+1]; //it can be dangerous and unreadable 
  //by the function who calls strremove... (or I'm wrong?)
  strcpy(source, temp);
  delete [] temp;
}


However, for my scope, I NEED to maintain the array dimension of string (it is long to explain why). So I will not consider to resize array dimension (Seeing also the fact that I no obtain so many mamory... I will use not much strings)
Last edited on
Yes I know it, but, seeing the fact that C++ does not have a "string" type, I prefer to use directly arrays.


but C++ DOES have a string type.
Really? And what is the keyword? (like char, int, etc... and for strings?)

I leared basics of C++ more or less 10 years ago, when some standard ANSI were different from now (for example the introduction of namespace usage)

BEFORE (when I learned C++)

1
2
3
4
5
6
7
8
#include <iostream.h>
#include <fstream.h>

int main () {
 ifstream in("file.txt");
 //...
 return 0; 
}


NOW

1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>

using namespace std;

int main () {
 ifstream in("file.txt");
 //...
 return 0;
}


So I don't know many of actual standars. However, for what I see, it seems to me that exist a sort of class (or a struct) for String, but I don't know anything about it.
For what I see since now it seems that "strings" are not a real "type" but simply are manipulated like a type by class functions (but I figure that internal buffer of data is always an array of chars. As I know "strings" is not a native type in C nor C++, not in past, not now).
1
2
3
4
#include <string>

std::string aString = "Hello World";
//etc... 


Yes, string is not a compiler intrinsic type; it is a "user-defined" type implemented as a class.

Sorry for the stupid question, but what are the new improvements, compared from the OLD <string.h> (without namespaces)?

It is possible to find, for example, a list of new functions, etc?
I don't know exactly the differences with the old <string.h> but you can find about the functions here: http://www.cplusplus.com/reference/string/string/

string.h is a C header file that defines C's string manipulation functions such as
strcpy(), strcat(), strlen(), etc, etc, all of which operate on char*.

string is a C++ header file that defines the C++ string class (see link posted by
Mitsakos).

If you're going to be writing C++ code, you should use the string class, if for
no reason other than that the string class provides all of the functionality of
C's string manipulation functions plus more, and in a much easier-to-use way

Eg,

1
2
3
string s1 = "Hello";
string s2 = "World";
string s3 = s1 + " " + s2;


is a lot easier and more intuitive than

1
2
3
4
5
6
7
char* s1 = "Hello";
char* s2 = "World";
char* s3 = strdup( s1 );
strcat( s3, " " );
strcat( s3, s2 );
//...
free( s3 );
Topic archived. No new replies allowed.