// 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();      
        }

Recommended Answers

All 12 Replies

You 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 like this:

int main()
        {
         sample s; // an object of type sample
         sample *e; // a pointer 
         e = &s;    // set pointer to object

Well Thanks for reply. But that is not the correct solution because the deseired result is not achieved.
by your recommendations:

1) i first change the above programe as follwoing:

#include <iostream>
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;
         }

};

int main()
{
 sample s;      //an object of class type
 sample *e;     //a pointer of class type
 e=&s;          //pointer holds the address of other object
 e->setdata(5,6);
 e->print();
 return 0;
}

2) when i compiled, no error generates. but the output is wrong.
3) the program must show the sum "a+b"of private members "a" and "b" through the the member function "print()"
4) In this programe it would be "5+6=11".
5) but instead of "11" the output displays the number something like that "1496052089"
6) I do not know what is this number. perhaps address of the object. but the question is still same.
where is the problem to get the correct answer.

I think what you are referring to is polymorphism. Here's a pretty good tutorial, that should help you out.

The 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, you are passing the values x and y into your classes SetData function. At this point, the a and b members of the class are uninitialised; they haven't explicitly been given values, so they hold arbitrary/random values. Then your are assigning the passed in variables x and y whatever values are held in variables a and b (which are both uninitialised). So effectively you are just changing the passed in values and are not changing the values of a and b (which is what you want to be doing). Then when you call your print function, you are seeing the sum of the two arbitrary values.
I hope that clears things up for you!

Edit: Oop. Didn't notice Jason's post.

Thanks Jason Hippy for your helpfull reply. you explained very clearly, i easily got it and solved my problem.

but i have confusion from the Ancient Dragon reply. He told me that "set pointer to pointer makes no sense", as i did in my program earlier as:

    int main()
                {

                 sample *e;
                 e=&e;              // here compiler generates an error something like that
                                    //"can not convert sample to sample"

                 e->setdata(5,6);
                 e->print();

                 return 0;      
                }

so the question is "why we can't allow a pointer object "e" to hold the itself address like "e=&e".

You understand how a double holds a double value, and an int holds an int value, and a string holds a string value, and they're all different?

Well, a pointer-to-an-int is not the same thing as an int. A pointer-to-an-int must point at an int. It cannot point at something else. You cannot make it point to a double. You cannot make it point to a string. The compiler complains (you can do it if you try really hard, because this is C++, but it involves you telling the compiler to ignore the fact that what you're doing makes no sense).

Is an int the same thing as a pointer-to-an-int? No. What does a pointer-to-an-int point at? It points at an int. Does it point at a pointer-to-an-int? No, because a pointer-to-an-int is not the same thing as an int.

So now let's look at your code. What kind of object is e? It is a pointer-to-a-sample. So what can it point at? A sample. Can it point at a pointer-to-a-sample? No, because a pointer-to-a-sample is not the same thing as a sample. So can it point at itself? No, because itself is a pointer-to-a-sample, which is not the same thing as a sample.

I suspect the error you actually get is not "can not convert sample to sample" but something like "can not convert sample* to sample"

Well Thanks for reply. But that is not the correct solution because the deseired result is not achieved.

Humm -- I answered the question you asked. Don't blaim me if your program doesn't work right after correcting the problem you asked about.

To clarify Moschops and Ancient Dragons comments, let's take another look at your original code:

sample *e;
e = &e;

The line sample *e; declares a variable called 'e' which is a pointer to a sample object, so its type is sample*.
At this point the pointer is uninitialised and points to invalid memory (Which is kinda bad, pointers should really be initialised to null if you are not going to assign them to anything straight away!).
The variable 'e' is NOT an instance of the sample class, it is a pointer to an instance of the sample class. But again, at this point it does not point to anything valid.

Then in the next line:
e = &e; You are trying to assign the pointer to point to its own address.
The right hand side of the assignment operator (=) expects a pointer to (or the address of) a valid instance of the sample class to be passed (a sample*). But instead, it is getting the address of itself ('e').

Now remembering that the type of e is sample*, by passing &e to to the assignment operator, effectively you are passing a pointer to a pointer to a sample object (sample**).
So because what you are passing (sample**) is not what the compiler is expecting (sample*), you are getting a warning/error!

Now compare that to Ancient Dragons entirely correct code suggestion:

sample s; // an object of type sample
sample *e; // a pointer 
e = &s;    // set pointer to object 

The line: sample s; creates an instance of the sample class called 's' on the stack. So this is a valid object of type sample, in memory, somewhere on the stack.

The following line sample *e; declares a variable called 'e' which is a pointer to a sample object (sample*). Again, at this point the pointer has not been assigned to point to anything, so it is a dangerous stray pointer because it points to invalid memory!

The final line e = &s; assigns the pointer to point to the address of s. Which is correct.
The right side of the assignment operator expects a valid sample pointer (sample*). &s is the address of a valid instance of the sample class and is therefore a valid sample*, fulfilling what the assignment operator for the sample* type expects. So all is good! Your pointer has now been assigned to point to an object of the appropriate type.

I hope I have explained this clearly enough! :)

So Ancient Dragons original post correctly solves the first part of your problem, my original post corrects the second part of your problem. And hopefully my above explanation and moschops contributions will satisfy any other doubts you have. I think you can mark this as solved now! ;)

Thanks to Ancient Dragon, Moschops and JasonHippy. Yours method of explanation is very good. I got it. It is very helpful to me.
Finally, at the end, I did not blamed Ancient Dragon. this is purely for correction and for help in solving the problem!!!

what are the reasons for hiding some members in a class

what are function member

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.