Menu

Build Error when Compiling - how to compile!

Koko
2002-11-25
2012-09-26
  • Koko

    Koko - 2002-11-25

    Hi,
    I am a newbie!
    I want to create a DevC++ project that will compile a few classes with their header files in the correct order!

    Now I would like to understand something. Should all my class declarations be in a .h file and my class definitions be in a .cpp file?
    example:

    class Cat
    {
    private:
       int age;
    public:
        Cat();
        ~Cat();
    }

    Now should I include the top class declaration in Cat.h?

    how about the functions and methods for Cat class, do they go in my Cat.cpp?

    example:

    Cat::Cat()
    {
    cout<<Cat constructed!"<<endl;
    }

    Cat::~Cat()
    {
    cout<<Cat destructed!"<<endl;
    }

    does this code go in my Cat.cpp

    do I include "Cat.h" in my Cat.cpp file?

    now if i have a few more classes like this, how do i compile them all?

    say iw ant to run everything from a main class, which only has my main method

    please help.

    thank you.

     
    • Curtis Sutter

      Curtis Sutter - 2002-11-28

      1) Yes you should put

      class Cat
      {
      private:
      int age;
      public:
      Cat();
      ~Cat();
      }

      in a file called Cat.h

      and

      #include "Cat.h"
      Cat::Cat()
      {
      cout<<Cat constructed!"<<endl;
      }

      Cat::~Cat()
      {
      cout<<Cat destructed!"<<endl;
      }

      in a file called Cat.cpp

      2) In your main code (where int main resides) add
      #include "Cat.h"

      3) Putting all of you classes in seperate files can be good (re-usable code) and bad (1000's of files).

      4) To compile everything just add the *.cpp files (Cat.cpp) to your project.

      This should work, as I have had it working for me.

      Curtis

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.