// In this program:
when i compile this program, the complier shows error in the main section(I have mention this error
in main section) that is "can not convert sample to sample in assignment".
this error is occuring only when i try to access private members of class through pointer object.
I am unable to solve this. please can some one help to solve it.
note: solve it only by pointer variable
#include<iostream>
#include<conio.h>
using namespace std;
class sample
{
private:
int a,b;
public:
void setdata(int x, int y)
{
x=a;
y=b;
}
void print()
{
cout<<a+b;
}
};
main()
{
sample *e;
e=&e; //here compiler generates error "can't convert sample to sample in assignment"
e->setdata(5,6);
e->print();
getch();
}
irtza
0
Newbie Poster
Recommended Answers
Jump to PostYou are attempting to set a pointer to a pointer to itself. That makes no sense at all, something like saying "you are your own father".
Note: main must be declared to return an int. c++ does not allow declaring functions without a return type.
You need to do something …
Jump to PostI think what you are referring to is polymorphism. Here's a pretty good tutorial, that should help you out.
Jump to PostThe reason you are seeing strange values is because in your SetData function you are doing things backwards, you should be doing this:
a=x; b=y;
NOT this:
x=a; y=b;
To explain why you are seeing seemingly random values:
In your original code, …
All 12 Replies
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
irtza
0
Newbie Poster
tinstaafl
1,176
Posting Maven
JasonHippy
739
Practically a Master Poster
Moschops
683
Practically a Master Poster
Featured Poster
irtza
0
Newbie Poster
Moschops
683
Practically a Master Poster
Featured Poster
Ancient Dragon
5,243
Achieved Level 70
Team Colleague
Featured Poster
JasonHippy
739
Practically a Master Poster
irtza
0
Newbie Poster
samiz4j
0
Newbie Poster
samiz4j
0
Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.