Witam, napisałem obiektowo listę jednokierunkową jako szablon klasy i przy tworzeniu obiektu SLinkedList<int> Lista; wyskakuje mi error SLinkedList<int> is ambiguous. Jakieś pomysły na rozwiązanie problemu?
0
1
Jedno z dwóch:
- Spytać o to na forum wróżbitów
- Pokazać kod
0
template<typename E> class SLinkedList;
template<typename E>
class SNode{
private:
E element;
SNode<E> * next;
friend class SLinkedList<E>;
public:
E getElement(){
return element;
}
SNode getnext(){
return next;
}
void setElement(E newElement){
this->element=newElement;
}
void setnext(SNode <E> newNext){
this->next=newNext;
}
};
#include "Wezel.hh"
template<typename E>
class SLinkedList {
private:
SNode<E> *head;
public:
SLinkedList();
~SLinkedList();
bool empty() const;
const E & front() const;
void addFront(const E& elem);
void removeFront();
void printList();
};
#include "Lista.hh"
#include <cstdlib>
#include <iostream>
using namespace std;
template<typename E>
SLinkedList<E>::SLinkedList()
:head(NULL){}
template <typename E>
SLinkedList<E>::~SLinkedList()
{
while (!empty())
removeFront();
}
template<typename E>
bool SLinkedList<E>::empty() const{
return (head==NULL);
}
template<typename E>
const E & SLinkedList<E>::front() const{
return head->element;
}
template<typename E>
void SLinkedList<E>::addFront(const E& elem){
SNode<E> * n=new SNode<E>;
n->element=elem;
n->next=head;
head=n;
}
template<typename E>
void SLinkedList<E>::removeFront(){
if(this->empty()){
cout<<"Lista jest pusta, nie ma co usunac"<<endl;
}
else{
SNode<E> * tmp=head;
head=head->next;
delete tmp;
}
}
template<typename E>
void SLinkedList<E>::printList(){
SNode<E> *tmp=head;
while(tmp !=NULL){
cout<<tmp->element;
tmp=tmp->next;
}
}
#include <iostream>
#include "Lista.cpp"
#include "Lista.hh"
using namespace std;
int main(){
SLinkedList<int> lista;
return 0;
}
0
Czy przypadkiem nie dołączyłeś lista.cpp do projektu?
Bo to się kompiluje http://ideone.com/U2y0Om
3
#include "Lista.cpp"
#include "Lista.hh"
- Szablony musisz zdefiniowac w jednym pliku. (a nie rozbijac na dwa pliki jak zwykle klasy). Jak wrzucisz z cpp do h to powinno sie kompilowac
- Nigdy nie powinno sie dolaczac zadnego pliku cpp
0
wlasnie wyczytalem na zagranicznym forum, ze przy szablonach mozna dodac #include "*.cpp" i to ozwiazuje problem. Btw error wyskakuje w eclipse.
1
Co? @fasadin dobrze napisał a nie ludzie na zagranicznych forach. class template
powinieneś mieć ** prawie zawsze ** zdefiniowane w hpp. Żaden cpp nie powinien istnieć.
0
Po wrzuceniu do jednego pliku kompiluje się i działa. Jednak w eclipse dalej error wyskakuje.
1
W pliku h na początek dorzuć:
#pragma once
lub
#ifdef PLIK_H
#define PLIK_H
// tutaj to co się znajduje w tym pliku
#endif