Question
I need to debug my code a little bit: I got 2 files: Playlist.cpp and Playlist.h //playlist.cpp #include #include #include #include #include #include Playlist.h using
I need to debug my code a little bit:
I got 2 files: Playlist.cpp and Playlist.h
//playlist.cpp #include
using namespace std;
Playlist::Playlist() { Playlist::Song_Entry head_value(-1, "HEAD"); Playlist::Song_Entry prev_to_current_value(-1, "PREV_TO_CURRENT"); Playlist::Song_Entry tail_value(-1, "TAIL"); _head = new Playlist::Node(head_value); _tail = new Playlist::Node(tail_value); _prev_to_current = new Playlist::Node(prev_to_current_value); _prev_to_current = _head; _size = 0; }
Playlist::~Playlist() { clear(); delete _head; }
Playlist* Playlist::clear() { while (_head != NULL) { Playlist::Node* temp = _head; _head = _head->get_next(); delete(temp); temp = NULL; } _tail = NULL; return this; } Playlist* Playlist::advance_cursor() { if (_prev_to_current == _tail) { return NULL; } _prev_to_current = _prev_to_current->get_next(); return this; } Playlist* Playlist::circular_advance_cursor() {
if (_prev_to_current->get_next() == _tail) { _prev_to_current = _head->get_next(); } else { advance_cursor(); } return this; }
Playlist::Song_Entry& Playlist::find_by_id(int id) const { Node* temp = _head; while (temp->get_next() != NULL) { if (temp->get_next()->get_song().get_id() == id) { return temp->get_next()->get_song(); } temp = temp->get_next(); } return this->_head->get_song(); }
Playlist::Song_Entry& Playlist::find_by_name(string name) const { Node* temp = _head; while (temp->get_next() != NULL) { if (temp->get_next()->get_song().get_name() == name) { return temp->get_next()->get_song(); } temp = temp->get_next(); } return this->_head->get_song(); } string Playlist::to_string() const { Node* temp = new Node(); temp = _head; string str = ""; stringstream ss; ss << get_size(); string size; ss >> size; int count = 0; str = "Playlist: " + size + "entries. "; while (temp->get_next() != NULL) { stringstream ss; ss << temp->get_next()->get_song().get_id(); string id; ss >> id; str += "{ id: " + id + ", name: " + temp->get_next()->get_song().get_name() + " }"; if (temp->get_next() == _prev_to_current) { str += " [P]"; } if (temp->get_next() == _tail) { str += " [T]"; } if (count == 25) { str += "... ";
break; } str += " "; temp = temp->get_next(); count++; } return str; }
//end of playliist.cpp
//playlist.h #ifndef Playlist_h #define Playlist_h #include
using namespace std;
class Playlist {
public: class Song_Entry {
private: int _id; string _name; public: Song_Entry(int id = 0, string name = "Unnamed")
: _id(id), _name(name) {}
int get_id() const { return _id; } string get_name() const { return _name; } bool set_id(int id); bool set_name(string name); bool operator==(const Song_Entry& that) { return this->_id == that._id && this->_name == that._name; } bool operator!=(const Song_Entry& that) { return !(*this == that); } friend std::ostream& operator<<(ostream& os, const Song_Entry& s) { return os << "{ id: " << s.get_id() << ", name: " << s.get_name() << " }"; } friend class Tests; // Don't remove this line }; private: // This is going to be our inner private class. The client doesn't need to // know class Node { // TODO - your code here private: Song_Entry _song; Node* _next; public: Node(const Song_Entry& song = Song_Entry()) : _song(song), _next(NULL) {} ~Node() {} // Do not do recursive free Song_Entry& get_song() { return _song; } Node* get_next() { return _next; } Node* insert_next(Node* p); Node* remove_next(); friend std::ostream& operator<<(ostream& os, Playlist::Node* s) {
return os << "{ song: " << s->get_song() << ", next: " << s->get_next() << " }";
} friend class Tests; // Don't remove this line }; private: Node* _head, * _tail, * _prev_to_current; size_t _size; public: Playlist(); ~Playlist(); size_t get_size() const { return _size; } Song_Entry& get_current_song() const; Playlist* clear(); Playlist* rewind(); Playlist* push_back(const Song_Entry& s); Playlist* push_front(const Song_Entry& s); Playlist* insert_at_cursor(const Song_Entry& s); Playlist* remove_at_cursor(); Playlist* advance_cursor(); Playlist* circular_advance_cursor(); // The following return the target payload (or sentinel) reference on success Song_Entry& find_by_id(int id) const; Song_Entry& find_by_name(string songName) const; string to_string() const; friend class Tests; };
#endif /* Playlist_h */
I got these errors while "Testing":
/tmp/cc1Gq6Ps.o: In function `Tests::fill_with_n_songs(Playlist*, Ref::Playlist*, unsigned long)': Tests.cpp:(.text+0x70f): undefined reference to `Playlist::push_back(Playlist::Song_Entry const&)' /tmp/cc1Gq6Ps.o: In function `Tests::test_constructors(std::ostream&)': Tests.cpp:(.text+0x955): undefined reference to `Playlist::Song_Entry::set_id(int)' Tests.cpp:(.text+0x9cd): undefined reference to `Playlist::Song_Entry::set_name(std::__cxx11::basic_string, std::allocator >)' /tmp/cc1Gq6Ps.o: In function `Tests::test_node_ops(std::ostream&)': Tests.cpp:(.text+0xff5): undefined reference to `Playlist::Node::insert_next(Playlist::Node*)' Tests.cpp:(.text+0x114c): undefined reference to `Playlist::Node::remove_next()' /tmp/cc1Gq6Ps.o: In function `Tests::test_insert_at_cursor(std::ostream&)': Tests.cpp:(.text+0x15a0): undefined reference to `Playlist::Song_Entry::set_id(int)'
Could you help with this?
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started