0

Good Day,

I have tried to lookup how to compile several C++ files on a *nix command line.

I've tried these two links Using G++ to compile multiple .cpp and .h files

Using G++ to compile multiple .cpp and .h files

I have a simple abstract class:

  // Base class
  class Shape 
  {
  public:
     // pure virtual function providing interface framework.
     virtual int getArea() = 0;

     void setWidth(int w) {
        width = w;
     }

     void setHeight(int h) {
        height = h;
     }

  protected:
     int width;
     int height;
  };

Then a derived one:

    // Derived classes
    class Rectangle: public Shape
    {
    public:
       int getArea()
       { 
          return (width * height); 
       }
    };

Here is the driver:

  #include <iostream>

  using namespace std;

  int main(void)
  {
     Rectangle Rect;

     Rect.setWidth(5);
     Rect.setHeight(7);

     // Print the area of the object.
     cout << "Total Rectangle area: " << Rect.getArea() << endl;

     return 0;
  }

This is a simple one, so I don't need a makefile, but this is what I've tried:

> g++ Shape.cc  - This creates a Shape.o
> g++ Shape.cc Rectangle.cc ShapeDriver.cc - This creates an error
> g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error

It turns out that Rectangle.cc is not recognizing the width and height definitions, which makes sense.

What else do I need to do to get this to compile? I'm a total C++ noob.

TIA,

coson

Community
  • 1
  • 1
coson
  • 8,301
  • 15
  • 59
  • 84
  • 1
    How is the driver file supposed to compile with no `#include` for a header file that defines what a `Rectangle` is nor any class definition for it? How is the compiler going to even know that `Rectangle` has a default constructor when it compiles that file? – David Schwartz Sep 27 '16 at 04:47
  • `g++ Shape.cc Rectangle.cc ShapeDriver.cc` is a correct compilation command, the problem is that you have a bug in your code. `Rectangle.ccc` is probably a typo – M.M Sep 27 '16 at 04:55

1 Answers1

0

You need to add the following to the different files...

Top of Rectangle.cc

#include "Shape.cc"

Top of ShapeDriver.cc

#include "Rectangle.cc"

Also, in your third gcc line, you have a typo

g++ ShapeDriver.cc Shape.cc Rectangle.ccc - This creates an error

It should have been Rectangle.cc

What your problem is, is that in each of your files, the different classes have never been defined, so they don't know how to use them. Like.... "Rectangle" first needs to know what a "Shape" is before it derives from it. You should be using .h files in between w/ the class definitions, and including them in the other .cc files so they know the other class structures they are calling on.

Dakusan
  • 6,504
  • 5
  • 32
  • 45
  • Thank you. That's what is was. I didn't know you could refer to other class via #include "Class.cc". All of the examples I saw where #include "class.h". It never occurred that I could do "file.cc" also. And you were right, I did have the typo. – coson Sep 27 '16 at 05:17
  • Well, it's a bit more complicated than. You really shouldn't include .cc files from other files. It was a hack to help you get your project compiling. Doing so could normally create conflicts since the functions are declared multiple times. I believe the primary reason it works here is because all of your function definitions are inlined within the class, and you are compiling all the files at once (not as .o's that are later linked together). – Dakusan Sep 27 '16 at 05:42
  • The proper way to do things is to have .h files in between that lets everything else know the structure definitions, and then a .c/.cc file that contains the actual functions. Any file that would use those structures, including the .cc file that contains the function code for the members of the structure, would include that .h file . – Dakusan Sep 27 '16 at 05:43