ďťż

[C++] Przeciążanie operatorów >> << + w szabloni

Z pAmIęTnIkA nIeGrZeCzNeGo AnIołkA

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

BufferTemplate.h:
Kod: #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:
Kod: #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:63: 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:65: 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:63: error: declaration of `operator>>' as non-function
BufferTemplate.h:63: error: expected `;' before '<' token
BufferTemplate.h:64: error: declaration of `operator<<' as non-function
BufferTemplate.h:64: error: expected `;' before '<' token
BufferTemplate.h:65: error: declaration of `operator+' as non-function
BufferTemplate.h:65: error: expected `;' before '<' token
In file included from Test.cpp:4:
BufferTemplate.cpp:205: error: template-id `operator>><>' in declaration of primary template
BufferTemplate.cpp:212: error: template-id `operator<< <>' in declaration of primary template
BufferTemplate.cpp:219: 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:71: instantiated from here
BufferTemplate.cpp:208: 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?
[/code]
  • zanotowane.pl
  • doc.pisz.pl
  • pdf.pisz.pl
  • drakonia.opx.pl
  • Copyright (c) 2009 Z pAmIęTnIkA nIeGrZeCzNeGo AnIołkA | Powered by Wordpress. Fresh News Theme by WooThemes - Premium Wordpress Themes.