-
April 15th, 2014, 04:39 AM
#1
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
-
April 15th, 2014, 05:29 AM
#2
Re: simple issue on function overloading in c++
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
}
Victor Nijegorodov
-
April 15th, 2014, 12:11 PM
#3
Re: simple issue on function overloading in c++
really thanks.. i forgot forward declaration of function..
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|