Reputation: 845
For the code below:
#include <iostream>
#include <string>
using namespace std;
class Foo2;
class Foo3;
template <class T>
class Foo1 {
public:
Foo1();
void print() {
cout << "My name is: " << name << endl;
}
T getNext(){
return nextLink;
}
string name;
T nextLink;
};
class Foo2 : public Foo1 {
public:
Foo2(){
name = "Foo2";
}
};
class Foo3 : public Foo1 {
public:
Foo3(){
name = "Foo3";
}
};
template <class T>
class LinkedList {
public:
T curr;
T first;
void add(T node){
if(first == NULL){
first = node
}
node->nextLink = this;
curr = node;
}
T getNext(){
return next;
}
void printAll(){
T curr = first;
cout << "Contents are: " ;
while(curr != NULL){
cout << curr.print() << ", ";
curr = curr.getNext();
}
}
};
int main() {
LinkedList<?> list;
list.add(new Foo2());
list.add(new Foo3());
list.printAll();
return 0;
}
I'm attempting to implement a generic linked list, i realise that i could import <list>
but that wouldn't suit my project. I'm trying to have a linked list of Foo2
and Foo3
objects - the above is the best i could accomplish as i'm new to C++.
Error:
generic.C: In instantiation of Foo1<Foo2>:
generic.C:26: instantiated from here
generic.C:22: error: Foo1<T>::nextLink has incomplete type
generic.C:6: error: forward declaration of âclass Foo2
generic.C: In instantiation of Foo1<Foo3>:
generic.C:34: instantiated from here
generic.C:22: error: Foo1<T>::nextLink has incomplete type
generic.C:7: error: forward declaration of class Foo3
generic.C: In member function void LinkedList<T>::add(T):
generic.C:50: error: expected ; before } token
generic.C: In member function T LinkedList<T>::getNext():
generic.C:55: error: ânextâ was not declared in this scope
generic.C: In function âint main()â:
generic.C:69: error: template argument 1 is invalid
generic.C:69: error: invalid type in declaration before â;â token
generic.C:70: error: request for member âaddâ in âlistâ, which is of non-class type âintâ
generic.C:71: error: request for member âaddâ in âlistâ, which is of non-class type âintâ
generic.C:72: error: request for member âprintAllâ in âlistâ, which is of non-class type âintâ
Upvotes: 0
Views: 2703
Reputation: 179819
Combining the bits, it seems like this should work:
int main() {
std::list<boost::variant<Foo2, Foo3> > list;
list.push_back(Foo2());
list.push_back(Foo3());
printAll(list); // You'd still need to write this obviously.
return 0;
}
Upvotes: 0
Reputation: 272517
Despite your assertions to the contrary, the example you've given could be solved with std::list
:
std::list<Foo1 *> list;
list.push_back(new Foo2());
list.push_back(new Foo3());
for (std::iterator<Foo1 *> it = list.begin(); it != list.end(); ++it)
{
(*it)->print();
}
Obviously, there's a potential memory leak here...
Upvotes: 2
Reputation: 576
I think the problem is the "?" in LinkedList
If this is the case, then you should use LinkedList<Foo1 *>
.
Why can't you use std::list? Maybe we can help you with that, it will be far better that using your own implementation.
Upvotes: 2
Reputation: 146930
You need to use a T*, not a T. Looks to me like you came from Java where everything is a reference. There is no ?
in C++ templates. I think that you need to pick up a book on basic C++ first, and then come back to templates.
Upvotes: 3