How to arrange header files when dealing with inheritance in c++

Thread Starter

asilvester635

Joined Jan 26, 2017
73
I have a base class Animal and a subclass Dog. I keep getting this error message from the terminal shown below.

Error message from terminal:
Undefined symbols for architecture x86_64:

"_main", referenced from:

implicit entry/start for main executable

ld: symbol(s) not found for architecture x86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)



Here are the header files included in both my base and subclass. My hunch tells me that there is something wrong with what type of header files I included in both my base and subclass. Are these right?
// base class
Animal.cc: #include "Animal.h",
Animal.h: nothing here

// subclass
Dog.cc:
#include "Animal.h", #include "Dog.h"
Dog.h: #include "Animal.h"

Also, I'll include my code for those who are curious

Animal.cpp
#include <cstdio>
#include <string>
#include "Animal.h"
using namespace std;

// define the virutal destructor because it is public
Animal::~Animal() throw() {}

Animal::Animal() throw() {
_name = "Madelyn";
_type = "Love";
_sound = "Thump";
}

Animal::Animal(const string& n, const string& t, const string& s) throw() {
_name = n;
_type = t;
_sound = s;
}

// define public functions of Animal
void Animal::speak() const throw() {
printf("%s the %s says %s", name().c_str(), type().c_str(), sound().c_str());
} // end of speak function
const string& Animal::name() const throw() {
return _name;
}// end of name function
const string& Animal::type() const throw() {
return _type;
} // end of type function
const string& Animal::sound() const throw() {
return _sound;
} // end of sound function


Animal.h

#ifndef ANIMAL_H_
#define ANIMAL_H_
#include <string>
using namespace std;

// Base class
class Animal {
private:
string _name;
string _type;
string _sound;

public:
Animal() throw();
Animal(const string& n, const string& t, const string& s) throw();
virtual ~Animal() throw();
const string& name() const throw();
const string& type() const throw();
const string& sound() const throw();
void speak() const throw();

}; // end of base class Animal

#endif


Dog.cpp
#include <cstdio>
#include <string>
#include "Animal.h"
#include "Dog.h"
using namespace std;

Dog:: Dog(const string& n) throw() : Animal(n, "dog", "woof") {
// nothing here
} // end of constructor

Dog:: Dog() throw() {
// nothing here
}

Dog::~Dog() throw() {

}

int Dog::walk() throw() {
return ++walked;
} // end of walk


Dog.h
#ifndef DOG_H_
#define DOG_H_
#include <string>
#include "Animal.h"
using namespace std;

// Dog class - derived from Animal
class Dog : public Animal {
int walked;

public:
Dog() throw();
virtual ~Dog() throw();
Dog(const string& n) throw();
int walk() throw();
};

#endif
 
Last edited:

MrSoftware

Joined Oct 29, 2013
2,188
Where is your main() and what does it look like? You are receiving a linker error, not a compilation error. Every C/C++ program needs an entry point, typically it's called main(). Actually by the spec I think it has to be named main for programs that will run on an OS, but can possibly be another name for programs that run on embedded platforms, etc.. In any event, your linker isn't finding your main() so you need to add one somewhere.
 

malikha

Joined May 4, 2017
1
H! iam new in this site please ineed help irun this question i get error idont no why? , the question is this ;
a. Create a base class named Rectangle that includes data members for the length and
width of a Rectangle, as well as functions to assign and display those values. Derive
a class named Block that contains an additional data member to store height, and
contains functions to assign and display the height. Write a main()function that
demonstrates the classes by instantiating and displaying the values for both a Rectangle
and a Block. Save the file as RectangleAndBlock.cpp.
THE CODE ARE;
#ifndef RECTA_H
#define RECTA_H

class Recta {
public:
void setl(int);
void setw(int);
int getl();
int getw();
//void disp();
private:
int l;
int w;
};

#endif /* RECTA_H */
.............................................................................................................................................................................................................................................................................
#include "Recta.h"


void Recta::setl(int le) {
l = le;
}
void Recta::setw(int we) {
w = we;
}




int Recta::getl() {
return l ;
}

int Recta::getw() {
return w ;
}
.............................................................................................................................................................................................................................................................................
#ifndef BLOCK_H
#define BLOCK_H

#include "Recta.h"

class Block:public Recta
{
public:
// Block();
void seth(int);
int geth();
private:
int h;
};

#endif /* BLOCK_H */

..............................................................................................................................................................................................................................................................................
#include "Block.h"

//Block::Block() {
// this->h = 40;
//

void Block::seth(int he) {
h =he ;
}
int Block::geth()
{return h;
}
..............................................................................................................................................................................................................................................................................
#include<iostream>


#include "Recta.h"
#include "Block.h"

using namespace std;

int main() {
Block ma(567);

cout<<ma.geth();


Recta re(34);
cout<<re.getw()<<endl;



return 0;
}
............................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................
.
 

spinnaker

Joined Oct 29, 2009
7,830
You are getting a linker error not a compile error. Are you sure you have included all of your dependent source code in your project?
 

spinnaker

Joined Oct 29, 2009
7,830
Block ma(567);


Block does not appear to have a constructor that fits this pattern.

i.e. You don't have a constructor with an integer parameter in class Block.
 
Top