Thread: Global pointer

  1. #1
    Registered User
    Join Date
    Dec 2007

    Global pointer

    I have a program with several cpp and header. I want a common instance of one of the classes for all places needed.
    What I did was to place (class Trials * currenttrial) inside another header file, initialize it in the main and include the header wherever I need to use this class.

    However, I get the following error

    GUI1 error LNK2005: "class Trials * currenttrial" (?currenttrial@@3PAVTrials@@A) already defined in VisualPerturbation.obj

    VisualPerturbation is one of the classes including the header and using this global instance. I got this error message for several other files containing this included header.


  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Farncombe, Surrey, England
    You need to put a "extern" in front of your global variable declaration, and then define the variable in ONE .cpp file without the extern.

    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 04:23 AM
  2. What is a virtual function pointer?
    By ting in forum C++ Programming
    Replies: 4
    Last Post: 03-05-2008, 02:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 09:40 PM
  4. Question About Pointer To Pointer
    By BlitzPackage in forum C++ Programming
    Replies: 2
    Last Post: 09-19-2005, 10:19 PM
  5. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM