+ Reply to Thread
Results 1 to 2 of 2

Thread: multiple definition of a variable

  1. #1
    Junior Member
    Join Date
    Jun 2012
    Posts
    14

    multiple definition of a variable

    I have 2 headers, and 2 code files. File1.cpp, File1.h, File2.cpp, File2.h

    File1.cpp:

    #include "File1.h"

    int main()
    {
    hello();
    return 0;
    }

    void hello()
    {
    //code here
    }
    File1.h:

    #ifndef FILE1_H_
    #define FILE1_H_
    #include "File2.h"
    void hello();

    #endif /* FILE1_H_ */
    File2.cpp:

    #include "File2.h"

    void world()
    {
    //more code;
    }
    File2.h:

    #ifndef FILE2_H_
    #define FILE2_H_

    int wat;
    void world();


    #endif /* FILE2_H_ */
    when I compile it ,multiple definition of wat is shown . help plz ?

  2. #2
    Junior Member
    Join Date
    Jun 2012
    Posts
    14
    To fix, use the following:
    extern int wat;
    File2.cpp

    int wat = 0;
    This (extern) tells the compiler that the variable wat exists somewhere, and that it needs to find it on it's own (in this case, it's in File2.cpp)

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

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