0

I am trying to understand inheritance in C++. I have made a base class called Area, and a derived class called RuralArea. Both classes have a .h header file and a .cpp file that defines the methods declared in the header. I am trying to link the ruralarea.cpp file with the main file, called ruraltest.cpp. In the terminal, I typed:

g++ -c ruralarea.cpp

g++ -c ruraltest.cpp

g++ -o ruraltest ruralarea.o ruraltest.o

I got the following errors:

Undefined symbols for architecture x86_64:
  "Area::set_pop()", referenced from:
      RuralArea::set_pop() in ruralarea.o
  "Area::set_size()", referenced from:
      _main in ruraltest.o
  "Area::Area()", referenced from:
      RuralArea::RuralArea() in ruralarea.o
  "Area::get_pop() const", referenced from:
      _main in ruraltest.o
  "Area::get_name() const", referenced from:
      _main in ruraltest.o
  "Area::get_size() const", referenced from:
      _main in ruraltest.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

Here is the header for my base class, area.h.

#ifndef AREA_H
#define AREA_H

#include <string>

using namespace std;

class Area
{
public:

    Area();

    void set_name();

    void set_size();

    void set_pop();

    string get_name() const;

    int get_size() const;

    int get_pop() const;

private:
    string name;
    int size;
    int pop;
};

#endif

Here is the file, area.cpp.

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

using namespace std;

Area::Area()
{
    name = "NA";
    size = 0;
    pop = 0;
}

void Area::set_name()
{
    cout << "Please enter the name of the area:" << endl;
    getline(cin, name);
}

void Area::set_size()
{
    cout << "Please enter the size of the area in square miles:" << endl;
    cin >> size;
    string remainder;
    getline(cin, remainder);
}

void Area::set_pop()
{
    cout << "Please enter the population of the area:" << endl;
    cin >> pop;
}

string Area::get_name() const
{
    return name;
}

int Area::get_size() const
{
    return size;
}

int Area::get_pop() const
{
    return pop;
}

Here is the header for my derived class, ruralarea.h.

#include <string>

using namespace std;

#include "area.h"

class RuralArea : public Area
{
public:
    RuralArea();
    void set_pop();
    int get_cows() const;
    int get_chickens() const;
private:
    int cows;
    int chickens;
};

Here is the code for the file ruralarea.cpp.

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

RuralArea::RuralArea():Area()
{
    cows = 0;
    chickens = 0;
}

void RuralArea::set_pop()
{
    Area::set_pop();
    cout << "Enter number of cows:" << endl;
    cin >> cows;
    cout << "Enter number of chickens:" << endl;
    cin >> chickens;
}

int RuralArea::get_cows() const
{
    return cows;
}

int RuralArea::get_chickens() const
{
    return chickens;
}

Finally, here is the file for my main program, ruraltest.cpp.

#include <iostream>
#include <iomanip>

using namespace std;

#include "ruralarea.h"

int main()
{
    RuralArea county;
    county.set_size();
    county.set_pop();
    cout << "Your rural area's info:" << endl;
    cout << "Name: " << county.get_name() << endl;
    cout << "Square miles: " << county.get_size() << endl;
    cout << "Population: " << county.get_pop() << endl;
    cout << "Cows: " << county.get_cows() << endl;
    cout << "Chickens: " << county.get_chickens() << endl;
}

If someone could tell me how to resolve the compiler errors, I would appreciate it.

Raddicus
  • 1,024
  • 1
  • 9
  • 9
  • 3
    Looks like you've forgotten to include area.cpp in your compilation list and area.o in your link phase. – John Sheridan Jan 03 '15 at 19:30
  • I was thinking that it had something to do with the base class not being linked to the other files properly. But I did't forget to do it. I don't know how to do it :P This is my first time trying to use a derived class and the book that I'm reading doesn't give an example of how to compile it. – Raddicus Jan 03 '15 at 19:32
  • 1
    g++ -c area.cpp to compile area.cpp AND g++ -o ruraltest area.o ruralarea.o ruraltest.o to link. – John Sheridan Jan 03 '15 at 19:34

0 Answers0