Global pointer

This is a discussion on Global pointer within the C++ Programming forums, part of the General Programming Boards category; I have a program with several cpp and header. I want a common instance of one of the classes for ...

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    49

    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.

    Help?

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    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.

    --
    Mats
    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, 01:36 AM
  3. scope of a pointer?
    By Syneris in forum C++ Programming
    Replies: 6
    Last Post: 12-29-2005, 08: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, 01:19 AM

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