0

I am new to c++ ,and i was trying to implement a basic rectangle class that takes width and height as a parameter and prints out the area on x code, now when i try to run it , build fails and i get the following error message:

Undefined symbols for architecture x86_64:
  "Rectangle::Rectangle()", referenced from:
      _main in main.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I looked at similar answers and i can't make it work for me. Any idea? I appreciate your help.

// here is Rectangle.hpp

#ifndef Rectangle_hpp
#define Rectangle_hpp
#include <stdio.h>

class Rectangle {

    public :
    int width,height;
    Rectangle();
    Rectangle (int a,int b);
    int area();
};


#endif /* Rectangle_hpp */

// and here is the Rectangle.cpp

#include "Rectangle.hpp"
#include <iostream>
using namespace std;

Rectangle::Rectangle(int a, int b)

{
    width = a;
    height = b;
}

int Rectangle::area()

{
    return (width*height);
}

// and here is main.cpp

#include "Rectangle.hpp"
#include <iostream>
using namespace std;

int main() {
    Rectangle rect(3,4);
    Rectangle rectb;
    cout << "rect area: " << rect.area() << endl;
    cout << "rectb area: " << rectb.area() << endl;
    return 0;
}
sepp2k
  • 363,768
  • 54
  • 674
  • 675
belayb
  • 43
  • 5
  • 1
    From your header file, (1) Remove `Rectangle();` (2) Replace `Rectangle (int a,int b);` in your header file with `Rectangle (int a = 0,int b = 0);` – Happy Green Kid Naps Sep 19 '16 at 02:02
  • Are you using a Makefile or are you trying to compile that directly from `gcc/g++` commands? Because the later will require you to first assemble `Rectangle.cpp` (usually as `Rectangle.o`) and then specify it as input when compiling `main.cpp` so that the linker can solve external references. – Havenard Sep 19 '16 at 02:10
  • Should go as `gcc -c Rectangle.cpp -o Rectangle.o; gcc Rectangle.o main.cpp -o runme` – Havenard Sep 19 '16 at 02:13

0 Answers0