0

I was practising some c++ before my test and I came across this task which says:

a-) Design class RankingList with three type parameters name, evaluation, id defining 3 features of each element of a ranking list. The elements should be sorted according to the value of evaluation.

b-) Write implementation of a method inserting element to a ranking list. An element with not unique id should not be inserted.

c-) Write implementation of operator<< for class template RankingList

So what I have currently done is the following:

#include <iostream>
#include <string.h>

using namespace std;



template<typename T1, typename T2, typename T3>
class RankingList
{

private:

    struct Rank
    {
        T1 name;
        T2 evaluation;
        T3 id;
        Rank *next;
    } rank;


    Rank *head;
    Rank *tail;



public:

    void setName(T1 newName)
    {
        rank.name = newName;
    }

    const T1 getName()
    {
        return rank.name;
    }


    void setEvaluation(T2 newEvaluation)
    {
        rank.evaluation = newEvaluation;
    }

    const T2 getEvaluation()
    {
        return rank.evaluation;
    }



    void setId(T3 newId)
    {
        rank.id = newId;
    }

    const T3 getId()
    {
        return rank.id;
    }




    RankingList();
    RankingList(const &other);
    ~RankingList(){};


    friend ostream &operator << (ostream &os, const RankingList<T1, T2, T3> &toPrint);



};

template<typename T1, typename T2, typename T3>
ostream &operator << (ostream &os, const RankingList<T1, T2, T3> & toPrint)
{
    cout << "12212" << endl;
    typename RankingList<T1, T2, T3>::Rank *temp;
    temp = toPrint.head;

    while(temp)
    {
        os << "Name: " << temp->name << ", evaluation " << temp->evaluation << ", id " << temp->id << endl;
        temp = temp->next;
    }
    return os;
}



int main()
{

    RankingList<int, int, int> list23();
    cout << list23 << endl;

    return 0;
}

So I have written a template class with the 3 types, used structure with the parameters required and added a node pointing to the next. Also, reference for the head and tail of ranks. I then added some getters and setters for the variables I got and implemented, declared few methods and implemented the operator<<;

The issue is that calling operator << wouldn't do anything at all just prints out 1 and not even sure if what I have any issues with the current code and how to implement task 2 properly.

Mr Pro Pop
  • 666
  • 5
  • 19
  • I'm confused. The class has a member called `rank` which is s struct. Your getters and setters deal directly with it. But NOTHING in the code you have shows sets the head or tail so they point nowhere and because of the rank variable you can only have one item in your list anyway. Even after I fix the errors that keep the code from compiling, unless there is more code I can't believe it doesn't crash when you run it. – Jerry Jeremiah Jan 28 '20 at 02:39
  • 2
    So it runs because you aren't actually creating a variable of RankingList type. The reason it prints 1 is that `RankingList list23();` is the declaration of a function with no definition. If you change it to `RankingList list23;` then it declares a variable and doesn't compile anymore because of the missing contructors. – Jerry Jeremiah Jan 28 '20 at 02:49
  • Where is ctor implementation? – 2785528 Jan 28 '20 at 02:51
  • 2
    @2785528 He doesn't have them - but that's not the problem because the code compiles and runs without them because in the code he doesn't actually create a variable of that class type. – Jerry Jeremiah Jan 28 '20 at 02:52
  • Possible duplicate of https://stackoverflow.com/questions/57146468/unclear-most-vexing-parse - it even explains why it prints "1". And it is probably a duplicate of this one too: https://stackoverflow.com/questions/17060725/most-vexing-parse-confusion – Jerry Jeremiah Jan 28 '20 at 02:53
  • Does this answer your question? [Unclear most vexing parse](https://stackoverflow.com/questions/57146468/unclear-most-vexing-parse) – Jerry Jeremiah Jan 28 '20 at 02:58
  • @JerryJeremiah those links unfortunately don't answer my questions, I really don't get it. Can you help me out with a runnable example for my code so I can learn it properly please? Thanks so much!! – Mr Pro Pop Jan 28 '20 at 03:02
  • Very confusing. There's nothing in the shown code that creates a list of anything. The shown code declares a template with a head & tail pointers, that do not appear to be used for anything. The template also creates a separate instance of the value class, for some reason, and defines some methods that operate on this separate instance of the value class. Very confusing. Then, the `<<` overload in question then suddenly decides that there is, indeed, a linked list in the container, despite the lack of any evidence that one was ever created, and attempts to print it. No wonder that fails. – Sam Varshavchik Jan 28 '20 at 03:02
  • Can you please help me fix the issues? @SamVarshavchik – Mr Pro Pop Jan 28 '20 at 03:10
  • These are not simple fixes. Major parts of the described functionality are completely missing, and must be implemented. Unfortunately, stackoverflow.com is just a basic Q/A site, but the help you're looking for [can only be found in a good C++ book](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list), and stackoverflow is not really a replacement for a good C++ book. I estimate there's at least a good 2-3 hours worth of coding that need to be done here, and you can't really expect someone to do this for you. – Sam Varshavchik Jan 28 '20 at 03:20

0 Answers0