Hello, this board has helped me before while I lurked around so I hope you can help me with my current project. This is my first time using Polymorphism/virtual and second time using pointers. When I execute the program, I don’t know if I’m using either perfectly to be honest. I get errors like…
1>library.obj : error LNK2005: "public: __thiscall Holding::Holding(char *,int)" (??0Holding@@QAE@PADH@Z) already defined in holding.obj
And one like…
1>C:\...\c++\Polymorphism\Polymorphism Lab\Debug\Polymorphism Lab.exe : fatal error LNK1169: one or more multiply defined symbols found
Any tips would be appreciated.
My Main():
Code:
// library.h
#ifndef _LIBRARY_H
#define _LIBRARY_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "holding.h"
//cpp's taken out, what the hell was I thinking?
#include "book.h"
Holding *holdPtr[5];
Book *myBook;
#endif
//-------------------------CPP BELOW--------------------------------
// library.cpp
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include "library.h"
ofstream csis;
int main(){
csis.open("csis.dat");
void getInfo(int);
for (int i=0;i<5;i++){
getInfo(i); }
for (int i=0;i<5;i++){
//cout holdPtr[i]->print();
holdPtr[i]->print(); }
csis.close();
}
void getInfo(int x){
char *titleTemp;
int callNumTemp;
char choice;
char *authorTemp;
cout << "Enter holdings type: " << endl;
cout << "/'b/' for book and /'r/' for recordings: ";
cin >> choice;
if (choice == 'b' || choice == 'B') {
cout << "Title: " << endl;
cin >> titleTemp;
cout << "Author: " << endl;
cin >> authorTemp;
cout << "CallNum: " << endl;
cin >> callNumTemp;
myBook = new Book(titleTemp,authorTemp,callNumTemp);
holdPtr[x] = myBook;
myBook->~Book();
} else
cout << "else called"<< endl;
}
My Abstract base class:
Code:
// holding.h
#ifndef _HOLDING_H
#define _HOLDING_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
using namespace std;
class Holding{
protected:
char *title;
int titleLength;
int *callNumber; //*
public:
Holding(char*, int);
Holding();
virtual ~Holding();
virtual void print(/*ostream &*/) = 0;
};
#endif
//-------------------------CPP BELOW--------------------------------
// holding.cpp
#include "holding.h"
using namespace std;
extern ofstream csis;
Holding::Holding(char *tempTitle, int tempCallNumber){
titleLength = strlen(tempTitle);
title = new char[titleLength + 1];
strcpy(title, tempTitle);
title[titleLength] = '\0';
*callNumber = tempCallNumber;
}
Holding::~Holding(){
delete [] title;
}
My derived class:
Code:
// Book.h
#ifndef _BOOK_H
#define _BOOK_H
#include <stdlib.h>
#include <iostream>
#include <fstream>
#include "holding.h"
extern char *author; //NEW NEW
extern int authorLength;
class Book : public Holding {
protected:
//char *author;
//int authorLength;
public:
Book(char*, char*, int);
Book(const Book &);
~Book();
virtual void print(/*ostream &*/); //ostream is req for project
// but for the time being, ignoring to fix other problem.
};
#endif
//-------------------------CPP BELOW--------------------------------
// Book.cpp
#include "book.h"
using namespace std; //new
extern ofstream csis; //new
char *author; //NEW NEW
int authorLength;
Book::Book(char *title, char *tempAuthor, int callNumtemp) : Holding(title, *callNumber) {
authorLength = strlen(tempAuthor);
author = new char[authorLength + 1];
strcpy(author, tempAuthor);
author[authorLength] = '\0';
}
Book::~Book(){
delete [] author;
}
void Book::print(/*ostream &out*/){
cout << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl;
csis << "Book: " << author << " " << '\"' << title << '\"' << " " << callNumber << endl; //uncommented
}