Hi everyone. I'm dealing with classes. Here I'm doing a class "materialPoint" which inheritate the data-members of other two classes ("particle" and "position"). I'm having troubles calling the methods of the class "position". Thank you all in advance guys.
I'm leaving here all the codes end the errror message:
test.cxx:45:26: error: ‘double position::getX() const’ is inaccessible within this context
45 | cout << "A: " << A.getX() << "\t" << A.getY() << "\t" << A.getZ() << endl;
| ^
In file included from test.cxx:3:
position.h:17:9: note: declared here
17 | double getX () const ;
#pragma once
#include "position.h"
#include "particle.h"
#include "vectorField.h"
#include<iostream>
#include<cmath>
class materialPoint : public particle, position {
public:
materialPoint () ;
materialPoint ( double charge , double mass , const position& ) ;
materialPoint ( double charge , double mass , double x , double y , double z ) ;
// vectorField ElectricField ( const position& ) const ;
// vectorField Gravitational_Field ( const position& ) const ;
};
***materialPoint.cxx***
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "materialPoint.h"
//vacuum permittivity 8.854188E-12
//G constant 6.67408E-11
materialPoint::materialPoint () : particle () , position () { }
materialPoint::materialPoint ( double q, double m, const position& p ) : particle ( q, m ) , position ( p ) { }
materialPoint::materialPoint ( double q, double m, double x, double y, double z ) : particle ( q, m ) , position ( x , y , z ) { }
Strictly your full error (ignore the line numbers - I dumped the code into a single file to make it workable) is
74:8: error: 'double position::getX() const' is inaccessible
225:26: error: within this context
225:26: error: 'position' is not an accessible base of 'materialPoint'
Make the materialPoint class declaration class materialPoint : public particle, publicposition {
You need to deal with access to each base class separately.
Does it really make sense to use inheritance here?
Apropos of nothing your routine position::getPhi() will fail with the completely legitimate input m_x=0. Use atan2, not atan, to prevent division by 0.
Well, I already created the classes particle and position before. I could surely write the class materialPoint without inheritance, but I thaught it would be nice to use my older classes and also practice inheritance.
Anyway, thanks for the help!
I thaught it would be nice to use my older classes and also practice inheritance
There's nothing wrong with practising inheritance, but don't end up learning bad design in the process. Learning *when* to use inheritance is as important as learning *how* to do it.
Inheritance is (mostly) for modelling an "is-a" relationship, so that the derived class has the interface and properties of the base class. For example, a cat IS AN animal - it has all the externally visible behaviour of an animal, and has all the properties of an animal. Similarly, a car IS A vehicle, a square IS A shape, etc.
Is it true that a materialPoint IS A position? Or does it HAVE A position?
Is it true that a materialPoint IS A particle? Or does it HAE A particle?
You can certainly make use of your older classes - code reuse is something to be aimed for wherever helpful - but the correct relationship here might be composition, rather than inheritance.