Assuming you aren't allowed to use the STL list class because this is a learning/teaching experience, then you should create your own linked list class. If you feel comfortable using templates fine, otherwise you can create a class for each type or include data variables for each type in the linked list node. The insert process can occur anywhere in the list, that is at the front, the back or somewhere inbetween. Some people will provide special insert functions for each option.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
At minimum you will want to make the node class you use in the linked list template compatible. Post the node class and how you think it might be doable if yor aren't sure how to do that.
Define what you mean by insert---only at head, only at end, random insertion, insert only if unique node value, insert in ascending order, whatever.
In order to do thinks like search, edit, erase, random insert, etc, then you probably should develop additional functionality in the classes you want to use in the list.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
node<Square>;
LinkedList<Square> sqList;
should create a LinkedList of Squares if node is something like:
template<class T>
node
{
T data;
node * next;
};
The types you declare will work just like native types when it comes to lists.
Assuming Insert is written something like:
template<class T>
class LinkedList
{
....
public:
void Insert<T>;
....
};
Then you could insert a Square in sqList by trying this:
Point A(4, 5);
Square s(A, 5, 6);
sqList.Insert(s);
or if you create a default constructor for Square (and you should) you could do this:
Square defaultSquare;
sqList.Insert(defaultSquare);
There may be other syntax that would allow Insert() to work, too, but those two options are a good start.
BTW: To populate the member variables of defaultSquare now stored in the node use a pointer to the node with "defaultSquare" in it and use public accessors, probably something like this:
current->data.setX(x);
current->data.setY(Y);
current->data.setLength(L);
current->data.setWidth(W);
if your Square class had public accessors, which it doesn't at this time.
Finally you can display a node containing a Square with similar syntax:
current->data.SquareSpec();
except that SquareSpec() is defunct, since you can only return one value at a time, not 4. SquareSpec() could return a Square or it could display all of values with internal calls to cout, or do something other than it is now. But that isn't the question you asked, so I'll be quiet now.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
I've not been in the habit of declaring the node class within the list class, but from what I can see elsewhere, it seems okay to do, and your syntax is what I found elsewhere.
It should not be necessary to use Node<Square> in your code above.
Personally I would swap lines 32 and lines 34 in Insert() and I would assign NULL to prev as well as to next.
I believe
new Node
will use the default constructor for Node which is okay, but then within Node
T data;
will probably call the default constructor for whatever T is. However, your Point and your Square classes don't have default constructors, at least as posted in your first post.
If removing Node<Square> doesn't work, then either post specific error message(s) or consider adding default constructors for both Point and Square since the compiler won't provide one if you declare a non-default constructor.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
I agree
[LinkedList<Square>::Node::Node()]+0xd): undefined reference to `Square::Square()
would appear to indicate you need to have a default constructor for Square when you call the default constructor of Node(). In addition to declare the default constructor Square(); in the Square class you need to define it. The definition could be done at declaration or separately (like you did for the constructor you already have). The defaul constructor could have a blank function body or, preferably in my opinion, it could assign the default value of zero to all 4 member variables.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
Yes, but you have to define the default constructor, too. Put empty {} between the the () and the ; or define it similar to how you did Square(Point, int, int);
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
When you declare a linked list using type int, class int has the << operator already overloaded for use. Same for the STL string class. However, I suspect that your Square class does not. If that's true, then declare and define an overloaded << operator and draw() should work for class Square, too.
Lerner
Nearly a Posting Maven
2,408 posts since Jul 2005
Reputation Points: 739
Solved Threads: 406
Skill Endorsements: 9
Question Answered as of 1 Year Ago by
Lerner