C++ Classes Help (Long Code Quote)

Started by
17 comments, last by xsirxx 18 years ago
I thought the other posters were pretty clear on this point, but then again I'm not a newbie, so I forgive you :)

DO NOT use a line like #include <something.cpp>

You should read the link: http://www.gamedev.net/reference/programming/features/orgfiles/

You only include header files in cpp files and header files. You never include cpp files.

Basically, you put class/struct definitions, function prototypes, global variables(as extern), typedefs, constants, and #defines

in order, this is the stuff above.
header.h
#ifndef HEADER_H#define HEADER_H#include <string> /* we use string, so include it *//* class definition */class CClass { /* or struct in place of class, they're identical in C++ */ private:  int private_var;public:  CClass();  ~CClass();  int getPrivate();};/* function prototype */void printSomething(const std::string &something);/* global variable */extern int I_AM_EVERY_WHERE;/* typedefinition */typedef CClass aClass;/* constant */const int bad_boy = 10;/* define */#define square(x) x*x#endif


and the associated cpp file header.cpp
#include <iostream> #include <string> /* we use iostream, string and header.h, so include them */#include "header.h"CClass::CClass { private_var = 0; }CClass::~CClass { /* unitialize stuff */ }int CClass::getPrivate() { return private_var; }int I_AM_EVERY_WHERE;/* note how I'm prefixing string, cout, and endl with std::, because * you should avoid using using namespace std; */void printSomething(const std::string &something) {  std::cout << something << std::endl;}

And finally, in your main.cpp file you'd do something like this:
#include <iostream>#include <header.h>int main(){  aClass temp;  std::cout << "private: " << temp.getPrivate(); << std::endl;  return 0;}


There are times when you are including a cpp file in your header file. I'm sure this is causing several problems as the very first error looks like it's coming from tile.h in line 12. there's nothing wrong with this line which means there error comes from earlier. so I looked in last header file you added and looked in location.h

At the bottom of location.h you include location.cpp. So logically I'm going to assume the error is there, but because you shouldn't be including cpp files in header files(or anywhere for that matter) I'm going to tell you to read that link and change your code until you don't include cpp files and then if you're still having problems I'll help.

C++: A Dialog | C++0x Features: Part1 (lambdas, auto, static_assert) , Part 2 (rvalue references) , Part 3 (decltype) | Write Games | Fix Your Timestep!

Advertisement
You mispelled SetX in your location class. You have SetX() and SeyX().

Edit: You also have semi-colons after some of your include directives (#include).
Quote:You mispelled SetX in your location class. You have SetX() and SeyX().

Thanks but I'm still getting a zillion errors.
Problem one,
------------------------------------------------------------------------------
Under...

Class Tile
{
...
}

yer,
Location TheTileLocation;
needs to be changed to,
Location* TheTileLocation;

Then in make yer,

Tile::~Tile( void )
{
if(TheTileLocation != NULL) delete TheTileLocation;
}


Next problem:
------------------------------------------------------------------------------
What the last poster said, goto the top of WorldMap.h and replace:

#include "Location.cpp"
#include "Tile.cpp"

with,

#include "Location.h"
#include "Tile.h"


Also, like the last poster said, YOU SHOULD NEVER INCLUDE .CPP.
that being said, at the end of Tile.h take out the include .cpps...they are not needed.


Next problem:
--------------------------------------------------------------------------------
you dont need to if define any CPP files, just the at the top of yer .h files. So you can safely remove those

Next problem:
-----------------------------------------------------------------------------

Under yer location class, you cant have two functions of the same name return 2 different variables...

So, all the GetX() and GetY() functions need to have different names, like so:

Location::GetXAsFloat(), GetYAsInt()...
The Set..() functions are fine because the input is another type... its just the output needs different.
--X
Alright, after much cleaning-up and revising the code as many times as needed to conform to the standards of the posters, I'm still getting some of the errors I've been getting this entire time. Most notably:
Quote:
h:\NSRM\Location.cpp(6): error C2065: 'SetX' : undeclared identifier
h:\NSRM\Location.cpp(7): error C2065: 'SetY' : undeclared identifier
h:\NSRM\Location.cpp(8): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(14): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(21): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(23): error C2588: '::~Location' : illegal global destructor
h:\NSRM\Location.cpp(24): error C2084: function 'int Location(void)' already has a body
h:\NSRM\Location.cpp(27): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(29): error C2065: 'x' : undeclared identifier
h:\NSRM\Location.cpp(31): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(33): error C2065: 'y' : undeclared identifier
h:\NSRM\Location.cpp(35): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(36): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(39): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(40): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(43): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(44): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(47): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(48): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Tile.cpp(6): error C2065: 'TheTileStatus' : undeclared identifier
h:\NSRM\Tile.cpp(6): error C2065: 'Sky' : undeclared identifier


When double-clicking on these items in the IDE's error window - it takes me to the line shown. There seems to be confusion within the progrm as to the definition of the Location class, although I made sure to put #include "Location.h" before Tile and WorldMap in the main file.
Does anyone know why this is happening?
-DragonGeo2
http://img360.imageshack.us/img360/4253/undefined4jf.png
I don't see why it thinks "Location" is an "undefined identifier" although the IDE simply finds it quite defined, as per this screenshot...
-DG
Don't write this much code at once until you have practice figuring out these things on your own. Take out a bunch of stuff, get the remaining things to *work*, and then put the rest back in gradually.
Yeah it kinda works when I comment out the line:
#include "WorldMap.h"

Can anyone answer me why my program isn't working?
Quote:Original post by DragonGeo2
Yeah it kinda works when I comment out the line:
#include "WorldMap.h"

Can anyone answer me why my program isn't working?


I would agree with the other poster, you really should pickup a good book on programming basics.... All these errors would be understandable even by reading Visual C++ in 21 Days. Published by Sams publishing(I think still).

here are some of yer errors, lets see if we can understand them:

h:\NSRM\Location.cpp(6): error C2065: 'SetX' : undeclared identifier
h:\NSRM\Location.cpp(7): error C2065: 'SetY' : undeclared identifier

-These 2 mean that SetX() and SetY() dont exist, Probably because u renamed them? double click the error to find where it is calling the wrong name.

h:\NSRM\Location.cpp(8): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(14): warning C4508: 'Location' : function should return a value; 'void' return type assumed
h:\NSRM\Location.cpp(21): warning C4508: 'Location' : function should return a value; 'void' return type assumed

-These warnings simply mean you did not return anything from Location. Try returning void...


h:\NSRM\Location.cpp(23): error C2588: '::~Location' : illegal global destructor
h:\NSRM\Location.cpp(24): error C2084: function 'int Location(void)' already has a body
-You have two bodies of code for Location, try finding where the second is and take it out. remove it most likely.

h:\NSRM\Location.cpp(27): error C2653: 'Location' : is not a class or namespace name
-This just means that because of the previous errors, Location could not be compiled and hence doesnt exist


h:\NSRM\Location.cpp(29): error C2065: 'x' : undeclared identifier
h:\NSRM\Location.cpp(31): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(33): error C2065: 'y' : undeclared identifier
h:\NSRM\Location.cpp(35): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(36): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(39): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(40): error C2365: 'SetX' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(43): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(44): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Location.cpp(47): error C2653: 'Location' : is not a class or namespace name
h:\NSRM\Location.cpp(48): error C2365: 'SetY' : redefinition; previous definition was a 'formerly unknown identifier'
h:\NSRM\Tile.cpp(6): error C2065: 'TheTileStatus' : undeclared identifier
h:\NSRM\Tile.cpp(6): error C2065: 'Sky' : undeclared identifier

-The rest of these should most likely be dealing with the previous errors, and will prolly need little adjusting after the first ones are fixed.

--X

This topic is closed to new replies.

Advertisement