Link to home
Start Free TrialLog in
Avatar of kuntilanak
kuntilanakFlag for United States of America

asked on

can't compile code

I have my files here:

http://cid-1bd707a1bb687294.skydrive.live.com/browse.aspx/C++?uc=5&nl=1

and I can't compile them...it gives me this error:

/tmp/ccthWPyg.o: In function `Item::~Item()':
Books.cc:(.text._ZN4ItemD2Ev[Item::~Item()]+0x13): undefined reference to `vtable for Item'
/tmp/ccthWPyg.o: In function `Item::Item()':
Books.cc:(.text._ZN4ItemC2Ev[Item::Item()]+0xf): undefined reference to `vtable for Item'
/tmp/ccthWPyg.o:(.rodata._ZTV8Aquarium[vtable for Aquarium]+0x28): undefined reference to `Item::print() const'
/tmp/ccthWPyg.o:(.rodata._ZTI8Aquarium[typeinfo for Aquarium]+0x10): undefined reference to `typeinfo for Item'
/tmp/ccthWPyg.o:(.rodata._ZTI5Plant[typeinfo for Plant]+0x10): undefined reference to `typeinfo for Item'
/tmp/ccthWPyg.o:(.rodata._ZTI4Book[typeinfo for Book]+0x10): undefined reference to `typeinfo for Item'

Please help me out on what I need to do
Avatar of brijesh_iae
brijesh_iae

I didn't find the your files at the given path. From the errors mentioned in your problem description, it seems that you've virtual destructors in your code for which you've no definitions. Please provide the definitions for your virtual destructors.
The same applies to other member errors in the problem description. For nonvirtual member function also, it is unable to locate the definition of the functions.
Avatar of kuntilanak

ASKER

are you sure that you can't download the files?  well..here's my .cc file
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cassert>

#include "Books.h"

/***************************************************************Book****************************************************************/

Book::Book(const String& title, int thickness, int weight)
: itsThickness(thickness), itsWeight(weight),
  itsName(title)
{}

Book::~Book()
{
    
}

int Book::minWidth() const{
	return itsThickness;
}

int Book::maxWeight() const{
	return itsWeight;
}

String Book::descrip() const{
	String result = "\'" + itsName + "\'";
	return result;
}

void Book::print() const
{
    cout << itsName << "(" << itsThickness << " mm, " << itsWeight << " dg)" << endl;
    //printf("%s (%d mm, %d dg)\n", itsName, itsThickness, itsWeight);
}



/***************************************************************Plant****************************************************************/

Plant::Plant(const String& name, int spread, int weight)
: plantName(name), plantSpread(spread),
  plantWeight(weight)
{}

Plant::~Plant()
{
    
}

int Plant::minWidth() const{
	return plantSpread;
}

int Plant::maxWeight() const{
	return plantWeight;
}

String Plant::descrip() const{
	String result = "\'A plant named " + plantName +"\'";
	return result;
}

void Plant::print() const
{
    printf("this needs modification\n");
}

/***************************************************************FishTank****************************************************************/

FishTank::FishTank(int base1, int base2, int height)
: tankBase1(base1), tankBase2(base2),
  tankHeight(height)
{ 
  aq_width = calWidth();
  aq_weight = calWeight();
}

FishTank::~FishTank()
{
    
}

int FishTank::minWidth() const{
	return aq_width;
}

int FishTank::maxWeight() const{
	return aq_weight;
}

String FishTank::descrip() const{
	char buffer[33];
	snprintf(buffer, sizeof(buffer), "%d", aq_width);
	String width(buffer);
	String result = "\'" + width + "mm wide fish tank" +"\'";
	return result;
}

void FishTank::print() const
{
    printf("this needs modification\n");
}

int FishTank::calWidth(){
	return (tankBase1>tankBase2)? tankBase1:tankBase2;
}

int FishTank::calWeight(){
	return (int)(tankBase1 * tankBase2 * tankHeight * .0001);
}

/***************************************************************FishBowl****************************************************************/

FishBowl::FishBowl(int radius)    
:bowlRadius(radius)
{ 
  aq_width = calWidth();
  aq_weight = calWeight();
}


FishBowl::~FishBowl()
{
    
}

int FishBowl::minWidth() const{
	return aq_width;
}


int FishBowl::maxWeight() const{
	return aq_weight;
}

String FishBowl::descrip() const{
	char buffer[33];
	snprintf(buffer, sizeof(buffer), "%d", aq_width);
	String width(buffer);
	String result = "\'" + width + "mm high fish bowl" +"\'";
	return result;
}

void FishBowl::print() const
{
    printf("this needs modification\n");
}

int FishBowl::calWidth(){
	return 2*bowlRadius;
}

int FishBowl::calWeight(){
	return (4/3)*PI*bowlRadius;
}


void Item::enableDebug(bool toggle)
{
  // Do whatever
}

Bookcase::Bookcase(int id)
: itsShelfCount(0), itsId(id) {}

Bookcase::~Bookcase()
{
    for (int i = 0; i < itsShelfCount; i++)
        delete itsShelves[i];
}

bool Bookcase::addShelf(int width, int capacity)
{
    if (itsShelfCount >= MaxShelves)
        return false;
    
    itsShelves[itsShelfCount++] = new Shelf(width, capacity);
    return true;
}

bool Bookcase::add(Item *bp) const
{
    if (itsShelfCount == 0) return false;

    Shelf *target = NULL;
    int available = -1;
    for (int i = 0; i < itsShelfCount; i++) {
        Shelf *sp = itsShelves[i];
        if (sp->can_hold(bp) && sp->available() > available) {
            target = sp;
            available = target->available();
            }
        }

    if (target == NULL)
        return false;

    target->add(bp);
    return true;
}

void Bookcase::print() const
{
    printf("Bookcase #%d\n", itsId);
    for (int i = 0; i < itsShelfCount; i++)
        itsShelves[i]->print();
}

Bookcase::Shelf::Shelf(int width, int capacity)
: itsWidth(width),
  itsMaxWeight(capacity),
  itsUsedWidth(0),
  itsUsedWeight(0),
  itsItemCount(0),
  itsMaxItems(ItemChunk),
  itsItems(new Item*[ItemChunk])
{}

void Bookcase::Shelf::add(Item *bp)
{
    assert(can_hold(bp));

    if (itsItemCount == itsMaxItems)
        expand();

    itsItems[itsItemCount++] = bp;
    itsUsedWeight += bp->maxWeight();
    itsUsedWidth += bp->minWidth();
}

void Bookcase::Shelf::print() const
{
    printf("--- Shelf (%d mm, %d dg) ---\n", itsWidth, itsMaxWeight);
    for (int i = 0; i < itsItemCount; i++) {
        printf("%2d: ", i+1);
        itsItems[i]->print();
        }
}

void Bookcase::Shelf::expand()
{
    Item **newItems = new Item*[itsMaxItems += ItemChunk];
    for (int i = 0; i < itsItemCount; i++)
        newItems[i] = itsItems[i];
    delete [] itsItems;
    itsItems = newItems;
}
    
bool Bookcase::Shelf::can_hold(Item *bp) const
{
    return itsUsedWeight + bp->minWidth() <= itsMaxWeight &&
        itsUsedWidth + bp->maxWeight() <= itsWidth;
}

int Bookcase::Shelf::available() const
{
    return itsWidth - itsUsedWidth;
}

Open in new window

here's my .h, see if it helps
Try making print method a pure virtual method in Item class...!!!
here's my .h
geez, the attach thing isn't attaching
#ifndef _Books_h_
#define _Books_h_

#include <cmath>
#include <stdio.h>
#include <stdlib.h>
#include <iostream>
#include "String.h"

#define PI 3.14159265358979323846

using namespace std;

class Item{
public:
  //
  // Return the width of the Item in its thinnest acceptable orientation
  //
   virtual int minWidth() const = 0;
 
  //
  // Return the maximum weight of the Item.
  //
  virtual int maxWeight() const = 0;

  //
  // Return a description of the Item
  //
  virtual String descrip() const = 0;

  //
  // Print the description of the Item on standard output, followed by a newline.
  //
  virtual void print() const;

  //
  // Enable (or disable) debug descriptions. When enabled, the String produced
  // by descrip() includes the minimum width and maximum weight of the Item.
  // Initially, debugging is disabled.
  static void enableDebug(bool);

  //
  // Item needs a virtual destructor.
  virtual ~Item(){}
};

class Book : public Item {
    public:
        Book(const String& title, int thickness, int weight);
        ~Book();
	 int minWidth() const;
	 int maxWeight() const;
	 String descrip() const;
        void print() const;

    private:
        void operator=(Book&);
        Book(const Book&);
        int itsThickness, itsWeight;
        const String itsName;
};

class Plant : public Item{
   public:
	Plant(const String& name, int spread, int weight);
	 ~Plant();
	int minWidth() const;
	int maxWeight() const;
	String descrip() const;
       void print() const;

   private:
	int plantSpread;
	int plantWeight;
	const String plantName;
};

class Aquarium : public Item{
public:
  virtual int calWidth() = 0;   // Pure virtual function.
  virtual int calWeight() = 0;   // Pure virtual function.
  virtual ~Aquarium(){}
protected:
  int aq_width;
  int aq_weight;
};


class FishTank : public Aquarium{
    public:
       FishTank(int base1, int base2, int height);
       ~FishTank();
	int minWidth() const;
	int maxWeight() const;
	String descrip() const;
       void print() const;
	int calWidth();
	int calWeight();
    private:
        int tankBase1, tankBase2, tankHeight;
};


class FishBowl : public Aquarium{
     public:
       FishBowl(int radius);          
       ~FishBowl();
	int minWidth() const;
	int maxWeight() const;
	String descrip() const;
       void print() const;
	int calWidth();
	int calWeight();
    private:
       int bowlRadius;
};

class Bookcase {
    public:
        Bookcase(int id);
        int id() const { return itsId; }
        ~Bookcase();
        bool addShelf(int width, int capacity);
        bool add(Item *it) const;
        void print() const;

    private:
        class Shelf;
        static const int MaxShelves = 10;
        Shelf *itsShelves[MaxShelves];
        int itsShelfCount, itsId;

        void operator=(Bookcase&);
        Bookcase(const Bookcase&);
        
    class Shelf {
        public:
            Shelf(int width, int capacity);
              
            ~Shelf() { delete [] itsItems; }
    
            void add(Item *bp);
            void print() const;
    
            bool can_hold(Item *bp) const;
            
            int available() const;
            
        private:
            int itsWidth, itsMaxWeight,
            itsUsedWidth, itsUsedWeight;
            int itsItemCount, itsMaxItems;
            Item **itsItems;
            static const int ItemChunk = 100;
    
            void expand();
            void operator=(Shelf&);
            Shelf(const Shelf&);
            };
        };

#endif

Open in new window

isn't it already pure virtual?
nope.... its just virtual.....virtual void print() const;

make it

virtual void print() const = 0;
yes that totally worked... may I know why it needs to be pure virtual? and why it didn't work out before?
ASKER CERTIFIED SOLUTION
Avatar of milindsm
milindsm
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial