0

So this is my own Vector class, which I have written:

#ifndef VEKTOR_HPP
#define VEKTOR_HPP

#include <iostream>

template<typename T>
class Vektor
{
    T* tomb;
    size_t meret;
    size_t kapacitas;
public:
    Vektor() : tomb(NULL), meret(0), kapacitas(0) {}

    Vektor(Vektor const& v)
    {
        meret = v.meret;
        kapacitas = v.kapacitas;
        tomb = new T[kapacitas];
        for(size_t i = 0; i < meret; i++)
        {
            tomb[i] = v.tomb[i];
        }
    }

    void berak(T adat)
    {
        if(meret >= kapacitas)
        {
            T* temp = new T[kapacitas+=10];
            for(size_t i = 0; i < meret; i++)
            {
                temp[i] = tomb[i];
            }
            delete [] tomb;
            tomb = temp;
        }
        tomb[meret] = adat;
        meret++;
    }

    size_t getkapacitas() const {return kapacitas;}
    size_t getmeret() const {return meret;}

    Vektor& operator=(Vektor const& v)
    {
        if(this != &v)
        {
            delete[] tomb;
            meret = v.meret;
            kapacitas = v.kapacitas;
            tomb = new T[kapacitas];
            for(size_t i = 0; i < meret; i++)
            {
                tomb[i] = v.tomb[i];
            }
        }
        return *this;
    }

    T& operator[](size_t idx)
    {
        if(idx >= meret)
        {
            throw "Tulindexeles!";
        }
        return tomb[idx];
    }

    T const& operator[](size_t idx) const
    {
        if(idx >= meret)
        {
            throw "Tulindexeles!";
        }
        return tomb[idx];
    }

    void kiir()
    {
        for(size_t i = 0; i< meret; i++)
        {
            std::cout << tomb[i] << " ";
        }
        std::cout << std::endl;
    }

    ~Vektor()
    {
        delete[] tomb;
    }
};

#endif // VEKTOR_HPP

And this is the problem, I keep getting, and I can't quite figure out how to solve it exactly. I do know I have to overload the << operator, but I couldn't find a solution.

error: no match for 'operator<<' (operand types are 'std::ostream' {aka 'std::basic_ostream'} and 'Jatekos')|

  • 1
    You have to implement it as a free function, not a member function, see: https://stackoverflow.com/questions/4421706/what-are-the-basic-rules-and-idioms-for-operator-overloading – Paul Sanders May 01 '22 at 22:35
  • 1
    Where is your attempt to define `operator<<`? What is the type `Jatekos`? Where is the error occurring? – paddy May 01 '22 at 22:35
  • Did you try `friend std::ostream& operator<<(std::ostream& os, const Jatekos& j) { }` ? – Ted Lyngmo May 01 '22 at 22:35

0 Answers0