Hi, I am getting an error message when using base classes. The error message is

x: undeclared identifier
y: undeclared identifier
z: undeclared identifier

The particle class derives from the fourvector class which derives from the threevector class, as follows.

#ifndef THREEVECTOR_H  // Prevents the class being re-defined
#define THREEVECTOR_H 

#include <iostream> // Include input/output stream
//#include <fstream>
#include <cmath>

class threevector
{

private:
	double xcoord, ycoord, zcoord; // Private data members
	double scalar;

public:

	// Default constructor
	threevector() 
	{
		xcoord = 0.0;
		ycoord = 0.0;
		zcoord = 0.0;
 	}
	// Cartesian constructor
	threevector(double x, double y, double z)
	{
		xcoord = x;
		ycoord = y;
		zcoord = z;
	}
	// Polar Constructor
	threevector(double r, double theta, double phi, char angletype)
	{
		if (angletype == 'd')
		{
			double pi = acos (-1.0);
			phi  = (phi * pi) / 180; 
			theta = (theta * pi) / 180;
		}
		else 
		{}
		xcoord = r * sin(theta) * cos(phi) ;
		ycoord = r * sin(theta) * sin(phi) ;
		zcoord = r * cos(theta);
	}
	// Spherical polar constructor
	/*threevector(double r, double theta, double phi, char angle_type)
	{
		if ((angle_type == 'd') || (angle_type == 'r'))
		{
			if (angle_type == 'd')
			{
				double pi = acos(-1.0);
				phi *= (pi/180);  
				theta *= (pi/180);
			}
			else
			{}
			
			xcoord = r*sin(theta)*cos(phi);
			ycoord = r*sin(theta)*sin(phi);
			zcoord = r*cos(theta);
		}
		else
		{}
	}*/
	
	// Access method for coordinates
	double getx()
	{
		return xcoord;
	}
	double gety()
	{
		return ycoord;
	}
	double getz()
	{
		return zcoord;
	}
	
	// Access method to find the square of a threevector
	double square()
	{
		return (xcoord*xcoord + ycoord*ycoord + zcoord*zcoord);
	}
	
	// Access method to find the magnitude of a threevector
	double mag3()
	{
		return sqrt(square());
	}
	
	// Access method to find the spherical polar angles of a threevector
	double gettheta()
	{
		return acos (zcoord / mag3());
	}
	double getphi()
	{
		return atan(ycoord / xcoord);
	}

	// Modifier method to modify existing coordinates
	void setx(double xnew)
	{
		xcoord = xnew;
	}
	void sety(double ynew)
	{
		ycoord = ynew;
	}
	void setz(double znew)
	{
		zcoord = znew;
	}
	
	// Overload the + operator
	threevector operator+(threevector v_other)
	{
		threevector v_sum(xcoord + v_other.getx(),
		                  ycoord + v_other.gety(),
			    		  zcoord + v_other.getz());
		return v_sum;
	}
	
	// Overload the += operator
	threevector operator+=(threevector v_other)
	{
		threevector v_sum(xcoord += v_other.getx(),
		                  ycoord += v_other.gety(),
			    		  zcoord += v_other.getz());
		return v_sum;
	}
	
	// Overload the - operator
	threevector operator-(threevector v_other)
	{
		threevector v_sum(xcoord - v_other.getx(),
		                  ycoord - v_other.gety(),
			    		  zcoord - v_other.getz());
		return v_sum;
	}
	
	// Overload the * operator
	threevector operator*(double number)
	{
		threevector v_prod( xcoord * number,
		                    ycoord * number,
			    		    zcoord * number);
		return v_prod;
	}
	
	// Overload the / operator
	threevector operator/(double number)
	{
		threevector v_div( xcoord / number,
		                   ycoord / number,
			    		   zcoord / number);
		return v_div;
	}
	
	// Overload the * operator
	threevector operator*(threevector v_other)
	{
		threevector v_dot( xcoord * v_other.getx(),
		                   ycoord * v_other.gety(),
			    		   zcoord * v_other.getz());
		return v_dot;
	}
	
	// Overload the ^ operator
	threevector operator^(threevector v_other)
	{
		threevector v_cross( (ycoord*v_other.getz()) - (zcoord*v_other.gety()),
		                     (zcoord*v_other.getx()) - (xcoord*v_other.getz()),
			    			 (xcoord*v_other.gety()) - (ycoord*v_other.getx()) );
		return v_cross;
	}
	
	// Method to print out contents to screen and to file
	void print() 
	{
		std::cout << xcoord << '\t'
 				  << ycoord << '\t'
				  << zcoord << std::endl;
	}
	void print(std::ofstream& fout)
	{
		fout << xcoord << '\t'
 		     << ycoord << '\t'
		     << zcoord << std::endl;
	}

}; 
#endif // THREEVECTOR_H




#ifndef FOURVECTOR_H
#define FOURVECTOR_H 

#include "H:\Visual Studio 2008\Projects\section2\threevector.h"

class fourvector : public threevector  
{
private:
	double tcoord;

public:

	// Constructors
	fourvector() : threevector()
	{
		tcoord = 0.0;
	}
	fourvector(double t, double x, double y, double z) : threevector(x, y, z)
	{
		tcoord = t;
	}
	fourvector(double t, threevector v) : threevector(v)
	{
		tcoord = t;
	}

    // Methods to print out contents to screenn and to file
	void print()
	{
		std::cout << tcoord << '\t';
		threevector::print();
	}
	void print(std::ofstream& fout)
	{
		fout << tcoord << '\t'; 
		threevector::print(fout);
	}

	// Access method to find the square of a fourvector
	double square()
	{
		return (tcoord*tcoord - threevector::square());
		
	}

	// Access method for coordinates
	double gett()
	{
		return tcoord;
	}

	// Modifier method to modify existing coordinates
	void sett(double tnew)
	{
		tcoord = tnew;
	}

	//Method that Lorentz tranforms an object 
	fourvector setframe (threevector newframe)
	{
		double beta  = newframe.getx();
		double gamma = 1.0 / sqrt(1.0 - (beta*beta));
		fourvector LT (  gamma * (gett() - (beta*getx())),
			             gamma * (getx() - (beta*gett())),
						 gety(), 
				         getz()  );
		return LT;
	}

};

#endif // FOURVECTOR_H 





#ifndef PARTICLE_H
#define PARTICLE_H

#include "fourvector.h"

class particle : public fourvector
{

public:

	//Constructor
	particle(double mass, double px, double py, double pz) : threevector(x, y, z)
	{
		tcoord = sqrt( (px*px) + (py*py) + (pz*pz) + (mass*mass) );
	}

	// Method to find the mass of a particle
	double get_mass()
	{
		return mag3();
	}

	// Method to find the energy of a particle
	double get_energy()
	{
		return tcoord;
	}

	// Method to find the kinetic energy of a particle
	double get_kinetic_energy()
	{
		return ( tcoord - get_mass() );
	}

	// Method to find the momentum of a particle
	threevector get_momentum()
	{
		threevector momentum( xcoord, ycoord, zcoord );
		return momentum;
	}

	// Method to find the velocity of a particle
	threevector get_velocity()
	{
		threevector velocity( xcoord/tcoord, ycoord/tcoord, zcoord/tcoord );
		return velocity;
	}

};

#endif // PARTICLE_H

Recommended Answers

All 6 Replies

The error message is for the constructor of the particle class. I don't know what's causing the mistake. Could someone please help?

Scoping rules a biting you again. The variables x, y, and z do not exist within the scope of the particle constructor. Can you think of other similarly-named variables that are in-scope?

px, py and pz?

If I implement the above and other changes, then I have the following constructor:

//Constructor
	particle(double mass, double px, double py, double pz) : threevector(px, py, pz)
	{
		sett(sqrt( (px*px) + (py*py) + (pz*pz) + (mass*mass) )) ;
	}

But still I get an error message:
'particle' : illegal member initialization: 'threevector' is not a base or member

What could be the problem?

Because particle inherits from fourvector you have to initialize fourvector from particle. In similar fashion, since fourvector inherits from threevector, you have to initialize threevector from fourvector. You can not legally jump directly from particle to threevector.

Just a comment. I find your hierarchy unneeded and wrong. You can easily make a template vector class for the general case, and then typedef certain instantiation of the template vector for easier use.
And have regular function work with the generic template vectors. As a general rule, choose composition over inheritance. So for example, in your particle class, use composition. That way your not limited to one instance of fourvector, and thus you can have a forcevector, position vector and so on.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.