C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 26th, 2009, 02:23 AM
biscuitdough biscuitdough is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 biscuitdough User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
Class declaration problems

I've been stuck on this error for about a week now and I cant figure out whats going wrong. I have 2 classes that have functions that accept pointers to eachother, and have private variables that point to eachother. One is The map class for a game, and the other is a player class. If i try includeing the class each class is referenceing, i end up with the common problem of one of the classes having to be declared before the other. so to fix this i try to do a class declaration of map in the player file, and player in the map file. then it compiles fine, but when i try to link the object files together, all hell breaks loose.
heres some of the code

Player.h
Quote:
#ifndef PLAYER_H
#define PLAYER_H
#include <string>
#include "SDL/SDL.h"

//forwared declaration
class Map_Wrap;

class Player
{
public:
Player();
~Player();
int getx();
int gety();
int getr();
void change_map (Map_Wrap *new_map);
int handle_input(SDL_Event event, Uint8 *keystates);
void move();
void paint_screen(SDL_Surface *screen);
bool check_collision( SDL_Rect A );

private:
SDL_Surface* add_surface(std::string filename);
SDL_Surface *dot;
SDL_Surface *direction_dot;
SDL_Rect box;
Map_Wrap *current_map;
double xmomentum;
double ymomentum;
//angular momentum
double amomentum;
//direction in radians
double direction;
int radius;
int small_rad;
int HP;
};


#endif


map_wrap.h

Quote:
#ifndef MAP_H
#define MAP_H
#include <vector>
#include <string>
#include "SDL/SDL.h"

const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
const int SCREEN_BBP=32;
const int TILE_SIZE = 50;

//forward declaration
class Player;

class Map_Wrap
{
public:
Map_Wrap(std::string filename);
~Map_Wrap();
std::vector<std::vector<int> > load_map (std::string filename);
SDL_Surface* draw_full_map ();
void apply_background (SDL_Surface *screen);
SDL_Rect center_camera(Player *player);
int get_height();
int get_width();
bool redraw_map();
private:
int map_width;
int map_height;
std::vector<std::vector<int> > raw_map_data;
SDL_Rect camera;
int x_index_tile;
int y_index_tile;
SDL_Surface *part_map;

};

#endif


maintest3.cpp
Quote:
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
#include <vector>
#include "SDL/SDL.h"
#include "SDL/SDL_image.h"
#include "timer.h"
#include "map_wrap.h"
#include "player.h"

int main ()
{
const int FRAMES_PER_SECOND = 30;

SDL_Surface *screen=NULL;
//screen setup
{
//Initialize all SDL subsystems
if( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )
{ exit(1); }
//Set up the screen
screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BBP, SDL_SWSURFACE );
//If there was an error in setting up the screen
if( screen == NULL )
{ exit(1); }
}

SDL_WM_SetCaption( "East and Evergreen", NULL );

//The frame rate regulator
Timer fps;
bool quit=false;
SDL_Rect *camera = new SDL_Rect;

SDL_Event event;
Uint8 *keystates = SDL_GetKeyState( NULL );

Map_Wrap *test_map= new Map_Wrap("map.map");
SDL_Surface *background = test_map->draw_full_map();
//int map_height=test_map.size();
//int map_width= test_map[0].size();
Player *p1 = new Player;
p1->change_map(test_map);

while (!quit)
{
fps.start();
while (SDL_PollEvent(&event))
{
if( event.type == SDL_QUIT )
{quit = true;}
}
p1->handle_input(event,keystates);
//p1.check_collision(wall);
p1->move();
//center camera
test_map->center_camera(p1);
//apply surfaces

p1->paint_screen(screen);
//apply_surface( background, screen, 0,0, camera );
//refresh
if( SDL_Flip( screen ) == -1 ) { return 1; }


if( fps.get_ticks() < 1000 / FRAMES_PER_SECOND )
{
//Sleep the remaining frame time
SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() );
}
}
SDL_FreeSurface( background );
delete camera;
delete p1;
}


output when trying to link

Quote:
/home/biscuitdough/Desktop/east/eastv1/maintest3.cpp:80: undefined reference to `Timer::Timer()'
/home/biscuitdough/Desktop/east/eastv1/maintest3.cpp:87: undefined reference to `Map_Wrap::Map_Wrap(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/home/biscuitdough/Desktop/east/eastv1/maintest3.cpp:88: undefined reference to `Map_Wrap::draw_full_map()'
/home/biscuitdough/Desktop/east/eastv1/maintest3.cpp:91: undefined reference to `Player::Player()'
/home/biscuitdough/Desktop/east/eastv1/maintest3.cpp:92: undefined reference to `Player::change_map(Map_Wrap*)'


and on and on for every function call from those classes.

please, any help would be nice, was making great progress before hitting this brick wall.

Reply With Quote
  #2  
Old June 26th, 2009, 03:46 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 26 m
Reputation Power: 5
How are you compiling? It seems you are not linking in any of the object files for those classes.
__________________
There is no such thing as C/C++, you either program C or C++

Reply With Quote
  #3  
Old June 26th, 2009, 03:24 PM
biscuitdough biscuitdough is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 biscuitdough User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
my make file

makefile
Quote:
graphtest: maintest3.o graphics_wrapper.o player.o timer.o
g++ -o graphtest maintest3.o map_wrap.o player.o timer.o -lSDL -lSDL_ttf -lSDL_image -lSDL_mixer


# define how each object file is to be built
maintest3.o: maintest3.cpp map_wrap.cpp map_wrap.o
g++ -c -g maintest3.cpp -lSDL -lSDL_image

map_wrap.o: map_wrap.cpp map_wrap.h player.o
g++ -c -g map_wrap.cpp -lSDL -lSDL_image

player.o: player.cpp player.h timer.o
g++ -c -g player.cpp -lSDL -lSDL_image

timer.o: timer.cpp timer.h
g++ -c -g timer.cpp -lSDL -lSDL_image

Reply With Quote
  #4  
Old June 27th, 2009, 01:59 AM
biscuitdough biscuitdough is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 biscuitdough User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
ok

ok, i messed witha few things, have gotten the error messages into a mangable state, and fixed a few problems i was going to have later.
what i have now, is that the functions can communicate with main fine, but can't access functions in eachother.
my output now:
Quote:
biscuitdough@GYPSY:~/Desktop/east/eastv1$ make
g++ -c -g player.cpp -lSDL -lSDL_image
g++ -c -g map_wrap.cpp -lSDL -lSDL_image
g++ -c -g maintest3.cpp -lSDL -lSDL_image
g++ -o graphtest maintest3.o map_wrap.o player.o timer.o sdl_global.o -lSDL -lSDL_ttf -lSDL_image -lSDL_mixer
map_wrap.o: In function `Map_Wrap::center_camera(Player*)':
/home/biscuitdough/Desktop/east/eastv1/map_wrap.cpp:71: undefined reference to `Player::getx()'
/home/biscuitdough/Desktop/east/eastv1/map_wrap.cpp:72: undefined reference to `Player::gety()'
player.o: In function `Player::move()':
/home/biscuitdough/Desktop/east/eastv1/player.cpp:137: undefined reference to `Map_Wrap::get_width()'
/home/biscuitdough/Desktop/east/eastv1/player.cpp:139: undefined reference to `Map_Wrap::get_height()'
collect2: ld returned 1 exit status
make: *** [graphtest] Error 1


all im doing is #includeing player.h in my map_wrap.cpp file and map_wrap.h in my player.cpp file. do i need to pre-declare all my player functions that i have to use in map_wrap.cpp extern and take out the #includes? (and vice versa for player.cpp)

Reply With Quote
  #5  
Old June 27th, 2009, 04:11 AM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 26 m
Reputation Power: 5
No you don't need to do that. Including it should work just fine.. It has been a long time since I have written my own make file..

Can you post all the code for player and map_wrap, header and .cpp? Just to check that nothing strange is happening there. Try to do make clean (or better manually delete all object files) before you compile to find out if the makefile is ok.

Reply With Quote
  #6  
Old June 27th, 2009, 03:36 PM
biscuitdough biscuitdough is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 biscuitdough User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
Here's all my source so far. this fourm doesnt allow links, so have to fudge it.

Quote:
(dubdubdub)(dot)filedropper(dot)com(slash)projectf iles

Reply With Quote
  #7  
Old June 28th, 2009, 02:44 PM
Icon's Avatar
Icon Icon is offline
Command Line Warrior
Click here for more information. Click here for more information
 
Join Date: Sep 2005
Posts: 942 Icon User rank is Private First Class (20 - 50 Reputation Level)Icon User rank is Private First Class (20 - 50 Reputation Level) 
Time spent in forums: 1 Week 6 Days 9 h 26 m
Reputation Power: 5
Ok, I can compile it if I remove all the SDL stuff. I have no time to install sdl and it has been a time since I've used it.

In your makefile your executable was depending on graphics_wrapper.o which does not exist. I really suggest you delete all .o files each time you compile to make sure you're not compiling against old object files. Sorry I can't be more helpful.

Reply With Quote
  #8  
Old June 28th, 2009, 08:58 PM
biscuitdough biscuitdough is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2009
Posts: 6 biscuitdough User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 18 m 50 sec
Reputation Power: 0
no man, thank you so much for just looking. It's linking ok now, but im still trying to figure out why. What i had to do was remove all my inline functions in player.cpp and map_wrap.cpp and it linked up fine.
have any idea why this would fix it? and for the tiny, tiny improvement i would get from inline, is there anyway i can get inline to work?
ive tryed both the alias for inline by putting the functions in the .h file, and moveing them to the cpp file with the word inline before the function declaration, but neither fixes the problem im having.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Class declaration problems


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

Request Your Free Technology Downloads!
 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

Request Your Free Technology Downloads!
 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

Request Your Free Technology Downloads!
 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

Request Your Free Technology Downloads!
 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

Request Your Free Technology Downloads!
 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
Stay green...Green IT