0

I have an existing program that takes in a input with 5 parameters. The inputs are a video title, url, comment, length, and rating. All contained in video.cpp and video.h. My new task is to insert each video into a linked list in sorted order, according to the video title.

My problem is that I have no idea how to access class Video, and it's parameters, from my void Vlist::Insert() function...? Here is what I have so far...

vlist.cpp:

#include <iostream>
#include <algorithm>
using namespace std;

#include "vlist.h"
#include "video.h"


void Vlist::Insert(Video)
  {

    if (m_head == NULL || m_head->m_video > video) {
      // Insert before head...
      m_head = new Node(video, m_head);
    } else {
      // Insert after existing node...
      Node *node = m_head;
      while (node->m_video != NULL && node->m_next->m_video < video) {
        node = node->m_next;
      }
      node->m_next = new Node(video, node->m_next);
    }

 }

vlist.h:

#ifndef VLIST_H
#define VLIST_H

using namespace std;

class Vlist {
 public:
 Vlist() {m_head = nullptr; }
 void Insert(Video);
 private:
 class Node {
        public:
                Node(Video *video, Node *next) {m_video = video; m_next = next; }
                Video *m_video;
                Node *m_next;
            };
            Node *m_head;
 };




#endif

video.h:

#ifndef VIDEO_H
#define VIDEO_H

using namespace std;

class Video {

  public:
    Video(string video_title, string video_link, string video_comment, double video_length, int video_number);
    void print();
    bool Title(Video *spare);




  private:


    string name;
    string web;
    string note;
    double duration;
    int score;

    string title;
   
};


#endif

video.cpp:

#include <iostream>
#include <algorithm>
using namespace std;

#include "video.h"

Video::Video(string video_title, string video_link, string video_comment, double video_length, int video_number)
  : title(video_title), link(video_link), comment(video_comment), length(video_length), rating(video_number)
{
  name = title;
}


bool Video::Title(Video *spare)
{
  return name > spare-> name;

}
void Video::print(){

  string hierarchy;
  switch(rating){

    case 1:
      hierarchy = "*";
      break;
    case 2:
      hierarchy = "**";
      break;
    case 3:
      hierarchy = "***";
      break;
    case 4:
      hierarchy = "****";
      break;
    case 5:
      hierarchy = "*****";
      break;

  }



  cout << title << ", " << link << ", " << comment << ", " << length << ", " << hierarchy << endl;

}

I have tried void Vlist::Insert(Video) , void Vlist::Insert(Video *video), void Vlist::Insert(m_video) but honestly I have no idea what I am doing. This is all very new to me. Any help would be appreciated.

Note: I realize I have not completed the linked list, I am merely trying to work in parts.

Fooji
  • 71
  • 7
  • If i've understood you correctly why don't you either make the fields `public` in the `Video` class or add `public` getters for those fields in the `Video` class. – WBuck Oct 01 '20 at 01:35
  • Please read [Why is “using namespace std;” considered bad practice?](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) and if you still decide to do it, at least _never_ do it in a header file. – paddy Oct 01 '20 at 01:52

1 Answers1

1

The members of Video are private. You cannot access them unless the class exposes public accessor functions.

class Video
{
public:
    const std::string& GetTitle() const { return title; }

    // ...

private:
    std::string title;
};

Then in your list:

void Vlist::Insert(Video* video)
{
    if (m_head == NULL || m_head->m_video->GetTitle() > video->GetTitle())
    {
        // Insert before head...
        m_head = new Node(video, m_head);
    }
    else
    {
        // Insert after existing node...
        Node *node = m_head;
        while (node->m_next != NULL &&
               node->m_next->m_video->GetTitle() <= video->GetTitle())
        {
            node = node->m_next;
        }
        node->m_next = new Node(video, node->m_next);
    }    
 }
paddy
  • 60,864
  • 6
  • 61
  • 103
  • do I have to do anything in`vlist.h` at all? because It keeps saying **Video has not been declared** – Fooji Oct 01 '20 at 01:47
  • 1
    Yes, obviously the code doesn't know anything about `Video` unless there is a declaration. You can try `#include "video.h"` near the top of `vlist.h`. If that doesn't solve it, you may have a circular dependency requiring a forward declaration instead: `class Video;` -- this is actually the preferred approach in general. Provided the only usage of `Video` inside `vlist.h` is through pointers or references, you should use forward declaration as a matter of preference. – paddy Oct 01 '20 at 01:50
  • Oh okay. The forward declaration approach worked! Thank you so much! Really appreciate it! – Fooji Oct 01 '20 at 01:56