CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2006
    Posts
    292

    Mulitiple definitions

    Hi all, I'm trying to write to a file using a global variable. For example....

    Code:
    /* prog1.cpp */
    
    #include "header.h"
    ofstream out;
    out.open(test,ios::app);
    out << "Prog1 " << endl;
    out.close();
    
    /* prog2.cpp */
    
    #include "header.h"
    ofstream out;
    out.open(test,ios::app);
    out << "Prog2 " << endl;
    out.close();
    
    /* header.h */
    
    char test[MAX_PATH]="file.txt";
    However when I compile I get the multiple definition of `test' error for each source file. Does anyone know how I can sort this out? Thanx.

  2. #2
    Join Date
    Aug 2008
    Posts
    902

  3. #3
    Join Date
    Nov 2006
    Posts
    292

    Re: Mulitiple definitions

    Is it possible to get an answer with out being redirected to a article? Does anyone have a C++ answer for me?

  4. #4
    Join Date
    Aug 2008
    Posts
    902

    Re: Mulitiple definitions

    You should probably read the article regardless, as I believe you'd not only find the answer to your current question, but probably your second, and third, etc that you are likely to run into later.

    If you want the short answer, it's because of multiple inclusion.

    Code:
    #ifndef HEADER_H_
    #define HEADER_H_
    
    /* header.h */
    char test[MAX_PATH]="file.txt";
    
    #endif /*FUNCS_H_*/

  5. #5
    Join Date
    Nov 2008
    Location
    England
    Posts
    748

    Re: Mulitiple definitions

    In one cpp file define the variable, in all other translation units that need the variable declare as extern.
    Get Microsoft Visual C++ Express here or CodeBlocks here.
    Get STLFilt here to radically improve error messages when using the STL.
    Get these two can't live without C++ libraries, BOOST here and Loki here.
    Check your code with the Comeau Compiler and FlexeLint for standards compliance and some subtle errors.
    Always use [code] code tags [/code] to make code legible and preserve indentation.
    Do not ask for help writing destructive software such as viruses, gamehacks, keyloggers and the suchlike.

  6. #6
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: Mulitiple definitions

    Quote Originally Posted by Russco View Post
    In one cpp file define the variable, in all other translation units that need the variable declare as extern.
    Russco is right. The error is not because of multiple inclusion but cause the global variable was DEFINED in the header so that each cpp that includes the header would create a new instance of that variable (symbol). The linker then will complain because of that.

    So, in the header you would need only a DECLARATION what is made by the extern keyword. And one and only cpp MUST have a definition of the variable simply like

    Code:
    // header
    ...
    extern char test[MAX_PATH];
    ...
    
    // first.cpp 
    #include "header.h"
    
    char test[MAX_PATH] = "file.txt";
    You see the use of shared global variables among source files is quite tricky. You better use a class and define a static public member.

    Code:
    // global.h
    #ifndef GLOBAL_H
    #define GLOBAL_H
    
    class Global
    {
    public:
        static char * getTest()
        {
               static char test[MAX_PATH] = "file.txt";
               return test;
        }
    };
    #endif
    
    // any cpp 
    #include "global.h"
    ...
        // set another file
        strcpy(Global::getTest(), "anotherfile.txt");
    
        // use filename
        ifstream ifs(Global::getTest());
    Beside of using plain char pointers the above is much easier and a C++ solution. And the char pointer easily could be replaced by a std::string.

  7. #7
    Join Date
    Nov 2006
    Posts
    292

    Re: Mulitiple definitions

    Thanx for the solution itsmeandnobodyelse, I've been trying to understand the concept of classes public and private over and over. Reading many many tutorials about it and it just seems to slip my mental grasp but I'll keep trying to learn and understand it. However, I'm having the same error. I declared it in the first .cpp like you said and extern in the header and the same error exists. What gives?
    Last edited by dellthinker; November 11th, 2010 at 11:54 AM.

  8. #8
    Join Date
    Jul 2009
    Location
    India
    Posts
    835

    Re: Mulitiple definitions

    Just one thing. Rather than using

    #ifndef GLOBAL_H
    #define GLOBAL_H

    ....
    ......

    #endif

    you can simply use #pragma once

  9. #9
    Join Date
    Jan 2006
    Location
    Singapore
    Posts
    6,765

    Re: Mulitiple definitions

    Quote Originally Posted by hypheni
    Just one thing. Rather than using

    #ifndef GLOBAL_H
    #define GLOBAL_H

    ....
    ......

    #endif

    you can simply use #pragma once
    But be warned that #pragma once is non-standard. I suggest that you use normal header inclusion guards.
    C + C++ Compiler: MinGW port of GCC
    Build + Version Control System: SCons + Bazaar

    Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
    Kindly rate my posts if you found them useful

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured