simple issue on function overloading in c++
I'm really confused and hence putting this very simple on function overloading. Below is the sample code
struct X
{
};
struct Y
{
}
struct Z
{
};
X x1;
Y y1;
Z z1;
set_data(X *m)
{
// need to initialize Z and Y from this funciton by getting reference to an instance
// create instance of X,Y,Z
set_data(&y1);
set_data(&z1);
}
set_data(Y *m)
{
// to initialize Y struct
}
set_data(Z *m)
{
// to initialize Z struct
}
But i do so it gives error "error C2664: 'set_data' : cannot convert parameter 1 from 'X *' to 'Y *'
Please suggest me where i'm wrong in above code
Re: simple issue on function overloading in c++
Quote:
Originally Posted by
manishak
Below is the sample code
Code:
struct X
{
};
struct Y
{
} <- semicolon is missing
struct Z
{
};
X x1;
Y y1;
Z z1;
set_data(X *m)
{
// need to initialize Z and Y from this funciton by getting reference to an instance
// create instance of X,Y,Z
set_data(&y1);
set_data(&z1);
}
set_data(Y *m)
{
// to initialize Y struct
}
set_data(Z *m)
{
// to initialize Z struct
}
But i do so it gives error "
error C2664: 'set_data' : cannot convert parameter 1 from 'X *' to 'Y *'
Please suggest me where i'm wrong in above code
First you have to use Code tags while posting code snippets. Otherwise your code is very hard to read/understand.
Second you missed the semicolon after struct Ydefinition
Third you missed to declare the prototipes of set_data functions.
Fourth you missed to define the function types.
So the corrected code should look like
Code:
struct X
{
};
struct Y
{
};
struct Z
{
};
void set_data(X *m);
void set_data(Y *m);
void set_data(Z *m);
X x1;
Y y1;
Z z1;
void set_data(X *m)
{
// need to initialize Z and Y from this funciton by getting reference to an instance
// create instance of X,Y,Z
set_data(&y1);
set_data(&z1);
}
void set_data(Y *m)
{
// to initialize Y struct
}
void set_data(Z *m)
{
// to initialize Z struct
}
Re: simple issue on function overloading in c++
really thanks.. i forgot forward declaration of function..