Hi,

I have a problem when trying to compile a program in C++ on linux using g++.

smurfVillage.h

#ifndef smurfVillage_H
#define smurfVillage_H

class Creature
{
  private:
    char gender;
    int age;
    char species;

  public:
    Creature();             //parameterless default constructor
    Creature(char,int,char);
    ~Creature();
    char getGender();
    int getAge();
    char getSpecies();
    char setGender(char);
    int setAge(int);
    char setSpecies(char);
};

 #endif

smurfVillage.cpp

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

//**************************Creature Constructors **************************//
Creature::Creature()
{
  //default parameterless constructor
  gender = 'F'; 
  age = 25;
  species = 'P';
}

Creature::Creature(char G, int A, char S)
{
  //Constructor for specified values
  gender = G;
  age = A;
  species = S;
}
//***********************End of Creature Constructors***********************//

//**************************Creature get functions**************************//
char Creature::getGender()
{
  return gender;
}

int Creature::getAge()
{
  return age;
}

char Creature::getSpecies()
{
  return species;
}
//**********************End of Creature get functions **********************//

//**************************Creature set functions**************************//
char Creature::setGender(char G)
{
  gender = G;
}

int Creature::setAge(int A)
{
  age = A;
}

char Creature::setSpecies(char S)
{
  species = S;
}
//**********************End of Creature set functions **********************//

main1.cpp

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

using namespace std;

int main ()
{
  Creature *C1 = new Creature;
  Creature *C2 = new Creature('M',22,'P');

  std::cout << C1->getGender();
  std::cout << "\n\n";

  C2->setAge(28);
  std::cout << C2->getAge();
  std::cout << "\n\n";

  delete C1;
  delete C2;

  return 0;
}

When I attempt to compile this with the following line:
g++ main1.cpp smurfVillage.cpp -o prog1

I get the following error message

main1.cpp: (.text+0x113): underfined reference to 'Creature::~Creature()'
main1.cpp: (.text+0x12b): underfined reference to 'Creature::~Creature()'
collect2 ld returned 1 exit status

Can someone please explain what the error is and how I can fix it?

Thanks,

Ben

the compiler will call the destructor when appropriate. You (almost) never have to do it. The compiler will provide a default constructor for a user defined type. However, if you declare a destructor for the user defined type, as you do on line 14 of smurfvillage.h, then you should define it as well. Since you don't use dynamic memory in Creature you could add empty curly braces behind the declaration or define it the same way in smurfvillage.cpp, or you could drop line 14 in the header file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.