Menu

user created libraries

2002-09-18
2012-09-26
  • Nobody/Anonymous

    I'm new to using user created libraries on dev-cpp, and I've been getting some errors, so I would like to make sure I'm doing things right. first...
    .h file-
    #ifndef mylibrary_h
    function declarations
    #define mylibrary_h
    #endif

    right?
    now standard protocol is for the mylibrary.cpp file to include the mylibrary.h file, and for the driver to include the mylibrary.cpp file. I try this, but I get linking errors. Also, I put my files in the "include" folder. Is this correct?

     
    • Nobody/Anonymous

      Once I include my .h file, it says I have an unterminating #if statement and garbage at the end of my #ifndef statement.

       
    • Nobody/Anonymous

      This is not mine. Just wanted to give something to go by.
      Hope it helps.

      #ifndef BOX_H
      #define BOX_H

      class Box
      {
        public:
          // Constructor
          Box(double lengthValue = 1.0, double breadthValue = 1.0, double heightValue = 1.0);

          // Function to calculate the volume of a box
          double volume();

        private:
          double length;
          double breadth;
          double height;
      };

      #endif

          Larry

       
    • Nobody/Anonymous

      Note:

      // Constructor
      Box(double lengthValue = 1.0, double breadthValue = 1.0, double heightValue = 1.0);

      and -

      // Function to calculate the volume of a box
      double volume();

      These two are defined in another file.  'Box.cpp'.

        Larry

       
    • Nobody/Anonymous

      Here is the other file that has the definitions.

      #include <iostream>
      #include "Box.h"

      using namespace std;

      // Constructor definition using an initializer list
      Box::Box(double lvalue, double bvalue, double hvalue) : length(lvalue),
                                                              breadth(bvalue),
                                                              height(hvalue)
      {
        cout << "Box constructor called" << endl;

        // Ensure positive dimensions
        if(length <= 0.0)
          length = 1.0;
        if(breadth <= 0.0)
          breadth = 1.0;
        if(height <= 0.0)
          height = 1.0;
      }

      // Function to calculate the volume of a box
      double Box::volume()
      {
        return length * breadth * height;
      }

        Larry

       

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.