Issue with setting a protected member

A

Avinash

I have a class defined as follows:

class main {
........
protected:
another_class *obj;

public:
void set_another_class(another_class *ptr);

}

main::set_another_class(another_class *ptr)
{
obj = ptr;
}

When I try to compile this, it gives me the following error:
"'obj' : undeclared identifier
'=' : cannot convert from 'class another_class *' to 'int'

Could somebody let me know why I cannot set a protected member from a
public method?
And also suggestion for setting the "another_class" pointer on "main".
 
I

Ivan Vecerina

:
: I have a class defined as follows:
:
: class main {
: .......
: protected:
: another_class *obj;
//NB: should be declared somewhere...
:
: public:
: void set_another_class(another_class *ptr);
:
: }
; //<-- missing here
:
void // missing return value
: main::set_another_class(another_class *ptr)
: {
: obj = ptr;
: }
:
: When I try to compile this, it gives me the following error:
: "'obj' : undeclared identifier
: '=' : cannot convert from 'class another_class *' to 'int'
:
: Could somebody let me know why I cannot set a protected member
: from a public method?
This is not the (root) problem, and since you probably edited the
code posted here, we cannot tell what error was in your original code.

But the following will compile without problem:

class another_class;

class main { //NB: I would reserve 'main' to int main(), really ...
protected:
another_class *obj;
public:
void set_another_class(another_class *ptr);
};

void main::set_another_class(another_class *ptr)
{
obj = ptr;
}



hth -Ivan
 
S

Salt_Peter

Avinash said:
I have a class defined as follows:

class main {
.......
protected:
another_class *obj;

public:
void set_another_class(another_class *ptr);

}

main::set_another_class(another_class *ptr)

void main::set_another_class(another_class *ptr);
{
obj = ptr;
}

When I try to compile this, it gives me the following error:
"'obj' : undeclared identifier
'=' : cannot convert from 'class another_class *' to 'int'

Could somebody let me know why I cannot set a protected member from a
public method?

Thats not your problem. There is an issue with another-class or with an
overload of its op=.
Did you forget to #include it?
And also suggestion for setting the "another_class" pointer on "main".

Why aren't you using a const reference instead and setting that in
ctor?
And why deal with the name clash between int main() and your class
name? Isn't life complicated enough?
Always initialize your members. Also, if you are going to use dumb
pointers, you should at least make the minimum efffort to protect that
critical pointer or you'll find yourself in hot water real fast since
anyone could modify that pointer by mistake.

This works:

#include <iostream>
#include <ostream>

class another_class { };

template< typename A >
class Main
{
A* p_another;
public:
Main() : p_another(0) { } // def ctor with init list
void set_another_class(A* const ptr);
A* const getA() const { return p_another; }
};

template< typename A >
void Main< A >::set_another_class(A* const ptr)
{
p_another = ptr;
}

int main() {
Main< another_class > m;
another_class ac;
m.set_another_class( &ac );
std::cout << "p_another = " << m.getA();
std::cout << std::endl;

// another_class* p_A = m.getA(); // error
another_class* const p_A = m.getA(); // ok, const ptr
// p_A = 0; // error, read-only
// m.getA() = 0; // error, invalid l-value
std::cout << "p_A = " << p_A;
std::cout << std::endl;
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top