7

I'm working on learning OOP with C++ and having a problem. I'm sure its a memory allocation problem but cant seem to get my head around it. Any help will be greatly appreciated.

My Client Code

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

    using namespace std;

    int main(){
        Box *box = new Box;
        return 0;
    }

My Box Class...

    #include <iostream>

    using namespace std;

    class Box{

        private:
            double width;
            double height;
            double perimeter;
            double area;


        public:
            Box(){
                cout << "Box created" << endl;
            }

            ~Box(){
                cout << "Box Destroyed" << endl;
            }

            double getWidth(){
                //
                return this->width;
            }

            double getHeight(){
                //
                return this->height;
            }

            double getArea(){
                //
                return this->area;
            }

            double getPerimeter(){
                //
                return this->perimeter;
            }

            void setWidth(double w){
                //
                this->width = w;
                if(!this->height){
                    computeSetArea(this->width, this->height);
                    computeSetPerimeter(this->width, this->height);
                }
            }

            void setHeight(double h){
                //
                this->height = h;
                if(!this->width){
                    computeSetArea(this->width, this->height);
                    computeSetPerimeter(this->width, this->height);
                }
            }

        private:
            void computeSetArea(double w, double h){
                //
                this->area = w*h;
            }

            void computeSetPerimeter(double w, double h){
                //
                this->perimeter = (w * 2) + (h + 2);
            }
    };

I use gcc and execute:

    gcc Box.cpp client.cpp -o mainfile

After such I receive this error.

/tmp/ccaVb21k.o: In function `__static_initialization_and_destruction_0(int, int)':
Box.cpp:(.text+0x1d): undefined reference to `std::ios_base::Init::Init()'
Box.cpp:(.text+0x22): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccaVb21k.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o: In function `main':
client.cpp:(.text+0x14): undefined reference to `operator new(unsigned int)'
client.cpp:(.text+0x3f): undefined reference to `operator delete(void*)'
/tmp/ccjtbzi4.o: In function `__static_initialization_and_destruction_0(int, int)':
client.cpp:(.text+0x6c): undefined reference to `std::ios_base::Init::Init()'
client.cpp:(.text+0x71): undefined reference to `std::ios_base::Init::~Init()'
/tmp/ccjtbzi4.o: In function `Box::Box()':
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x11): undefined reference to `std::cout'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x16): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x1e): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::endl<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&)'
client.cpp:(.text._ZN3BoxC1Ev[Box::Box()]+0x26): undefined reference to `std::basic_ostream<char, std::char_traits<char> >::operator<<(std::basic_ostream<char, std::char_traits<char> >& (*)(std::basic_ostream<char, std::char_traits<char> >&))'
/tmp/ccjtbzi4.o:(.eh_frame+0x12): undefined reference to `__gxx_personality_v0'
/tmp/ccjtbzi4.o:(.eh_frame+0x4b): undefined reference to `__gxx_personality_v0'
collect2: ld returned 1 exit status
Alan
  • 45,915
  • 17
  • 113
  • 134
seanr
  • 195
  • 1
  • 4
  • 10
  • please take a look here: http://stackoverflow.com/questions/1696300/how-to-compile-c-under-ubuntu-linux?rq=1 – rsc Nov 15 '13 at 20:48
  • By convention .h files contain the class definition, while the .cpp files contain the class implementation, so you should either: split your definition out to a .h file, and keep the implementation in the cpp, or rename your .cpp to .h (since it's really a .h with the implementation inlined). If you *do* rename the file to .h, please remove the using namespace std; from the .h file, as that it is bad practice to pollute the global namespace inside a header file. – Alan Nov 15 '13 at 23:30

3 Answers3

18

Your code compiles fine, you're getting a linker error (ld is the linker, and it's returning 1 (error)), which is complaining about missing c++ libs.

To fix, you'll need to add the stdc++ lib to your commandline, or use g++.

Replace gcc with g++ or add -lstdc++ to your gcc command line.

gcc Box.cpp client.cpp -o mainfile -lstdc++

or

g++ Box.cpp client.cpp -o mainfile

This will link the std c++ library with your compiled code. Using g++, you can omit this step.

Alan
  • 45,915
  • 17
  • 113
  • 134
1

Setting up your class structure like this works for me:

Box.h:

class Box
{
public:
    Box();
    ~Box();

    double getWidth();
    double getHeight();
    double getArea();
    double getPerimeter();
    void setWidth(double w);
    void setHeight(double h);
    void computeSetArea(double w, double h);
    void computeSetPerimeter(double w, double h);

private:
    double width;
    double height;
    double perimeter;
    double area;
};

And then for Box.cpp:

#include "box.h"
#include <iostream>
using namespace std;

Box::Box(){
    cout << "Box created" << endl;
}

Box::~Box(){
    cout << "Box Destroyed" << endl;
}

double Box::getWidth(){
    return this->width;
}

double Box::getHeight(){
    return this->height;
}

double Box::getArea(){
    return this->area;
}

double Box::getPerimeter(){
    return this->perimeter;
}

void Box::setWidth(double w){
    this->width = w;
    if(!this->height){
        computeSetArea(this->width, this->height);
        computeSetPerimeter(this->width, this->height);
    }
}

void Box::setHeight(double h){
    this->height = h;
    if(!this->width){
        computeSetArea(this->width, this->height);
        computeSetPerimeter(this->width, this->height);
    }
}

void Box::computeSetArea(double w, double h){
    this->area = w*h;
}

void Box::computeSetPerimeter(double w, double h) {
    this->perimeter = (w * 2) + (h + 2);
}

Output:

Box created
Bruce Dean
  • 2,798
  • 2
  • 18
  • 30
  • Should i compile with the header file source file or just the header? – seanr Nov 15 '13 at 22:30
  • Just the source files, never directly compile headers. Also never include one source file in another (like your code above). – john Nov 15 '13 at 22:49
  • @seanr, Hi Sean, yes just like John says. I used: "g++ Box.cpp main.cpp -o mainfile" then to run of course I used: "./mainfile" Did this work for you? – Bruce Dean Nov 15 '13 at 22:52
  • @john, Hi John, Box.h and Box.cpp are separate files, hope that clarifies what I listed. Thanks. – Bruce Dean Nov 15 '13 at 22:55
  • Thank you very much ill try to research more on why to use headers and why not include cpp files directly inside your source files. – seanr Nov 15 '13 at 22:57
  • @roybatty I wasn't refering to your code but to the code originally posted by seanr. – john Nov 15 '13 at 23:03
  • This works because you used g++, instead of g++, not because of how you've laid out your classes. – Alan Nov 15 '13 at 23:25
1

Try to use g++ instead of gcc.

Enjal Parajuli
  • 193
  • 1
  • 11