Not logged in, Join Here! or Log In Below:  
 
News Articles Search    
 

 Home / General Programming / Simple syntax question Account Manager
 
Archive Notice: This thread is old and no longer active. It is here for reference purposes. This thread was created on an older version of the flipcode forums, before the site closed in 2005. Please keep that in mind as you view this thread, as many of the topics and opinions may be outdated.
 
picard81

April 10, 2005, 10:41 AM

Hi all!
There's an open source project and I'm trying
to play around with the code but I've a little problem.
example:
i've a file file1.cpp which contains a struct

code:
struct {
int field1;
...
} blah;



now I would use this struct into file2.cpp without changing
file1.cpp; how do I do?
sorry for the stupid question; I'm using visualc .net 2003
thanks in advance!

 
timm

April 10, 2005, 10:59 AM

Declare the struct in a header file.

 
picard81

April 10, 2005, 11:04 AM

Don't works: when I put .h into file1 and file2 the
compiler says that the struct is already defined.
I would write something like:
extern ... struct blah
like I do with any other variable but don't go with
structs...

 
NCCAChris

April 10, 2005, 11:17 AM

in the header declare at the top

#pragma once

or

#ifndef _SOMENAME_H_
#define _SOMENAME_H_

your code and struct here

#endif

_SOMENAME_H_ is a unique name - usually based on the name of the header - a simple hack if u will to ensure that the header is only included once

 
NCCAChris

April 10, 2005, 11:21 AM

but really u should look at a decent c/c++ book first before coding - then u might not need to ask such questions.....

 
DonJuan

April 10, 2005, 03:44 PM


Bad code! Bad engine!

 
Mark Friedenbach

April 10, 2005, 05:09 PM

Change

  1.  
  2. struct {
  3. int field1;
  4. ...
  5. } blah;
  6.  

into
  1.  
  2. struct blah_tag {
  3. int field1;
  4. ...
  5. };
  6.  
  7. extern struct blah_tag blah;
  8.  

And move it into a header file shared by both file1.cpp and file2.cpp. Now where the code was originally located in file1.cpp, put the following
  1.  
  2. struct blah_tag blah;
  3.  

good luck

 
This thread contains 7 messages.
 
 
Hosting by Solid Eight Studios, maker of PhotoTangler Collage Maker.