Salman Ali
Salman Ali

Reputation: 255

Operator Overloading solution

i have made a c++ code. An MList that holds items in it. I overloaded the << operator to print the values in MList in a particular format. Here is the code:

friend ostream& operator<<(ostream &out, const MSet<V> &m)
{
    string s = "";
    s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl;
    s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl;
    for (int i = 0; i < m.size_; i++)
    {
        if (i < m.size_ - 1)
            s += m.ary[i].element + ",";//out << m.ary[i].element << ",";
        else
            s += m.ary[i].element;
    }
    //cout << "String : " << s;
    return out << s;
}

But it does not print correct value. It prints the size and capacity right but not the values. Instead of values, it prints some signs like heart:

Here is the output.

You can see it prints size and capacity right but not the values. Here is the relevant code. I am executing case 2 only right now:

#include<iostream>
using std::cout; using std::endl;
using std::ostream; using std::cin; using std::boolalpha;

#include<string>
using std::string;

using namespace std;

template <class V>
struct SetElement
{
    V element;
    int cnt;

    SetElement() = default;
    SetElement(V v) : element(v){}
};

template <class V>
ostream &operator<<(ostream & o,const SetElement<V> &p)
{
    return o << p.element;
}

template <class V>
class MSet
{
private:
    SetElement<V> *ary;
    size_t capacity_;
    size_t size_;

public:
    MSet(V val)
    {
        capacity_ = 2;
        ary = new SetElement<V>[capacity_];
        ary[0].element = val;
        ary[0].cnt = 1;
        size_ = 1;
    }

    SetElement<V>* find(V val)
    {
        SetElement<V> *found = nullptr;
        bool yes = false;
        for (int i = 0; i < size_ && !yes; i++)
        {
            if (ary[i].element == val)
            {

                found = &ary[i];
                yes = true;
            }
        }
        return found;
    }

    friend ostream& operator<<(ostream &out, const MSet<V> &m)
    {
        string s = "";
        s += "Size " + to_string(m.size_) + "\n";//out << m.size() << endl;
        s += "Cap " + to_string(m.capacity_) + "\n"; //out << m.capacity() << endl;
        for (int i = 0; i < m.size_; i++)
        {
            if (i < m.size_ - 1)
                s += m.ary[i].element + ",";//out << m.ary[i].element << ",";
            else
                s += m.ary[i].element;
        }
        //cout << "String : " << s;
        return out << s;
    }

};

int main(){
    int test;
    long l1, l2, l3;

    cin >> test;
    cout << boolalpha;

    switch (test){

        // ...
    case 2: {
        cin >> l1 >> l2;
        MSet<long> m_l(l1);
        auto p = m_l.find(l1);
        if (p != nullptr)
            cout << *p << endl;
        else
            cout << "Val:" << l1 << " not found " << endl;

        p = m_l.find(l2);
        if (p != nullptr)
            cout << *p << endl;
        else
            cout << "Val:" << l2 << " not found " << endl;
        //cout << "MList \n";
        cout << m_l;
        break;
    }
        // ...
    }
}

Upvotes: 0

Views: 80

Answers (1)

rocambille
rocambille

Reputation: 15976

You're adding the values into a temporary string, which may involve implicit conversions depending of the template type (here your numerical values were converted into characters).

Just print the values, without the temporary string:

friend ostream& operator<<(ostream &out, const MSet<V> &m)
{
    out << "Size " << m.size_ << endl;
    out << "Cap " << m.capacity_ << endl;
    for (int i = 0; i < m.size_; i++)
    {
        if (i < m.size_ - 1)
            out << m.ary[i].element << ",";
        else
            out << m.ary[i].element;
    }
    return out;
}

Upvotes: 4

Related Questions