szablon klas i przeciążenie [] i =

0

mam taką klasę:

#include "ipcs.h"
#include <sys/shm.h>
/*!
klasa generyczna obslugi pamieci wspoldzielonej.
*/
template<typename Ttype>
class memory :public ipcs
{
private:
   bool enable;
    /// bufor przechowujacy dane
    Ttype *buf;
    /// maksymala liczba zmiennych umieszczonych w tablicy buf
    int MaxSize;

public:
    /// konstruktor pobierajacy maksymalny rozmiar pamieci wspoldzielonej
    memory(unsigned int size);
   memory();
    ~memory();

   void setSize(unsigned int size);
    /*!
    Przeladowany operator rownosci. Pozwala na bezposrednie przypisanie elementow znakiem =
     */
    void operator=(Ttype value);

    /*!
    Przeladowany operator []. Dzieki temu na obiekcie mozemy operowac jak na normalnej tablic
    */
    Ttype &operator[](int number);

};

template<typename Ttype>
 memory<Ttype>::memory(unsigned int size)
{
    this->ID = shmget(IPC_PRIVATE,sizeof(Ttype)*size,IPC_CREAT|0666);


    this->buf =  (Ttype*)shmat(this->ID,NULL,0);

    this->MaxSize = size;
    this->enable = true;
}
template<typename Ttype>
 memory<Ttype>::memory()
{
    
    this->enable = false;
}

template<typename Ttype>
Ttype &memory<Ttype>::operator[](int element)
{
    if(element > this->MaxSize-1)
        throw "Blad naruszenia pamieci";


    Ttype *pointer = this->buf;

   // pointer+= element;

    return *this->buf[0];
}


template<typename Ttype>
void memory<Ttype>::operator=(Ttype value)
{
    buf = &value;
}

template<typename Ttype>
memory<Ttype>::~memory()
{  // zwalnianie pamieci
   //if(this->buf)
    //  delete buf;

}
template<typename Ttype>
void memory<Ttype>::setSize(unsigned int size)
{  
   if(this->enable == false)
   {
      this->ID = shmget(IPC_PRIVATE,sizeof(Ttype)*size,IPC_CREAT|0666);
      this->buf =  (Ttype*)shmat(this->ID,NULL,0);
   
      this->MaxSize = size;
      this->enable = true;
   }
}

i probujac przypisac wartosc do inta:

   int mem2 = shmget(IPC_PRIVATE,sizeof( memory<int> ),IPC_CREAT|0666);
        memory<int> *buff = (memory<int>*)shmat(mem2,NULL,0);

        buff->setSize(10);
         tmp = buff[j%10];

pokazuje ze nie moze przekonwertowac z memory<int> do int. Co jest źle w tym przeciążeniu ??

0
template<typename Ttype>
Ttype& memory<Ttype>::operator[](int element)
{
	if(element >= MaxSize) throw "Blad naruszenia pamieci";
	return buf[element];
}
tmp = (*buff)[j%10];

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