When I try to compile this code

using namespace std;

#include <iostream>

class COne{
	protected:
		int a;
	public:
		bool is_equil( COne & other){
			if ( a == other->a)
				return 1;
			else
			return 0;
		};
		int seta(int val){
			a = val;
			return 0;
		};
};

int main(){
	COne a, b;
	
	a.seta(5);
	b.seta(3);
	
	cout << "result is: " << a.is_equil(b) << endl;
	b.seta(5);
	cout << "result is: " << a.is_equil(b) << endl;
	return 0;
}

I get the following error
"Base operand of '->' has non-pointer type COne"
Why is it? I am probably missing something very simple here.

Like u8sand said...use "."

using namespace std;

#include <iostream>

class COne{
	protected:
		int a;
	public:
		bool is_equil( COne & other){
			if ( a == other.a)
				return 1;
			else
			return 0;
		};
		int seta(int val){
			a = val;
			return 0;
		};
};

int main(){
	COne a, b;

	a.seta(5);
	b.seta(3);

	cout << "result is: " << a.is_equil(b) << endl;
	b.seta(5);
	cout << "result is: " << a.is_equil(b) << endl;
	return 0;
}

"->" is used to access data members on the heap :) hope i helped :)

Why is it? I am probably missing something very simple here.

Something subtle, yes. References are not pointers, even though internally they might be implemented using pointers. When you have a reference to an objects, members are accessed the same way as with regular objects.

Try using . instead of ->

Yes, the "." works, but why? I can't figure out why and it is driving me crazy. Is it because I am pointing to the class with its member function?

>"->" is used to access data members on the heap

The arrow operator -> is used to access data members (of a class) via a pointer, these data members do not necessarily have to be on the heap, consider the following example:

// declaration of the 'counter' structure:
struct counter { int count; };

// declare pointer:
counter *p;

// declare structure variable:
counter c;

// put some value in it:
c.count = 5;

// Let the pointer point to an instance of 'counter':
p = &c;

.
.

Well, is c on the heap here?
I think it's in the automatic memory.
(sorry for the boring example, but I really had no inspiration :P)

The arrow operator ( -> ) is often used to replace things like this in your code: (*[I]pointer_to_structure[/I]).[I]data_member[/I] .
There have to be parentheses around pointer_to_structure because of the rules of precedence, if you don't place them, then you are referring to a data member of a structure (and in this case: a pointer which is a data member of a structure).

Yes, the "." works, but why?

Because the arrow operator ( -> ) is used in combination with pointers.
(Take a look at Tom Gunn's post, he has already explained it to you)

>"->" is used to access data members on the heap

The arrow operator -> is used to access data members (of a class) via a pointer, these data members do not necessarily have to be on the heap

I see, thanks for clearing that confusion up for me...I learn something new here everyday :)

This article has been dead for over six months. Start a new discussion instead.