Sign in to follow this  
Riviera Kid

c++ inheritance problem

Recommended Posts

Hi am trying to move a program from java to c++ and i dont have alot of experience with c++. here is Rotation3D.h ************************************ #include "StdAfx.h" class Rotation3D : public Vector3D { public: int roll,pitch,yaw; Rotation3D(double tx, double ty, double tz, int troll, int tpitch, int tyaw):Vector3D(tx,ty,tz) { roll=troll; pitch=tpitch; yaw=tyaw; } ~Rotation3D() { } }; ***************************************** and here is Vector3D.h *********************************************** #include "StdAfx.h" class Vector3D { public: double x,y,z,xx,yy,zz,m,screenX,screenY; double MAT_rot[4][4]; double COS[360]; double SIN[360]; bool bVisible; Vector3D(double tx,double ty,double tz) { x=tx; y=ty; z=tz; xx=tx; yy=ty; zz=tz; } ~Vector3D() { } }; **************************************** stdafx contains vector3D.h and rotation3d.h includes. when i compile it. It says error C2504: 'Vector3D' : base class undefined i have read loads of tutorials and i cant find out why it wont work. Help please. Riviera Kid

Share this post


Link to post
Share on other sites
dont put them into stdafx header file. Include them directly for whoever needs them, this is much better since a change in one of the files doesn't mean a full recompile of everything, just what use it.

Or try putting '#pragma once' on top of the two header files (not stdafx), might do the trick here.

Share this post


Link to post
Share on other sites
Remove this #include "stdafx.h" from the beggining of your source file. stdafx.h should only be included in .cpp files (it is the main file for using precompiled headers. Also, you do not have to include your own .h files in stdafx.h, untill you have a good reason to do so.

Then, #include "vector3d.h" at the beggining of your Rotation3D.h file.

As a side note, it seems that you have your implementation in your .h files. .h should only be used for declaration (and, of course, inline function implementation). Java and C++ do not use the same programmation paradigm. In Java, the declaration and implementation code are mixed - this is not the case in C++ : declarations go in the .h file, and implementation goes in the .cpp file. Try to stick with that rule until you'll have sufficient C++ knowledge to know when to bypass this rule.

HTH,

Share this post


Link to post
Share on other sites
i had my declarations in the h files and implemented them in the cpp files as my friend told me to do but all the tutorials i found just put them all into the h files and i got totally confused. I was getting errors both ways.

is so awkward why cant there just be one way to do something.

Share this post


Link to post
Share on other sites
i have just put all my implementations into cpp files and i am still getting that same error

error C2504: 'Vector3D' : base class undefined

rotation3D.cpp
************************************************
#include "stdafx.h"

Rotation3D::Rotation3D(double tx, double ty, double tz,int troll, int tpitch, int tyaw):Vector3D::Vector3D(tx,ty,tz)
{
roll=troll;
pitch=tpitch;
yaw=tyaw;
}

Rotation3D::~Rotation3D()
{

}

******************************************************

vector3d.cpp
*****************************************************
#include "stdafx.h"

Vector3D::Vector3D(double tx,double ty, double tz)
{
x=tx;
y=ty;
z=tz;
xx=tx;
yy=ty;
zz=tz;
bVisible=true;
}

Vector3D::~Vector3D()
{

}
*******************************************************

Share this post


Link to post
Share on other sites
Well, that's a crap thing about tutorials : most of them just assume that beginners are able to understand why the code has been written this way. If so, then they are no longer beginners :)

Having your implementation in a .h file may allow you to inline functions - that is : put the code of the function where the function is called instead of having to deal with a function call, some argument management, and so on. In most cases, it ends up with faster-but-bigger code (in some cases, you'll have slower-but-bigger code, so take care :). To end up : it is good.

But (yes, there is a but) it is also evil for an unexperienced programmer, because event if it is a simple optimsation, it is still an optimisation and the "premature optimisation is the root of all evil" rule still apply here.

So do not bother with what tutors says :)

Now, your code should be (just to illustrate what I said):

vector3d.h

#pragma once

class Vector3D
{
public:
double x,y,z,xx,yy,zz,m,screenX,screenY;
double MAT_rot[4][4];
double COS[360];
double SIN[360];

bool bVisible;

Vector3D(double tx,double ty,double tz);
~Vector3D();
};




rotation3d.h

#pragma once

#include "vector3d.h"

class Rotation3D : public Vector3D
{
public:
int roll,pitch,yaw;

Rotation3D(double tx, double ty, double tz, int troll, int tpitch, int tyaw);
~Rotation3D();
};



vector3d.cpp

#include "stdafx.h"
#include "vector3d.h"

Vector3D::Vector3D(double tx,double ty,double tz)
{
x=tx;
y=ty;
z=tz;
xx=tx;
yy=ty;
zz=tz;
}

~Vector3D()
{
}



rotation3d.cpp

#include "stdafx.h"
#include "rotation3d.h"

Rotation3D::Rotation3D(double tx, double ty, double tz, int troll, int tpitch, int tyaw):Vector3D(tx,ty,tz)
{
roll=troll;
pitch=tpitch;
yaw=tyaw;
}

Rotation3D::~Rotation3D()
{

}



HTH

Share this post


Link to post
Share on other sites
dont include "vector3d.h" and "rotation3d.h" in the stdafx file, include them in "cpp" implementation files and include "vector3d.h" in the rotation3d header file.

Share this post


Link to post
Share on other sites
Quote:
Original post by Riviera Kid
i had my declarations in the h files and implemented them in the cpp files as my friend told me to do but all the tutorials i found just put them all into the h files and i got totally confused. I was getting errors both ways.

is so awkward why cant there just be one way to do something.


Sounds like your having dumb linker syndrome, you need to understand that even thou java & C++ have simillar syntax there completely different especially there schematics.

Java is predominantly reference based (it uses pointers up the wazoo but it trys to hide that fact) it's also quite a dynamic language where as C++ is predominantly value based (but you can make it completely reference based if you wish) and you have program for dynamism yourself.

Learning it properly online is just not going to happen, since you already know about programming then i would suggest you get:
The C++ programming language (Special 3rd Edition) by Bjarne Stroustrup, the creator of C++ its one of those bible type books.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this