Thread: extern struct...

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    75

    extern struct...

    Alright...I searched the boards...and thought I had found what i was looking for...but it didnt quite work...so i tried this...and tried that...still no luck...

    Code:
    //Pracitce Game
    //PracGame2.cpp
    #include<iostream>
    #include<windows.h>
    #include<string>
    #include<stdlib.h>//rand()
    #include<fstream>//file i/o 
    #include<basicf.h>//for clrscr();
    #include<color.h>//for color definition
    #include<time.h>//for time
    #include"GMS.h"//for struct define...
    using namespace std;
    
    
    
    int main()
    {
    	cout<<e1->PEN_NME<<endl;
    	return 0;
    }
    
    
    ////////////////////////////GMS.h//////////////////////
    
    
    //resource...
    //GMS.h
    #include<iostream>
    #include<windows.h>
    using namespace std;
    
    struct Enemy
    {
    	int MX_ATT;//max attack
    	int MN_ATT;//min attack
    	int MX_DEF;//max defense
    	int MN_DEF;//min defense
    	int MX_HP;//max hp
    	int MN_HP;//min hp
    
    	int T_TRN;//total turns
    	int R_TRN;//remaining turns
    
    	char *PEN_NME;//enemy name
    };
    
    Enemy Elias;
    Enemy *e1;
    Elias.PEN_NME=("Elias of Lithel");
    e1=&Elias;
    All i want is to be able to define all of the variables in the structure under many different names...this is a somewhat simplified version of the code...but if someone could help I would greatly appreciate it...
    MSVC++~

  2. #2
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Code:
    struct foo
    {
      union
      {
        int name1,name2,name3;
      };
    };
    Here, all three variable names are interchangeable.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    uhm...i dont really know anything about unions...but Im not sure if I explained myself well enough...

    struct Enemy; is going to have alot...and I mean alot of different definitions...and I dont want them in 'int main()'...it would be nice if I could have a seperate file for them...I tried using 'extern'...but I couldnt get it to do exactly what I needed it to...
    MSVC++~

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    well since no one rushed to my aid ;P (heh j/k)

    Ill just go with uploading all of the enemys into a predefined number of struct pointers and what not...

    thx anyways~
    MSVC++~

  5. #5
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Maybe this is what you need? Is the problem you are having in trying to access the e1 pointer from within the main function?

    Code:
    //Pracitce Game
    //PracGame2.cpp
    #include<iostream>
    #include<windows.h>
    #include<string>
    #include<cstdlib>//rand()
    #include<fstream>//file i/o 
    #include<basicf.h>//for clrscr();
    #include<color.h>//for color definition
    #include<ctime>//for time
    #include"GMS.h"//for struct define...
    using namespace std;
    
    extern ENEMY* e1;
    
    int main()
    {
    	cout<<e1->PEN_NME<<endl;
    	return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  6. #6
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    nope didnt work =(

    Code:
    Compiling...
    PracGame2.cpp
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2143: syntax error : missing ';' before '.'
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2501: 'Elias' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2371: 'Elias' : redefinition; different basic types
            c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(40) : see declaration of 'Elias'
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2143: syntax error : missing ';' before '.'
    c:\program files\microsoft visual studio\myprojects\pracgame2\pracgame2.cpp(27) : error C2374: 'El1' : redefinition; multiple initialization
            c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(41) : see declaration of 'El1'
    Error executing cl.exe.
    
    PracGame2.exe - 5 error(s), 0 warning(s)

    i mean...geez there has to be a way to do this...I just cant figure it out...thx for the try though =/

    <b>edit:</b>
    the pointer is now El1...because e1 is taken up doing something else now...
    MSVC++~

  7. #7
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Try changing this:

    Elias.PEN_NME=("Elias of Lithel");

    To this:

    Elias.PEN_NAME="Elias of Lithel";

    And see how you fare.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  8. #8
    Registered User
    Join Date
    Jan 2002
    Posts
    75
    well that did pull off one warning...but gah...still no luck!!

    Code:
    Compiling...
    PracGame2.cpp
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2143: syntax error : missing ';' before '.'
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2501: 'Elias' : missing storage-class or type specifiers
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2371: 'Elias' : redefinition; different basic types
            c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(40) : see declaration of 'Elias'
    c:\program files\microsoft visual studio\myprojects\pracgame2\gms.h(42) : error C2143: syntax error : missing ';' before '.'
    Error executing cl.exe.
    
    PracGame2.exe - 4 error(s), 0 warning(s)
    MSVC++~

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  2. Looking for constructive criticism
    By wd_kendrick in forum C Programming
    Replies: 16
    Last Post: 05-28-2008, 09:42 AM
  3. Concatenating in linked list
    By drater in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 11:10 PM
  4. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM
  5. Bi-Directional Linked Lists
    By Thantos in forum C Programming
    Replies: 6
    Last Post: 12-11-2003, 10:24 AM