how can declare a global class?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zahraZ
    New Member
    • Jul 2010
    • 17

    #1

    how can declare a global class?

    Hi,
    i want to declare a global class that another classes can use this class.
    so, i write this:
    Code:
    extern [I]classname[/I] a;
    but this error occur:
    Code:
    error LNK2001: unresolved external symbol "class [I]classname[/I] a" (?a@@3V[I]classname[/I]@@A)
    what can i do?
    thanks,
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    When you code:

    Code:
    extern classname a;
    you are telling the compiler that there is an object a of type classname but the object s not in this file. Hence the extern.

    Notice your error is an LNK error. Your code compiled fine but the linker can't find the a object.

    You need to create the object as a global variable in another file:

    Code:
    classname a;

    Comment

    • zahraZ
      New Member
      • Jul 2010
      • 17

      #3
      yes,
      i know it's not right ,i don't know what shoud i do i want to solve this problem:
      i have a class(A) that i include it's .h file in another class(B). in class B i have a object of class A in a function.
      in another class i want to use this object . So i think that, this object of class A must be global.

      can you help me?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Code:
        in class B i have a object of class A in a function.
        in another class i want to use this object .
        You mean like:

        Code:
        class B
        {
           A obj;
        
        
        
        };
        and it's an object of B that you want to use?

        Maybe you could post a code snippet that shows what you want to do.

        Comment

        • zahraZ
          New Member
          • Jul 2010
          • 17

          #5
          this is a simple code:
          .h file of class A
          Code:
          class A{
          public:
             A(){}
          }
          this is class B's .h file:
          Code:
          class B{
          public:
             void readData();
          }
          class B's cpp file
          Code:
          #include "B.h"
          #include "A.h"
          void b::readData(){
              A aa;
          }
          I want to use aa in class C. how should i declare aa??
          thanks alot

          Comment

          • weaknessforcats
            Recognized Expert Expert
            • Mar 2007
            • 9214

            #6
            Code:
            #include "B.h" 
            #include "A.h" 
            void b::readData(){ 
                A aa; 
            }
            In this example, aa is a local variable in b::readData().

            When you call b::readData, the default constructor for class A will be called. When you leave the function, the class A destructor will be called. Therfore, aa will exist only as long as you are in this function.

            If you intend to keep aa around so you can use it in other member functions, then aa needs to be a member variable of B rather than a local variable in b::readData():

            Code:
            class B{ 
            private:
               A aa;
            public: 
               void readData(); 
            };
            BTW: None of your class declarations have a terminating semi-colon. I added one in the above example.

            Comment

            • zahraZ
              New Member
              • Jul 2010
              • 17

              #7
              Thanks alot,
              it was very useful. thanks for (;) too, i forget to write them.
              thanks again

              Comment

              Working...