Przeciążanie operatorów >><< + w szablonie klasy

0

Jak przeciążyć operatory >> << + w szablonie klasy. Taki kod:

BufferTemplate.h:

#include <iostream> //istream, ostream
const int maxTextSize = 100;
const size_t enlargeSize = 50;
const size_t maxBufferSize = 1000;
const size_t intSize = sizeof (int);
template <class T> class Buffer //klasa bufora
{
    private:
        (...)
    public:
        (...)
        friend std::istream & operator>> (std::istream &i, Buffer<T> &b); //pobiera i usuwa tablice znakową z bufora oraz ją wyświetla
        friend std::ostream & operator<<  (std::ostream &o, Buffer<T> &b); //dodaje do bufora tekst
      
        friend Buffer<T> operator+ (const Buffer<T> &A, const Buffer<T> &B); //tworzy bufor z zawartości dwóch innych buforów
};

BufferTemplate.cpp:

#include <iostream> //istream, ostream, cin, cout
#include "BufferTemplate.h" //Buffer - włączenie mojego pliku nagłówkówego z deklaracją klasy
using namespace std; //istream, ostream, cin, cout
(...)
template <class T> istream & operator>>  (istream &i, Buffer<T> &b)
{
    T temp[maxTextSize];
    i >> temp;
    b->Add(temp);
    return i;
}
template <class T> ostream & operator<< (ostream &o, Buffer<T> &b)
{
    char temp[maxTextSize];
    b->Get(temp);
    o << temp;
    return o;
}
template <class T> Buffer<T> operator+ (const Buffer<T> &A, const Buffer<T> &B)
{
    bool deleteAfterOverload;
    if (A.DeletesAfterOverload() || B.DeletesAfterOverload())
    {
        deleteAfterOverload = true;
    }
    else
    {
        deleteAfterOverload = false;
    }
    size_t resultSize;
    resultSize = A.GetSize() + B.GetSize();
    if (resultSize > maxBufferSize)
    {
        resultSize = maxBufferSize;
    }
    int a;
    const int x = A.GetFilling();
    const int y = B.GetFilling();
    T *data = new T [resultSize];
    for (a = 0; a < x && a < resultSize; a++)
    {
        data[a] = A.GetChar(a);
    }
    int b;
    for (b = 0; b < y && a < resultSize; a++, b++)
    {
        data[a] = B.GetChar(b);
    }
    return *(new Buffer<T>(data, deleteAfterOverload, a, resultSize));
}

powoduje błędy: "In file included from BufferTemplate.cpp:4,
BufferTemplate.h warning: friend declaration std::istream& operator>>(std::istream&, Buffer<T>&)' declares a non-template function BufferTemplate.h:63: warning: (if this is not what you intended, make sure the function template has already been declared and add <> after the function name here) -Wno-non-template-friend disables this warning BufferTemplate.h:64: warning: friend declaration std::ostream& operator<<(std::ostream&, Buffer<T>&)' declares a non-template function
BufferTemplate.h warning: friend declaration `Buffer<T> operator+(const Buffer<T>&, const Buffer<T>&)' declares a non-template function
Undefined first referenced
symbol in file
operator>>(std::basic_istream<char, std::char_traits<char> >&, Buffer<char>&)/var/tmp//ccbFaGuc.o
operator+(Buffer<char> const&, Buffer<char> const&)/var/tmp//ccbFaGuc.o
operator<<(std::basic_ostream<char, std::char_traits<char> >&, Buffer<char>&)/var/tmp//ccbFaGuc.o".

Po dodaniu <> (po operator>>, operator<< i operator+) są natomiast błędy: "In file included from BufferTemplate.cpp:4,
BufferTemplate.h error: declaration of operator>>' as non-function BufferTemplate.h:63: error: expected ;' before '<' token
BufferTemplate.h error: declaration of operator<<' as non-function BufferTemplate.h:64: error: expected ;' before '<' token
BufferTemplate.h error: declaration of operator+' as non-function BufferTemplate.h:65: error: expected ;' before '<' token
In file included from Test.cpp
BufferTemplate.cpp error: template-id operator>><>' in declaration of primary template BufferTemplate.cpp:212: error: template-id operator<< <>' in declaration of primary template
BufferTemplate.cpp error: template-id operator+<>' in declaration of primary template BufferTemplate.cpp: In function std::istream& operator>>(std::istream&, Buffer<T>&) [with T = char]':
Test.cpp instantiated from here
BufferTemplate.cpp error: base operand of ->' has non-pointer type Buffer<char>'
BufferTemplate.cpp: In function std::ostream& operator<<(std::ostream&, Buffer<T>&) [with T = char]': Test.cpp:74: instantiated from here BufferTemplate.cpp:214: error: base operand of ->' has non-pointer type `Buffer<char>'".

Jak powinny wyglądać nagłówki/deklaracje operatorów tych operatorów przeciążonych, żeby nie było błędów?

0

Wrzuć definicje operatorów do BufferTemplate.h.

1 użytkowników online, w tym zalogowanych: 0, gości: 1