instance project

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • mar11
    New Member
    • Sep 2009
    • 38

    instance project

    Hi all,

    i am having problem by instantiating an object of class in the same source file.
    say, we have this code :

    // file.h
    #ifndef A
    #define A

    class A{

    A();
    ~A();
    // member functions
    A_foo();
    };
    #endif;

    // file.cpp
    #include file.h

    A Obj; // here is the cause of error

    void A_foo(){

    // something

    }

    when i execute the program i get segmentation error. If i increment the instance Obj . the program can be executed somehow!!

    How can i declare this Obj of the same class file?

    may someone help me with any suggestion?
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You need to include file.h and not file.cpp.

    .cpp files are never included.

    Comment

    • mar11
      New Member
      • Sep 2009
      • 38

      #3
      you are right. i was in haste.. but that was not my real question!!

      do you have any suggestion?

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Your code won't even compile. How can you possibly be getting a segmentaion fault?

        You are creating a global object of type A but your class A methods are all private so there is no way the compiler can call the A default constructor. This is a compile error.

        Also, your A class method A_foo has no return type in the class declaration but in the code for the function it returns void.

        Lastly, your class name is A and your #ifndef ios also A. That's not going to work.

        If you are actually getting this to compile, please get a compiler that works.

        Comment

        • mar11
          New Member
          • Sep 2009
          • 38

          #5
          object instance

          your right!! the code are non-compilable. but that is still not my question.

          My question is how can i implement an global object correctly in "the same file structure" as it is written above??



          thanks

          Comment

          • Banfa
            Recognized Expert Expert
            • Feb 2006
            • 9067

            #6
            Well ignoring all the compile errors and missing code what you have in the first post should work so that would suggest the error is in something you chose not to post. Post a compilable example that produces the error and we may be able to be more specific.

            Here is a minimal working example
            Code:
            class GlobalData
            {
            };
            
            GlobalData obj;
            
            int main()
            {
            }
            However that aside in C global data was bad practice but not always avoidable, in C++ it is not only bad practice but is generally easily avoidable, I suggest you look up the singleton design pattern

            Comment

            Working...