PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

C++ namespace & classes problem

I have been downloading C samples of terrain heightmap generation code and now have a good batch to pick from however one of them uses classes which I am not too comfortable with :(

The code I added is below followed by the error. It seems to imply that terrain is not a member of class HillTerrain even though it's setup in the line above

Code:

#include "rfHillTerrain.h"
using namespace RobotFrog; // the class is in this namespace

HillTerrain terrain(); //
terrain.Generate();

error C2228: left of '.Generate' must have class/struct/union type

Can anyone take a guess at what's wrong here ( assuming I have given enough information )
simonjacobs · 15 Mar 2002, 11:28 UTC
It means the HillTerrain class is not defined. Were there any other error messages or just one?

What compiler are you using?

Can you show us the definition of HillTerrain?
simonjacobs · 15 Mar 2002, 11:29 UTC
Does evc support 'namespace'? When was that added to c++ anyhow, I've never used them (except when messing around with c#).
Conan · 15 Mar 2002, 13:42 UTC — Had another look at the 'class'
It was just the one error & I'm using Embedded VC++ 3.
Although the author calls the terrain routine a class it is not actually laid out like any of the classes I have just looked at. It has the appearance of a function in the main but some parts which look like constructor / destructor bits ( or whatever you call them ).

I will see if I can push the code around so it looks like a normal class by comparing to others. I will get rid of the namespace stuff as well.

The tuturial where the source came from is:-
http://www.robot-frog.com/3d/hills/index.html

Funny how some Windows code just compiles & other stuff throws up little wierdos :roll:

(This all started when I realised that using a bitmap heightmap for terrain would use a huge amount of ram so I was proud of my little terrain for about only 5 minutes)
Take out the parentheses when declaring the terrain variable, or better yet, use the new keyword.

Code:

#include "rfHillTerrain.h"

using namespace RobotFrog; // the class is in this namespace

int main( void )
{
HillTerrain terrain; // you can also specify terrain params here
terrain.Generate();
}



OR

Code:
#include "rfHillTerrain.h"

using namespace RobotFrog; // the class is in this namespace

int main( void )
{
HillTerrain terrain = new HillTerrain(); // you can also specify terrain params here
terrainGenerate();
}


Pam
I think there are two errors in the code below.
Comments are in the code.
Code:

#include "rfHillTerrain.h"
using namespace RobotFrog; // the class is in this namespace
// when invoking the default constructor (as below)
// you have to leave the parenthesis out
// The compiler will otherwise interpret the
// statement as the declaration of a method
// terrain, taking no arguments and returning a
// HillTerrain object.
//
// HillTerrain terrain();
HillTerrain terrain;

//
// It's also not possible to invoke methods in the
// global skope like this... but this line was
// perhaps just copied from inside some method?
terrain.Generate();


/Orpe
simonjacobs · 15 Mar 2002, 14:15 UTC
Looks exactly like a class to me!

I would just remove the namespace (just delete those lines) and that will probably fix it.
simonjacobs · 15 Mar 2002, 14:18 UTC
Are you allowed to declare a function inside a function? A bit weird if you ask me :)
Pam, here are a couple minor corrections to your dynamic allocation sample.

Code:

#include "rfHillTerrain.h"

using namespace RobotFrog; // the class is in this namespace

int main( void )
{
HillTerrain *terrain = new HillTerrain; // you can also specify terrain params here
terrain->Generate();
delete terrain;
}


Dan East
Ack! Duh, Thanks Dan.

Pam
simonjacobs wrote:
Are you allowed to declare a function inside a function? A bit weird if you ask me :)


It's allowed. You can declare a function inside another function but not define a function inside
another function.

The only common (but still quite uncommon) use of this is to make sure that only a particular function, funcA, calls another function, funcB, by declaring funcB inside funcA.

For this to make any difference though, the other functions must not see the definition of funcB (nor another declaration of funcB).

/Orpe
Conan · 15 Mar 2002, 16:05 UTC — thanks Pam, Dan, Simon & Orpe
Many thanks for the tips. I have pushed the code around & it still does not work though I moved the errors from 1 to 38 & back so I will dig out my big C++ book & look at classes tonight. I assumed the sample code posted was working & because it did some smart things with terrain I though I would try & get it working. I can just give up & cut out the functional bit & turn it into regular C if necessary.

One thing I don't understand is why there is no 'class HillTerrain' at the start of the class definition but I'm not trying to understand classes anyway :)

Once the terrain generator works I get down to the fun part of modifying the terrain via user input.

ps: It takes me more than 9 months to write a game as I do it on the train to & from work but I always get them finished.
I've downloaded Bob Nystrom's code and I don't see why you're still having a problem (after using the suggestions above). There is certainly a proper class declaration for HillTerrain in rfHillTerrain.h.

Perhaps it's something you've modified for your particular use. You might try backing out your changes, then add them one-by-one making sure it compiles successfully at each step.
Thanks Digby

The only things I changed deliberately were the references to assert.h and assert within the code as I don't have this header file & everyone I look at on the web is different.
(.... 30 minutes later :idea: )

OK. Here's what I was doing wrong: I did not realise that the definition of the class was in the header file. Not knowing what I was looking at I thought that the line HillTerrain::HillTerrain( etc etc was some kind of class definition cause I don't know much about classes etc.

So now having removed the namespace stuff the code compiles past the error & all I have are a couple of external references not found to track down.

Is C++ overcomplicated or should I send off to amazon.com for more motor neurons ( the bulk pack )?

Thanks again, have a fun weekend

Saturday update: Ripped out the C++ Class stuff & turned the code into regular functions which now compile fine with no errors :D
Paul,
name spaces are part of the ansi standard for C++ and goes back to at least '96 +/- a year when it was accepted as a standard I think. It really works just like .Net namespaces at least in theory. VC 6 has support for namespaces.
C++ is very complicated but worth it once you get the OOP basics. Name spaces also very cool, they makes grouping classes very easy.