By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,189 Members | 2,167 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,189 IT Pros & Developers. It's quick & easy.

Protected data can't be accesed

P: n/a
Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared in
that ambit( sorry for the poor translation but errors are in spanish).
Here is the code for the inherited class. (The base class works
perfectly )
The code for the constructors has been copy pasted from the base class
in a desperate try. Originally it wasnt there only the initialization
to 0 of the matrix but the problem eas the same.

#ifndef MMATRIX_CPP
#define MMATRIX_CPP

#include <iostream>
#include <cstdlib>
#include "matrix.cpp"

using namespace std;

//Definicion de la clases

template< class T >
class MMatrix : public Matrix< T //Hereda de las matrices
{
public:
MMatrix( unsigned = 3, unsigned = 3 );
MMatrix( const MMatrix< T & ); //Constructor de copia
};

#endif

/*mmatrix.hpp*/
#include "mmatrix.cpp"
#include <iostream>
#include <cstdlib>

using namespace std;

//Constructor para las nuevas matrices identico al otro
template< class T >
MMatrix< T >::MMatrix( unsigned rows, unsigned columns )
{
int i, j;

assert( rows 0 );
assert( columns 0 );

r_size = rows; //Si no se pasa el tamaño se asume 3
c_size = columns; //Si no se pasa el tamaño se asume 3

data = new T*[ rows ];
for( i = 0; i < rows; i++ )
data[ i ] = new T[ columns ];

assert( data != 0 );

for( i = 1; i <= r_size; i++ ) {
for( j = 1; j <= c_size; j++ ) This( i, j ) = 0;
}
}

template< class T >
MMatrix< T >::MMatrix( const MMatrix< T &init ) : r_size( init.r_size
), c_size( init.c_size )
{
int i, j;

data = new T*[ r_size ];
for( i = 0; i < r_size; i++ )
data[ i ] = new T[ c_size ];

assert( data != 0 );

for( i = 0; i < r_size; i++ ) {
for( j = 0; j < c_size; j++ )
data[ i ][ j ] = init.data[ i ][ j ];
}
}

I also have some problem working with several files and the
preprocessor, but thats another problem.

Thanks.

P.D. "This" is a macro "#define This( i, j ) this->operator()( (i), (j)
)" in the base class archive

Jul 25 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
at***********@gmail.com wrote:
Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared
in that ambit( sorry for the poor translation but errors are in
spanish). Here is the code for the inherited class. (The base class
works perfectly )
[..]
I think you're running into the same problem many have run into before
you:

class base {
protected:
int i;
};
class derived {
public:
void foo(base const& b) {
b.i; // prohibited -- this object doesn't have access
}
};

Yes, it's truly prohibited. The object can only access _its_own_
protected members, not other object's.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 25 '06 #2

P: n/a
I think you're running into the same problem many have run into before
you:

class base {
protected:
int i;
};
class derived {
public:
void foo(base const& b) {
b.i; // prohibited -- this object doesn't have access
}
};

Yes, it's truly prohibited. The object can only access _its_own_
protected members, not other object's.

V
Isn't the whole point of the protected keyword to provide direct access
to some member data and functions only to the base class and its derived
classes? In your example derived does not inherit from base. This code
should demonstrate that.

#include <iostream>
class foo{
public:
int GetA() { return itsA; }
void SetA(int a) { itsA = a; }
protected:
int itsA;
};

class bar : public foo{
public:
int AnotherGetA() { return itsA; }
};

int main(){
bar myBar;
myBar.SetA(10);

std::cout << myBar.GetA() << std::endl;
std::cout << myBar.AnotherGetA() << std::endl;
return 0;
}

Also, can't an object directly access the private members of ANY object
of its class, not just itself?
Jul 25 '06 #3

P: n/a
I've doen my program to see if that was the problem and it works
perfectly.

#include <iostream>

using namespace std;

class base {
protected:
base();
int i;
};
class derived:public base {
public:
void foo() { cout << i; }
};
base::base() { i=99; }

int main()
{
derived test;
test.foo();
return 0;
}

Jul 25 '06 #4

P: n/a
at***********@gmail.com wrote:
Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared in
that ambit( sorry for the poor translation but errors are in spanish).
Maybe this will help:
http://www.parashift.com/c++-faq-lit...html#faq-35.18

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 25 '06 #5

P: n/a

Marcus Kwok ha escrito:
at***********@gmail.com wrote:
Hi, im having problems compiling some code, and i dont understand why.
I've made a Matrix class for any kind of data and now im implementing
an inherited class with all the mathematical stuff (MMatrix)
(determinants and so on).
The data and the number of rows and columns are declared in the base
clase as protected. When i ask the functions of the inherited class to
use them, the compiler(g++) tells me that they haven't been declared in
that ambit( sorry for the poor translation but errors are in spanish).

Maybe this will help:
http://www.parashift.com/c++-faq-lit...html#faq-35.18
Thanks, very useful info and the work around makes the thing move. But
it looks horrible to me, or is it just getting used.

Jul 25 '06 #6

P: n/a

"Mike Stevenson" <mi***************@noaa.govwrote in message
news:ea**********@gnus01.u.washington.edu...
>I think you're running into the same problem many have run into before
you:

class base {
protected:
int i;
};
class derived {
public:
void foo(base const& b) {
b.i; // prohibited -- this object doesn't have access
}
};

Yes, it's truly prohibited. The object can only access _its_own_
protected members, not other object's.

V

Isn't the whole point of the protected keyword to provide direct access
to some member data and functions only to the base class and its derived
classes? In your example derived does not inherit from base.
I'm pretty sure that was an oversight on Victor's part. With the ": public
base" added to the derived class, it would demonstrate the problem
correctly.
>This code
should demonstrate that.

#include <iostream>
class foo{
public:
int GetA() { return itsA; }
void SetA(int a) { itsA = a; }
protected:
int itsA;
};

class bar : public foo{
public:
int AnotherGetA() { return itsA; }
};

int main(){
bar myBar;
myBar.SetA(10);

std::cout << myBar.GetA() << std::endl;
std::cout << myBar.AnotherGetA() << std::endl;
return 0;
}
That doesn't match the original post, because it never tries to access the
protected member of a different object. Both functions simply access
this->istA. The original problem was how to get access to the protected
member of an object passed as a parameter, not of the current object.
Also, can't an object directly access the private members of ANY object
of its class, not just itself?
Nope. Only through the "this" pointer. (Think of it like this: a
SportsCar, derived from a Car, should have access to its engine (which is
part of the base class Car), but it should _not_ have access to the engine
of _another_ Car!)

-Howard
Jul 25 '06 #7

P: n/a

"Howard" <al*****@hotmail.comwrote in message
news:Zl*********************@bgtnsc04-news.ops.worldnet.att.net...
>
>Also, can't an object directly access the private members of ANY object
of its class, not just itself?

Nope. Only through the "this" pointer. (Think of it like this: a
SportsCar, derived from a Car, should have access to its engine (which is
part of the base class Car), but it should _not_ have access to the engine
of _another_ Car!)
Oops, sorry! The restriction regarding access via the "this" pointer only
applies to derived classes and access to the protected members of the base,
not to private/protected members of the _same_ class. (And the anaology is
therefore pretty lame as well.)

-Howard

Jul 25 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.