Witam, walczę ostatnio z tym problemem:
#pragma once
template <class type>
class Stacker{
struct container{
type storage; // stored a single type value
container* connection; // connection to next container
};
container* felement; // first element
unsigned long long insize; // size of a stack
public:
Stacker();
~Stacker();
void push(const type& tobj);
void pop();
void clear();
const type& top() const;
const unsigned long long& size() const;
};
#include "Stacker.h"
template <class type>
Stacker<type>::Stacker(){
////////////////////////////////////////////////////////////////////
this->felement = NULL;//////////////////////////////////////////////
////////////////////////////////////////////////////////////////////
this->insize = 0;
}
template <class type>
Stacker<type>::~Stacker(){
this->clear();
}
template <class type>
void Stacker<type>::push(const type& tobj){
container* handle = new container; // new container
handle->storage = tobj; // assign what will be stored
handle->connection = this->felement; // point at previous container on stack
this->felement = handle; // grab the new container
this->insize++;
}
template <class type>
void Stacker<type>::pop(){
container* handle = this->felement; // grab stacks first container
this->felement = this->felement->connection; // grab stacks second container
delete handle; // remove unwanted first container
this->insize--;
}
template <class type>
void Stacker<type>::clear(){
while(this->felement != NULL)
this->pop();
}
template <class type>
const type& Stacker<type>::top() const{
if(this->felement == NULL)
return NULL;
else
return this->felement->storage;
}
template <class type>
const unsigned long long& Stacker<type>::size() const{
return this->insize;
}
Napisałem taką klasę, założenia: Dynamiczna liczba argumentów, Nieznany Typ argumentów.
Mam 2 problemy z tym, pierwszy jest taki, że nie działa przy wywołaniu:
#include "Stacker.h"
// Kompiluję komendą: g++ main.cpp -o main Stacker.cpp
// uruchamiam: ./main
// system to Linux mint
int main(){
Stacker<char> stack;
printf("e stack fele: %d\n",stack.top());
printf("size: %lld\n",stack.size());
}
Wyskakuje błąd:
/usr/bin/ld: /tmp/cc80CBJU.o: in function `main':
main.cpp:(.text+0x250): undefined reference to `Stacker<char>::Stacker()'
/usr/bin/ld: main.cpp:(.text+0x25c): undefined reference to `Stacker<char>::top() const'
/usr/bin/ld: main.cpp:(.text+0x284): undefined reference to `Stacker<char>::size() const'
/usr/bin/ld: main.cpp:(.text+0x2aa): undefined reference to `Stacker<char>::~Stacker()'
/usr/bin/ld: main.cpp:(.text+0x2d3): undefined reference to `Stacker<char>::~Stacker()'
collect2: error: ld returned 1 exit status
Drugim problemem jest natomiast tak jak w pliku Stacker.cpp zaznaczyłem w liniach 6,7,8 wskaźnik do elementu typu "type", chcę aby początkowo stos był pusty więc NULL wydawał się czymś odpowiednim, zwłaszcza, że używając metody "top()" zwraca ona typ "const type&" . Sam już nie wiem jak to zaimplementować, żeby mogło coś zwracać dla pustego stosu, coś co by się nie wysypywało (zakładając, że stos może być używany dla bardziej złożonych typów niż char, również dla takich gdzie nie ma konstruktora domyślnego itd).
Edit: Paradoksem jest to, że nawet gdy stos jest pusty metoda "top()" musi zwrócić referencje do obiektu, lecz nawet gdy nie zwracamy referencji ale kopię obiektu to również to by nie poszło bo tak jak wspominałem co z obiektami bez domyślnych konstruktorów, nie stworzymy takiego tylko na potrzeby zwrócenia czegokolwiek.