Advertisement
Guest User

Linker trouble

a guest
Apr 22nd, 2014
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. //box.h
  2. #ifndef BOX_H
  3. #define BOX_H
  4.  
  5. template <typename DataType>
  6. struct box
  7. {
  8.     DataType data;
  9.     box<DataType> *next;
  10.     box(DataType, box<DataType>*);
  11. };
  12.  
  13. #endif
  14.  
  15. //box.cpp
  16. #include "box.h"
  17.  
  18. template <typename DataType>
  19. box<DataType>::box(DataType d, box<DataType> *n)
  20. {
  21.     data = d;
  22.     next = n;
  23. }
  24.  
  25. //LList.h
  26. #ifndef LLIST_H
  27. #define LLIST_H
  28.  
  29. #include "box.h"
  30.  
  31. template <typename DataType>
  32. class LList
  33. {
  34. public:
  35.     box<DataType> *first;
  36.     LList ();
  37.     LList (const LList&);
  38.     ~LList();
  39.     void push (DataType);
  40.     void pop ();
  41.     void removeAll();
  42.     void copy (const LList<DataType>&);
  43.     void insertAt (DataType, int);
  44.     void removeAt (int);
  45.     box<DataType> *findElementAt (int);
  46.     LList<DataType>& operator = (const LList<DataType>&);
  47.     DataType& operator[] (int);
  48.     bool operator == (LList<DataType>);
  49. };
  50.  
  51. #endif
  52.  
  53. //LList.cpp
  54. #include <assert.h>
  55. #include <cstring>
  56. #include "box.h"
  57. #include "LList.h"
  58.  
  59. template <typename DataType>
  60. void LList<DataType>::removeAll ()
  61. {
  62.     while (first != NULL)
  63.         pop();
  64. }
  65.  
  66. template <typename DataType>
  67. LList<DataType>::~LList()
  68. {
  69.     removeAll ();
  70. }
  71.  
  72. template <typename DataType>
  73. LList<DataType>& LList<DataType>::operator = (const LList<DataType> &l)
  74. {
  75.    
  76.     if (&l != this)
  77.     {
  78.         this->removeAll();
  79.         this->copy (l);
  80.     }
  81.     return *this;
  82. }
  83.  
  84. template <typename DataType>
  85. void LList<DataType>::copy (const LList<DataType> &l)
  86. {
  87.    
  88.     box<DataType> *otherCrr = l.first;
  89.  
  90.     int i =0;
  91.  
  92.     while (otherCrr != NULL)
  93.     {
  94.         this->insertAt (otherCrr->data,i);
  95.         otherCrr = otherCrr->next;
  96.         i++;
  97.     }  
  98. }
  99.  
  100. template <typename DataType>
  101. LList<DataType>::LList (const LList &l)
  102. {
  103.     this->first = NULL;
  104.  
  105.     copy (l);
  106.    
  107.  
  108. }
  109.  
  110. template <typename DataType>
  111. LList<DataType>::LList ():first(NULL) {}//{first = NULL;}
  112.  
  113. template <typename DataType>
  114. void LList<DataType>::push (DataType newData)
  115. {
  116.  
  117.     box<DataType> *newBox = new box<DataType> (newData,NULL);
  118.  
  119.     newBox->next = first;
  120.  
  121.     first = newBox;
  122. }
  123.  
  124. template <typename DataType>
  125. void LList<DataType>::insertAt (DataType newData, int ix)
  126. {
  127.    
  128.     if (ix == 0)
  129.     {
  130.         push (newData);
  131.         return;
  132.     }
  133.  
  134.     box<DataType> *crr = findElementAt (ix-1);
  135.  
  136.     assert (crr != NULL);
  137.  
  138.     box<DataType> *newBox = new box<DataType> (newData,crr->next);
  139.     crr->next = newBox;    
  140.  
  141. }
  142.  
  143. template <typename DataType>
  144. box<DataType>* LList<DataType>::findElementAt (int ix)
  145. {
  146.     box<DataType> *crr = first;
  147.     for (int i = 0; i < ix && crr != NULL; i++)
  148.         crr = crr->next;
  149.  
  150.     return crr;
  151. }
  152.  
  153. template <typename DataType>
  154. void LList<DataType>::pop ()
  155. {
  156.  
  157.     assert (first != NULL);
  158.  
  159.     box<DataType> *save = first;
  160.     first = first->next;
  161.     delete save;
  162. }
  163.  
  164. template <typename DataType>
  165. void LList<DataType>::removeAt ( int ix)
  166. {
  167.     if (ix == 0)
  168.     {
  169.         pop();
  170.         return;
  171.     }
  172.  
  173.     box<DataType> *crr = findElementAt (ix-1);
  174.  
  175.     assert (crr != NULL);
  176.     assert (crr->next != NULL);
  177.  
  178.     box<DataType> *save = crr->next;
  179.  
  180.     crr->next = crr->next->next;
  181.     delete save;
  182.  
  183. }
  184.  
  185. template <typename DataType>
  186. DataType& LList<DataType>::operator[] (int index)
  187. {
  188.     box<DataType> *crr = first;
  189.  
  190.     int count = 0;
  191.  
  192.     while (crr != NULL && count != index)
  193.     {
  194.         crr = crr->next;
  195.         count++;
  196.     }
  197.  
  198.     assert (crr != NULL);
  199.  
  200.     return crr->data;
  201.  
  202. }
  203.  
  204. template <typename DataType>
  205. bool LList<DataType>::operator == (LList<DataType> other)
  206. {
  207.     box<DataType> *crr1 = first;
  208.     box<DataType> *crr2 = other.first;
  209.  
  210.     while (crr1 != NULL && crr2 != NULL)
  211.     {
  212.         if (crr1->data != crr2->data)
  213.             return false;
  214.  
  215.         crr1 = crr1->next;
  216.         crr2 = crr2->next;
  217.     }
  218.  
  219.     return crr1 == NULL && crr2 == NULL;
  220. }
  221.  
  222. //Song.h
  223. #ifndef SONG_H
  224. #define SONG_H
  225.  
  226. class Song
  227. {
  228. private:
  229.     char* name;
  230.     char* artist;
  231.     int yearPublished;
  232.     int length;
  233.     Song* original;
  234. public:
  235.     Song();
  236.     Song(const Song&);
  237.     Song(char*, char*, int, int, Song*);
  238.     ~Song();
  239.     Song& operator=(const Song&);
  240.     void setName(const char*);
  241.     char* getName() const;
  242.     void setArtist(const char*);
  243.     char* getArtist() const;
  244.     void setYearPublished(const int);
  245.     int getYearPublished() const;
  246.     void setLength(const int);
  247.     int getLength() const;
  248.     void setOriginal(Song*);
  249.     Song* getOriginal() const;
  250.     void print() const;
  251. };
  252.  
  253. #endif
  254.  
  255. //Song.cpp
  256. #include <iostream>
  257. #include <cstring>
  258. #include "Song.h"
  259.  
  260. Song::Song()
  261. {
  262.     name = new char[100];
  263.     artist = new char[100];
  264.     original = NULL;
  265. }
  266.  
  267. Song::Song(const Song& other)
  268. {
  269.     name = new char[100];
  270.     artist = new char[100];
  271.     *this = other;
  272. }
  273.  
  274. Song::Song(char* _name, char* _artist, int _yearPublished, int _length, Song* _original)
  275. {
  276.     name = new char[100];
  277.     artist = new char[100];
  278.     setName(_name);
  279.     setArtist(_artist);
  280.     setYearPublished(_yearPublished);
  281.     setLength(_length);
  282.     setOriginal(_original);
  283. }
  284.  
  285. Song::~Song()
  286. {
  287.     delete[] name;
  288.     delete[] artist;
  289.     delete original;
  290. }
  291.  
  292. Song& Song::operator=(const Song &other)
  293. {
  294.     setName(other.getName());
  295.     setArtist(other.getArtist());
  296.     setYearPublished(other.getYearPublished());
  297.     setLength(other.getLength());
  298.     setOriginal(other.getOriginal());
  299.     return *this;
  300. }
  301.  
  302. void Song::setName(const char* _name)
  303. {
  304.     delete[] name;
  305.     name = new char[strlen(_name)+1];
  306.     strcpy(name, _name);
  307. }
  308.  
  309. char* Song::getName() const
  310. {
  311.     return name;
  312. }
  313.  
  314. void Song::setArtist(const char* _artist)
  315. {
  316.     delete[] artist;
  317.     artist = new char[strlen(_artist)+1];
  318.     strcpy(artist, _artist);
  319. }
  320.  
  321. char* Song::getArtist() const
  322. {
  323.     return artist;
  324. }
  325.  
  326. void Song::setYearPublished(const int _yearPublished)
  327. {
  328.     yearPublished = _yearPublished;
  329. }
  330.  
  331. int Song::getYearPublished() const
  332. {
  333.     return yearPublished;
  334. }
  335.  
  336. void Song::setLength(const int _length)
  337. {
  338.     length = _length;
  339. }
  340.  
  341. int Song::getLength() const
  342. {
  343.     return length;
  344. }
  345.  
  346. void Song::setOriginal(Song* _original)
  347. {
  348.     original = _original;
  349. }
  350.  
  351. Song* Song::getOriginal() const
  352. {
  353.     return original;
  354. }
  355.  
  356. void Song::print() const
  357. {
  358.     std::cout<<"Name: "<<name<<"\nArtist: "<<artist<<"\nYear Published: "<<yearPublished<<"\nLength: "<<length<<"\n\n";
  359. }
  360.  
  361. //Album.h
  362. #ifndef ALBUM_H
  363. #define ALBUM_H
  364.  
  365. #include "LList.h"
  366.  
  367. class Album
  368. {
  369. private:
  370.     LList<Song> songs;
  371.     char* name;
  372. public:
  373.     Album();
  374.     Album(const Album&);
  375.     ~Album();
  376.     Album& operator=(const Album&);
  377.     void setSongs(LList<Song>);
  378.     LList<Song> getSongs() const;
  379.     void setName(char*);
  380.     char* getName() const;
  381.     void addSong(Song);
  382.     void print();
  383.     int getLength();
  384.     char* getArtist();
  385.     Song* findSong(char*);
  386.     //void deleteDuplicates();
  387.     //void detectCovers();
  388. };
  389.  
  390. #endif
  391.  
  392. //Album.cpp
  393. #include <iostream>
  394. #include <cstring>
  395. #include "Song.h"
  396. #include "Album.h"
  397. #include "LList.h"
  398.  
  399. Album::Album()
  400. {
  401.     name = new char[100];
  402. }
  403.  
  404. Album::Album(const Album& other)
  405. {
  406.     songs = other.getSongs();
  407.     name = new char[strlen(other.getName())+1];
  408.     strcpy(name, other.getName());
  409. }
  410.  
  411. Album::~Album()
  412. {
  413.     delete[] name;
  414. }
  415.  
  416. Album& Album::operator=(const Album& other)
  417. {
  418.     setName(other.getName());
  419.     songs = other.getSongs();
  420.     return *this;
  421. }
  422.  
  423. void Album::setName(char* _name)
  424. {
  425.     delete[] name;
  426.     name = new char[strlen(_name)+1];
  427.     strcpy(name, _name);
  428. }
  429.  
  430. char* Album::getName() const
  431. {
  432.     return name;
  433. }
  434.  
  435. void Album::setSongs(LList<Song> _songs)
  436. {
  437.     songs = _songs;
  438. }
  439.  
  440. LList<Song> Album::getSongs() const
  441. {
  442.     return songs;
  443. }
  444.  
  445. void Album::addSong(Song song)
  446. {
  447.     songs.push(song);
  448. }
  449.  
  450. void Album::print()
  451. {
  452.     std::cout<<"Name: "<<name<<"\nArtist: "<<getArtist()<<"\nSongs:\n\n";
  453.     for(box<Song>* i = songs.first; i!=NULL; i=i->next)
  454.     {
  455.         i->data.print();
  456.     }
  457.     std::cout<<"Album length: "<<getLength()<<'\n';
  458. }
  459.  
  460. int Album::getLength()
  461. {
  462.     int length = 0;
  463.     for(box<Song>* i = songs.first; i!=NULL; i=i->next)
  464.     {
  465.         length+=i->data.getLength();
  466.     }
  467.     return length;
  468. }
  469.  
  470. char* Album::getArtist()
  471. {
  472.     char* artist = songs.first->data.getArtist();
  473.     for(box<Song>* i = songs.first; i!=NULL; i=i->next)
  474.     {
  475.         if(strcmp(artist, i->data.getArtist()) != 0) return "Various artists";
  476.     }
  477.     return artist;
  478. }
  479.  
  480. Song* Album::findSong(char* songName)
  481. {
  482.     for(box<Song>* i = songs.first; i!=NULL; i=i->next)
  483.     {
  484.         if(strcmp(songName, i->data.getName()) == 0) return &i->data;
  485.     }
  486.     return NULL;
  487. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement