I need to overload a function and I'm not sure how to do it. I started it and I'm not sure where to go from there. Can someone explain to me, in basic, dumbed-down terms what would work best for me to do?
Well, all overloading is is when you have two functions with the same name but given different parameters
looks like you almost had it, you need to add a comma though between each parameter as demonstrated below
1 2 3 4 5 6 7 8 9 10 11 12 13
void insert(char *p, int pos) { //added a comma on this line between the parameters
if [pos >= | pos <=length +1);
}
void insert(char *p) {
box *pb = new box;
strcpy( (*pb).name, p);
(*pb).link = head;
head=pb;
length++;
}
Different parameter how? Like giving it two different things to do? And I know I start on the one function but it's not finished, I need more to it, I just don't know what else to do with it. Early C++ was easier...this stuff I've never done with this * and all.
Well, overloading a function does not necessarily mean giving it two different things to do, but typically each function will do something different since there are more parameters. Literally, overloading is something that most OOP languages allow you to do. It is so you can have two different functions with the same name but yet perform different tasks depending on which one is called. If you call insert(&char); then this means you will call the first "version" of insert and it work perform the tasks associated with such function. But if you want to call the other insert function you will need to provide two arguments since this is the requirement for this function to be called likes so insert(&charOne, &firstPos).
When you have something like int *something it means you are declaring a pointer. You can look in the documentation section of this site and get the nit and gritty on pointers, but basically they are like variables except they hold the address of the variable.
You may have noticed above when doing examples of those functions calls I used "&" in front of the what are assumed to be variables. That is because you want to pass the address to the pointers since the parameters for the function are declared as pointers. The "&" allows to reveal the address of any variable.
Hope this helped. It is hard to explain when one is not familiar with your current experience.
void insert(char *p);
void insert(char *p, int pos);
and the code you have for the single argument version, I'd guess that you're supposed to use the second argument to determine where to insert a new name in the list.
To do that, you're going to want to walk the list until you've passed <pos> entries, then insert the new entry at that point. You will need to handle a few special cases:
1) What if the list is empty?
2) What if <pos> is negative?
2) What if <pos> exceeds the number of entries in the list?