Thread: Structure trouble...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    673

    Structure trouble...

    OK, lets see I am trying to use a ONE structure with multiple .cpp's (which I got to work just not the right way). I initialized my structure in my header file. I trying to find a way to save a value in a int or string from the structure and then be able to output the value of the struct variable in another .cpp... I search for about 2 hours on google and the boards themselves ( Im not to good at searching for stuff )... I did end up figuring out how to have 2 different values with the same name but not a way to have one with same name over the span of multiple .cpp's. any help is greatly appreciated (BELOW is a small sample program I made to try this ( it doesnt work the way I want tho ))

    This is my header
    Code:
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    
    using namespace std;
    
    void menu();
    void load();
    void newg();
    int main();
    
    struct test {
    	int tst;
    };
    test tet;
    This is one .cpp
    Code:
    #include "all.h"
    
    int main()
    {
    	cout << "choose a value for tst" << endl;
    	cin >> tet.tst;
    	cin.get();
    	menu();
    }
    This is my other .cpp
    Code:
    #include "all.h"
    
    void menu()
    {
    	cout << "tst Value you chose" << tet.tst << endl;
    	cin.get();
    }
    and this is the error that I get
    Code:
    .objs\main.o:main.cpp:(.bss+0x0): multiple definition of `tet'
    Thanks again for any help you provide

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    You need to use inclusion guards. Google it.

  3. #3
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    The include guards didnt help me any.... any other ideas? ( I am searching google at the moment for possible solutions but I would be eternally grateful for your support)

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    int main();
    The global main function shall not be used within a program, so there is really no need for you to have a prototype for it.

    The include guards didnt help me any.... any other ideas?
    The include guards should work. However, a redesign to not use a global test object is in order. Have tet created in main(), then pass it to menu(). Do not declare it in your header. If not, you have one tet used in your main() function and another used in menu(). You could declare it as extern... but why bother with a global test object in the first place?
    Last edited by laserlight; 06-25-2006 at 12:18 AM.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    How do you declare a struct int a extern?

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > test tet;
    This is the thing you're creating multiple instances of - it's a definition (of storage), not a declaration.
    It's what the error message is telling you.

    Move this single line to just ONE of the cpp files (probably main.cpp).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    How do you declare a struct int a extern?
    A struct isnt an int.
    You would probably have an:
    Code:
    extern test tet;
    in your header file, but a better way is a redesign:

    the all.h header file:
    Code:
    #ifndef ALL_H
    #define ALL_H
    
    #include <iostream>
    #include <windows.h>
    #include <fstream>
    
    struct test {
    	int tst;
    };
    
    void menu(const test& tet);
    void load();
    void newg();
    
    #endif
    the file with the global main() function:
    Code:
    #include "all.h"
    
    int main()
    {
    	test tet;
    	cout << "choose a value for tst" << endl;
    	cin >> tet.tst;
    	cin.get();
    	menu(tet);
    }
    the file with the implementation of the menu() function:
    Code:
    #include "all.h"
    
    void menu(const test& tet)
    {
    	cout << "tst Value you chose" << tet.tst << endl;
    	cin.get();
    }
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    673
    oh thanks alot, so basicly I just had a design problem. you a life save thanks ( and I wasnt calling the struct and int I was talking about the int inside the struct just had bad wording)
    thanks again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 13
    Last Post: 12-14-2007, 03:34 PM
  2. trouble understanding the source file structure
    By Mario F. in forum C++ Programming
    Replies: 5
    Last Post: 05-26-2006, 06:46 PM
  3. trouble with structure
    By sal817 in forum C Programming
    Replies: 3
    Last Post: 03-15-2006, 11:27 AM
  4. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  5. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM