Hello

I am a new to C++ and have some questions.

I have two classes called Point and Square like this:

class Point {
    int x, y;
public:

   int getx() const {
        return x;
    }

    int gety() const {
        return y;
    }

    Point(int a, int b) {
        x = a;
        y = b;

    }
};

class Square {
    
public:
   int x, y, length, width;
   SquareSpec(){return x, y, length, width;}
    Square(Point, int, int);
     
};
Square::Square(Point A, int lngt, int wdth){
    length = lngt;
    width = wdth;
    x = A.getx();
    y = A.gety();
}

Now the thing is that somehow I need to put the Square specifications in to the linked list.
So in the main I get something like this:

int main() {
LinkedList<...> square1;
Point A(3,4);
Square sqrt(A, 6, 9);
square1.Insert(...);
}

My linked list insertion looks like this:

template <class T>
void LinkedList<T>::Insert(T d) {
}

What is the best way to get these specs in to the linked list?

Recommended Answers

All 14 Replies

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.

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.

Yes, it is a learning assignment. And I already wrote a linked list class. It has to be generic so I have to use the template. If you want i can post the linked list, but at first the problem lays in the code posted. I have a lot of problems using classes and passing them.

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.

For now I only need to be inserting at head. All the sorting and things like that will come later.
And I have my linked list working already. I already made a list like this:

LinkedList<int> intItems;
LinkedList<String> stringItems;
intItems.Insert(30);
intItems.Insert(30);
stringItems.Insert("astring");

The problem is, is that I don't know what the best way is to add the specifications of the square to the list. I need them to be in one list so later every item can be handled one by one.

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.

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.

Thanks for the wide answer. For a lot i already went in the right direction.
Al tough I get errors.
This is what my list looks like:

template <class T>
class LinkedList {
private:

    struct Node {
        T data;
        Node* next;
        Node* prev;
    };
    Node* head;
    Node* last;
public:

    LinkedList() {
        head = NULL;
        last = NULL;
    }
    ~LinkedList();

    bool isEmpty() const {
        return head == NULL;
    }
    void Insert(T d);
    void Remove(T d);
    void draw();
};

template <class T>
void LinkedList<T>::Insert(T d) {
    if (isEmpty()) {
        Node* temp = new Node;
        head = temp;
        temp->next = NULL;
        temp->data = d;
    }
}

And I tried to add this in the main:
Node<Square>;
But that gives errors. Probably because its private.

int main() {
LinkedList<Square> square1;
Node<Square>;
Point A(1,4);
Square sqrt(A, 6, 9);
square1.Insert(sqrt);
// square1.draw();
}

And with the return of four values.. shame on me. I should now that.

Can you tell me what I'm doing wrong with the insertion?
Is the "struct node" the right way?

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.

So I have added default constructors and I don't get the errors anymore. So thank you for that!!
But! I get another error which i can not understand, because it doesn't give me specific lines where the error is. This is what I get:

build/Debug/Cygwin-Windows/main.o: In function `_ZNK5Point4getxEv':
/cygdrive/c/Users/user/Documents/NetBeansProjects/CppApplication_1/main.cpp:(.text$_ZN10LinkedListI6SquareE4NodeC1Ev[LinkedList<Square>::Node::Node()]+0xd): undefined reference to `Square::Square()'
collect2: ld returned 1 exit status
make[2]: *** [dist/Debug/Cygwin-Windows/cppapplication_1.exe] Error 1
make[1]: *** [.build-conf] Error 2
make: *** [.build-impl] Error 2

A default constructor is just adding Square(); in the public above the normal constructor right?

Any thoughts?

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.

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.

So that is what i did. And I am still getting this error.
For now I left it blank.
So now I have a default constructor and a normal constructor. Am I saying it right?

public:
   int x, y, length, width;
   Square();    
    Square(Point, int, int);    
};

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 as you are already my hero, and about to put this thread as solved.
I still am going to ask one more question. My insertion is working.
But my draw is not working, but was working when making an linked list of ints or strings with a for loop and:
cout << curr->data << " ";
But when inserted square and trying to draw I get errors on this one.
This will be my last issue before I can go on hacking by myself.

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, you are a master..

Thank you so much for your help and effort!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.