简体   繁体   中英

C++ Operator Overload >>

I hadn't found answer even if exist anywhere. I have class and I want overload >> operator

That code compile but it don't work how I want. I want put value in all cells inside array, but it only work for 1st cell. Looks like my loop don't work.

Edited: Full code below. Btw sorry for language but this is my homework and teacher want that names.

    #include <cstdlib>
    #include <iostream>
    #include <string>
    #include <cmath>

    template <class T>
    class Wielomian {
    private:
        int stopien;
        T *wspolczynniki;
    public:
        friend std::ostream & operator << (std::ostream &output, Wielomian &w)
        {
            output << "Wielomian: ";
            for (int i = w.stopien-1; i >= 0; i--)
            {
                output << w.wspolczynniki[i] << "x^" << i << " ";
                if (i)
                    output << "+ ";
            }
            return output;
        }
        friend std::istream & operator >> (std::istream &input, Wielomian &w)
        {
            int i = 0;
            do {
                input >> w.wspolczynniki[i++];
            } while (w.stopien < i);
            return input;
        }
        T operator () (T x)
        {
            T wynik = 0;
            for (int i = 0; i < this->stopien; i++)
            {
                wynik += this->wspolczynniki[i] * pow(x,i);
            }
            return wynik;
        }
        T& operator[](const int index)
        {
            return wspolczynniki[index];
        }
        Wielomian operator + (const Wielomian &w)
        {
            const Wielomian *wiekszy;
            const Wielomian *mniejszy;
            if (w.stopien > this->stopien)
            {
                wiekszy = &w;
                mniejszy = this;
            }
            else
            {
                wiekszy = this;
                mniejszy = &w;
            }

            for (int i = 0; i < mniejszy->stopien; i++)
                wiekszy->wspolczynniki[i] += mniejszy->wspolczynniki[i];
            return *wiekszy;
        }
        Wielomian operator - (const Wielomian &w)
        {
            const Wielomian *wiekszy;
            const Wielomian *mniejszy;
            if (w.stopien > this->stopien)
            {
                wiekszy = &w;
                mniejszy = this;
            }
            else
            {
                wiekszy = this;
                mniejszy = &w;
            }

            for (int i = 0; i < mniejszy->stopien; i++)
                wiekszy->wspolczynniki[i] -= mniejszy->wspolczynniki[i];
            return *wiekszy;
        }
        Wielomian operator = (const Wielomian &w)
        {
            this->stopien = w.stopien;
            this->wspolczynniki = new float[this->stopien];
            memcpy(this->wspolczynniki, w.wspolczynniki, w.stopien * sizeof(double));
        }
        Wielomian(const Wielomian &w)
        {
            this->stopien = w.stopien;
            this->wspolczynniki = new float[this->stopien];
            memcpy(this->wspolczynniki, w.wspolczynniki, w.stopien * sizeof(double));
        }
        Wielomian(int stopien = 0, T wspolczynik[] = { 3 })
        {
            this->stopien = stopien;
            wspolczynniki = new T[this->stopien];
            for (int i = 0; i < stopien; i++)
            {
                this->wspolczynniki[i] = wspolczynik[i];
            }
        }
        ~Wielomian()
        {
            free(this->wspolczynniki);
        }

    };



    int main()
    {
        double tab[4] = {3,4,5,6};
        Wielomian<double> w1(4,tab);

        std::cin >> w1;
        std::cout << w1;


        std::cin.get();
        std::cin.get();
        return 0;
    }

Your while condition in operator >> is wrong. In your code you have:

int i = 0;
do {
  input >> w.wspolczynniki[i++];
} while (w.stopien < i);

i is 0 at the beginning, and then after input >> w.wspolczynniki[i++] it is 1 . The while condition is (w.stopien < i) so if w.stopien (which is 4 in your example) is smaler then i which is 1 in the first iteration, you will continue the loop. But 4 < 1 is false you will always only read one value.

So get your do-while to work you would need to change it to (w.stopien > i) . But as you test if your index i is in the correct range you shouldn't use a do-while at all, but a while loop.

int i = 0;
while (i < w.stopien) {
  input >> w.wspolczynniki[i++];
}

Or even a for loop, which would make clearer what you are doing:

for(int i=0; i< w.stopien; i++) {
  input >> w.wspolczynniki[i];
}

In addition to that - and what is already mentioned in the comments - never combine the memory allocations and deallocation that don't belong together. If you use new[] then you have to use delete[] to free the memory and not free .

And don't use signed numbers ( int ) for indices, so stopien and i should be unsigned (eg size_t ). And for stopien you should ensure on construction that it is 1 or larger.

And if you are allowed to you should switch form T* wspolczynniki to std::vector<T> wspolczynniki that would allow you to get rid of the copy constructor, the assignment operator, the destructor, you would not need int stopien , and you could simplify other parts of the code using [algorithm](https://en.cppreference.com/w/cpp/algorithm), or at least keep that you normally would use a std::vector` (or other containers) then doing the allocation and the copying yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM