Topic: ? - beginners code error
Author: padriff@fudriff.demon.co.uk ("D:WINSOCKKA9QSPOOLMAIL")
Date: Thu, 16 Mar 1995 03:45:15 +0000 Raw View
In article: <1995Mar14.230532.25486@ttinews.tti.com> tran@capri.TTI.COM (Hung Vu Tran) writes:
> > /* This is from a book lesson that I am taking:
> > I am using MS Visual basic standard (V-1.00), I am getting the error
> --> "This produces
> > ambiguous call to overloaded function"
> >
> > I can not figure out why this code is wrong can someone please explain
> what is going on. Unless
> > I am blind, it is copied
> > exactly as per the lesson. */
> >
> >
> >
> > // A customized version of stracat().
> > #include <iostream.h>
> > #include <string.h>
> >
> >
> > void mystrcat(char *s1, char *s2, int len=0);
> > void mystrcat(char *s1, char *s2);
> >
>
> the problem is right above:
> you mystrcat function was declared twice.
> renaming one of those mystrcat functions (remember do the change for
> the function call too) will fix the problem.
>
> have fun.
The problem here is not to with the fact that the name is the same really, but that there is no
way to distinguish between the two functions when they are called. This is because of the
default value you have assigned to the third parameter in :
void mystrcat(char *s1, char *s2, int len=0);
when you call
mystrcat(s1, s2);
the compiler doesn't know whether or not you wish to call
void mystrcat(char *s1, char *s2, int len=0);
or
void mystrcat(char *s1, char *s2);
as the call is valid for BOTH functions
ie if it chooses the first it simply adds the default 0 for the integer giving
mystrcat(s1, s2, 0);
or if it chooses the second it remains
mystrcat(s1, s2);
the solution is either to rename one of the functions or simply remove the default for the
parameter int len.
ie your declarations become
void mystrcat(char *s1, char *s2, int len);
void mystrcat(char *s1, char *s2);
--
Paddy McDonald
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
~ ACCEPT NOTHING | SMILE ~
~ BLAME EVERYONE | DON'T FRET ~
~ BE BITTER | DRINK COFFEE ~
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
padriff@fudriff.demon.co.uk
Author: tran@capri.TTI.COM (Hung Vu Tran)
Date: Tue, 14 Mar 1995 23:05:32 GMT Raw View
In article <bonnes.42.0013D513@ssnet.com>, bonnes@ssnet.com (Thomas J.
Bonnes) writes:
> Can somone send me email or post to me on here about what follows?
>
> bonnes@ssnet.com is my email
> Thanks Tom Bonnes
>
>
> /* This is from a book lesson that I am taking:
> I am using MS Visual basic standard (V-1.00), I am getting the error
--> "This produces
> ambiguous call to overloaded function"
>
> I can not figure out why this code is wrong can someone please explain
what is going on. Unless
> I am blind, it is copied
> exactly as per the lesson. */
>
>
>
> // A customized version of stracat().
> #include <iostream.h>
> #include <string.h>
>
>
> void mystrcat(char *s1, char *s2, int len=0);
> void mystrcat(char *s1, char *s2);
>
the problem is right above:
you mystrcat function was declared twice.
renaming one of those mystrcat functions (remember do the change for
the function call too) will fix the problem.
have fun.
> main()
> {
> char str1[80] = "this is a test";
> char str2[80] = "0123456789";
>
> mystrcat(str1, str2, 5); // concatenate 5 characters
> cout << str1 << '\n';
>
> strcpy(str1, "this is a test"); //reset str1
>
> mystrcat(str1, str2); //concatenate entire string
> cout << str1 << '\n';
>
> return 0;
> }
>
Author: bonnes@ssnet.com (Thomas J. Bonnes)
Date: Thu, 2 Mar 1995 19:49:48 Raw View
Can somone send me email or post to me on here about what follows?
bonnes@ssnet.com is my email
Thanks Tom Bonnes
/* This is from a book lesson that I am taking:
I am using MS Visual basic standard (V-1.00), I am getting the error --> "This produces
ambiguous call to overloaded function"
I can not figure out why this code is wrong can someone please explain what is going on. Unless
I am blind, it is copied
exactly as per the lesson. */
// A customized version of stracat().
#include <iostream.h>
#include <string.h>
void mystrcat(char *s1, char *s2, int len=0);
void mystrcat(char *s1, char *s2);
main()
{
char str1[80] = "this is a test";
char str2[80] = "0123456789";
mystrcat(str1, str2, 5); // concatenate 5 characters
cout << str1 << '\n';
strcpy(str1, "this is a test"); //reset str1
mystrcat(str1, str2); //concatenate entire string
cout << str1 << '\n';
return 0;
}