multiple definitions of msg

This is a discussion on multiple definitions of msg within the C++ Programming forums, part of the General Programming Boards category; When I compile the blow code I get 'multiple definition of msg' error Code: //CTask.h #include <iostream> #include <queue> #include ...

  1. #1
    Registered User
    Join Date
    Oct 2002
    Posts
    291

    multiple definitions of msg

    When I compile the blow code I get 'multiple definition of msg' error
    Code:
    //CTask.h
    #include <iostream>
    #include <queue>
    #include <stdlib.h>
    #include "CQueue.h"
    #define Terminated 100
    #ifndef _CTASK_H
    #define _CTASK_H
    
    using namespace std;
     
    class CTask
    {
      public:
      CTask();
      virtual ~CTask();
      
    };
    CQueue *msg = new CQueue();
    
    #endif
    Any ideas what might be wrong ?

  2. #2
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    msg is defined in one of the headers you include

  3. #3
    booyakasha
    Join Date
    Nov 2002
    Posts
    208
    do you define a variable named msg in CQueue.h also?

  4. #4
    Registered User
    Join Date
    Oct 2002
    Posts
    291
    Originally posted by beege31337
    do you define a variable named msg in CQueue.h also?
    No.

    I have tryed several names and they all get the same error message as above.

  5. #5
    Used Registerer jdinger's Avatar
    Join Date
    Feb 2002
    Posts
    1,065
    Not a good idea to dynamically create the object in the header file. Not sure why you'd want to in the first place. Basically what's happening is that every time the CTask.h header is included in a file it tries to create the msg object.

  6. #6
    Programming Sex-God Polymorphic OOP's Avatar
    Join Date
    Nov 2002
    Posts
    1,078
    He's right. I didn't notice it was a header file that you had the declaration in.

    In general you really do NOT want global variables like that, ESPECIALLY ones which span multiple files. It's considered bad practice, but if you really want to then you should use the keyword

    extern

    at the beginning of the declaration.

    Look up singletons for a more elegant solution to global variables -- but still, avoid needing global access to any variables as much as you can.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 06-08-2009, 04:03 PM
  2. Replies: 5
    Last Post: 08-06-2008, 10:59 AM
  3. Multiple definitions?
    By Ganoosh in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2005, 05:20 AM
  4. Linker errors - Multiple Source files
    By nkhambal in forum C Programming
    Replies: 3
    Last Post: 04-24-2005, 03:41 AM
  5. Replies: 1
    Last Post: 05-01-2003, 03:52 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21