Confusion with constructors and classes.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bparm
    New Member
    • Mar 2010
    • 3

    Confusion with constructors and classes.

    Hello, I am currently in a college class for programming with Object orientated C++ and I have been stuck with constructors and classes. The assignment I have been working with needs me to split the core file into a header, core and main file. I have all three written up but there's problems with it. While in the main I seem to be unable to call a function from the class. The problem it seems for me is with the constructor. As when I had the original file It ran fine when executing the main.(it was all on one file) Now with the header and main files with a constructor in the core file, I seem to be somewhat confused in what needs to be done. Currently I have a linking error, yet if I change things a little bit. I have this error:

    Code:
    \cit43\asteroidproject\asteroids.cpp(6) : error C2011: 'Asteroid' : 'class' type redefinition
    1>        e:\
    cit43\asteroidp roject\asteroid s.h(4) : see declaration of 'Asteroid'

    yet the link error is this:

    Code:
    >Asteroidmain.obj : error LNK2019: unresolved external symbol "public: void __thiscall Asteroid::displayStats(int,int,int)" (?displayStats@Asteroid@@QAEXHHH@Z) referenced in function _main
    1>E:\cit43\Asteroidproject\Debug\Asteroids.exe : fatal error LNK1120: 1 unresolved externals
    I'll post the snips of the code or if someone would like to help me figure this out(i'm not looking for a hand out, I've been working on this for two weeks troubleshooting and being stumped) I'll PM the code files, and maybe someone else with more experience can see where I'm going wrong with this.

    Maybe when i solve this problem it'll help me figure out my other problem which somehow makes rand print out -8 million and something when it was specified for something else. Again any and all help and insight will be appreciated.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    This should be fairly straightforward .

    You have to renmember that the compiler compiles each implementation file separately. That means if you need a class declaration in both files then each file must contain the declaration. You can't put it one file and expect the other files to pick up on that.

    So, let's assume this class:

    Code:
    class MyClass
    {
         public:
           void AMethod();
    };
    If I code the member function in a separate file, I need:


    Code:
    class MyClass
    {
         public:
           void AMethod();
    };
    
    void MyClass::AMethod()
    {
       //etc...
    }
    Then if I also need it in my main file:

    Code:
    int main()
    {
        MyClass obj;
        obj.AMethod();
    }
    Then I have to tell the compiler that MyClass is a class with AMethod, meaning I really need:

    Code:
    class MyClass
    {
         public:
           void AMethod();
    };
    
    int main()
    {
        MyClass obj;
        obj.AMethod();
    }
    Finally, is either write a makefile or use a development tool that uses projects to set up a compile of these two files and a link.

    The one weakness here is that the MyClass definition is on two places so if I need to chnage it I will need to change all of the places where it occurs. This is not a good situation. So, the common procedure is to create a third file that contaions the class definition and then include that file in each of the two files being compiled:

    Code:
    //File MyClass.h
    class MyClass
    {
         public:
           void AMethod();
    };
    And then im MyClass.cpp:

    Code:
    #include "MyClass.h"
    
    void MyClass::AMethod()
    {
       //etc...
    }
    And in my main file:

    Code:
    #include "MyClass.h"
    
    int main()
    {
        MyClass obj;
        obj.AMethod();
    }
    And off you go.

    Comment

    • bparm
      New Member
      • Mar 2010
      • 3

      #3
      Thanks for clearing it up, I'm still trying to get what i have to work properly. I've had problems while in the main file, calling one of the functions, I think I know why I'm having trouble but i cannot figure out how to fix it. I have a feeling that I need to use get, and set functions, just not a hundred percent sure. You don't mind if I send you my codes in a PM would you? so maybe I can get a little bit of help in figuring out where I went wrong? it seems that constructors seems to throw a lot of people I know off.

      Comment

      • weaknessforcats
        Recognized Expert Expert
        • Mar 2007
        • 9214

        #4
        Go ahead and send your code.

        Comment

        • bparm
          New Member
          • Mar 2010
          • 3

          #5
          Okay, thank you for helping.

          Comment

          Working...