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.cp p"
#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 7 1667 at***********@g mail.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
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.AnotherGe tA() << std::endl;
return 0;
}
Also, can't an object directly access the private members of ANY object
of its class, not just itself?
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;
} at***********@g mail.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
Marcus Kwok ha escrito: at***********@g mail.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.
"Mike Stevenson" <mi************ ***@noaa.govwro te in message
news:ea******** **@gnus01.u.was hington.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.AnotherGe tA() << 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
"Howard" <al*****@hotmai l.comwrote in message
news:Zl******** *************@b gtnsc04-news.ops.worldn et.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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: away |
last post by:
Why some classes are seen to has their constructors declared as "protected"?
Thanks!
|
by: Act |
last post by:
Why is it suggested to not define data members as "protected"?
Thanks for help!
|
by: Nick Keighley |
last post by:
Hi,
I'm curious about best practice for protected members.
Consider this code fragment:-
class Patch
{
public:
Patch ();
virtual void draw (Page, int x, int y) = 0;
|
by: jopperdepopper |
last post by:
Hi,
finally giving php 5 a go, and going over the new approach to classes.
Can someone clarify the public, private and protected to me?
I quote the php manual: "The visibility of a property or method can be
defined by prefixing the declaration with the keywords: public,
protected or private. Public declared items can be accessed
everywhere."
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
| |
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |