Jump to content
• Advertisement

# c++ inheritance problem

This topic is 5151 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

## 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

##### Share on other sites
Advertisement
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

##### 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

##### 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

##### 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

##### 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 onceclass 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

##### 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

##### Share on other sites
thankyou. Working now.

#### Share this post

##### Share on other sites
Quote:
 Original post by Riviera Kidi 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

##### Share on other sites

• Advertisement
• Advertisement

• ### Popular Contributors

1. 1
2. 2
3. 3
Rutin
18
4. 4
JoeJ
14
5. 5
• Advertisement

• 14
• 10
• 23
• 9
• 32
• ### Forum Statistics

• Total Topics
632631
• Total Posts
3007532
• ### Who's Online (See full list)

There are no registered users currently online

×

## Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!