LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 07-23-2006, 07:36 PM   #1
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Rep: Reputation: 30
Problem with extern struct object, in header file. Undefined type.


I am having a problem with my program. I am getting an error (shown below) when compiling my game.
I have the struct defined in a seperate file called variables.cpp, along with a global object array.

So it looks like this:

Code:
struct CharObj
{
    //...
}character_obj[128];
While in the header file I made an external reference to it, looks like the following:

Code:
extern struct CharObj character_obj[128];
Yet, when I get the following error when compiling:

Code:
g++ -c -g Char_Movement.cpp
Char_Movement.cpp: In function `void PlayerMove(bool, bool, bool, bool, int)':
Char_Movement.cpp:10: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:10: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:22: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:22: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:34: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:34: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:46: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
Char_Movement.cpp:46: error: invalid use of undefined type `struct CharObj'
space_carnage.h:67: error: forward declaration of `struct CharObj'
make: *** [Char_Movement.o] Error 1
Any suggestions? If need more information, please ask and I should respond right away.
 
Old 07-23-2006, 07:59 PM   #2
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 46
did you define "struct CharObj" anywhere that is included by this file?
 
Old 07-23-2006, 08:32 PM   #3
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
I defined struct CharObj in another file called variables.cpp. I'm thinking you mean did I make an object of this struct right? If so, I did in that same variables.cpp file.

It looks like the following:

// Variables.cpp file

Code:
struct CharObj
{
    //...
}character_obj[128];
// space_carnage.h

Code:
extern struct CharObj character_obj[128];
Obviously the above two pieces of codes from the files exclude all other variables and predecessor files.

I believe I may have misunderstood you, correct me if I am right.

Last edited by RHLinuxGUY; 07-23-2006 at 08:33 PM.
 
Old 07-23-2006, 08:36 PM   #4
spooon
Senior Member
 
Registered: Aug 2005
Posts: 1,755

Rep: Reputation: 46
Well Char_Movement.cpp needs to know what the hell a "struct CharObj" is, if it appears in its code. So you should put the definition of it, i.e.
Code:
struct CharObj
{
    //...
};
in the header file.
 
Old 07-23-2006, 09:40 PM   #5
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Thank you, that works. I have a question though, how would I be able to make an class/struct object global to all files? I already found another way to send data with pointers, but as a future reference, is it possible?
 
Old 07-23-2006, 10:05 PM   #6
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
If you're going to use a particular struct type in a number of places, it's best to place it in a header file rather than to define a type in a C/CPP file.

For future reference, the following should work as well:
Code:
extern struct CharObj *character_obj;
 
Old 07-23-2006, 11:55 PM   #7
RHLinuxGUY
Member
 
Registered: Oct 2003
Distribution: Ubuntu 7.04
Posts: 889
Blog Entries: 1

Original Poster
Rep: Reputation: 30
Thank you, I got it now.
 
Old 07-24-2006, 12:01 AM   #8
Matir
Moderator
 
Registered: Nov 2004
Location: Kennesaw, GA
Distribution: Ubuntu
Posts: 8,502

Rep: Reputation: 110Reputation: 110
No problem. Just trying to expose all possible solutions, especially in the hopes that future searchers may find it useful.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
undefined symbol in the shared object phani@tcs Linux - General 3 01-10-2006 03:51 PM
extern "C" and static data type problem with g++ vtluu Red Hat 1 05-21-2004 10:45 AM
extern "C" and static data type problem with g++ vtluu Programming 2 04-28-2004 05:10 AM
problem with extern file pointer in library linuxping Programming 5 02-08-2004 09:55 PM
using struct type X as pointer in struct X. worldmagic Programming 1 10-28-2003 02:06 PM


All times are GMT -5. The time now is 02:49 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration