Creating functional get and set methods.

Hello everyone !

I found this interesting lib LinkedList.h. It's my first time using it and I haven't seen any related problems using it as well. I'm trying to build get and set methods in order to actually link the lists (put one list inside the other and so on).

My first attempt in creating the set method was this :

void MyClass::setMyList(LinkedList<MyList*> mylist){
_mylist = mylist;

}

This did not work as it should. And the reason this is happening remains a mistery to me.

Then I tryed another attempt :

void MyClass::addList(Lists* list){
_list.add(list);

}

This second attempt seems to be working so far. But I'm really confused about all of this... my current get method is like this :

LinkedList<MyList*>MyClass::getMyList(){
return _mylist;

}

Which is not working too, can someone show me how to build both set and get list methods for this lib? If any more info is needed, please let me know. Thanks in advance !

This did not work as it should.

It did something. You expected it to do something. We do not know what either thing is.

Which is not working too

That, too, tells us nothing.

Post ALL of your code, and some proof that it "does not work", whatever that means.

We have no idea what type _myList is, for instance.

You should NOT be passing lists by value. You should be passing pointers.

Ok !

What I expect :

Since I'm deserializing a JSON, and I need some values among its data, those values must be managed using linked lists. Please consider this website for better understanding of the architecture JSON EDITOR ONLINE, then please paste this json and upload it to the editor {"Identificação":1002,"Programa":"PRG VRO","Agendamentos":[{"Day":1,"Hour":"14:10:00","Events":[{"Setor":13,"Duration":180,"Sequencial":1,"Receita":"REC01","Water":1.5,"Feeds":[{"Silo":"Silo 12","Feed":"Potássio","Volume":22.356}]}]},{"Day":1,"Hour":"14:30:00","Events":[{"Setor":14,"Duration":150,"Sequencial":3,"Receita":"REC01","Water":1.53,"Feeds":[]}]},{"Day":1,"Hour":"14:40:00","Events":[{"Setor":15,"Duration":160,"Sequencial":4,"Receita":"REC01","Water":1.5,"Feeds":[{"Silo":"Silo 13","Feed":"Potássio","Volume":2.3},{"Silo":"Silo 17","Feed":"Potássio","Volume":2.217}]}]}]}

I expect to properly deserialize this JSON (which is already being done) and as it is deserialized I should fill the lists with the values by using the set method, and I must also be able to GET the filled list using the get method.

What they did :

  • The switched set method seems to be working fine, its adding my list to the object. And then linked to another list.
  • The get method is not returning the list properly, actually I am getting some wrong returns and crashing right after (this will be explained better soon)

I've attached some files containing the code part ,my code is really big right now, so I restricted only to the LinkedList managing part, I wonder if excess of information would bring you too much effort to help me, but if you need anything else, feel free to ask.

So there is a file called Deserializer which is a method for deserializing JSON in the format shown previously. It goes through the JSON perfectly, therefore there is no need to really check this. I just ask for your attention in the following lines : (25,43,44,59,60,75,76,101,102,103,114,133,165,166,172,175,183 and 184) these lines are the ones I'm using the methods mostly.

So,as you should see in the deserialized method, as I go through the JSON I'm also filling the lists and printing the values to confirm and be sure things are running as they should. And by the monitor everything goes fine, the odd part begins in the "Print Tests" part over the void setup loop (line 39).

Everytime I try to use a LinkedList get method, my values are not corresponding as they were printed while deserializing. This is blowing my mind. Then I get stuck and can't no longer test the rest of my code and project. I would really appreciate some help, thanks a lot !

**** EDIT
setup_void.cpp

there are some "()' missing in the getEvents and getFeeds methods, in the print test part

MainClass.cpp (946 Bytes)

Feeds.cpp (279 Bytes)

project.h (1.91 KB)

Events.cpp (957 Bytes)

Deserializer.cpp (4.66 KB)

setup_void.cpp (3.94 KB)

Everytime I try to use a LinkedList get method, my values are not corresponding as they were printed while deserializing.

Are all values are not corresponding or just some of them?
Can you post what are you receiving and an example of what is expected?

I cannot see, in your code, where do you call the:
setDay()
setHour()
setMinute()
setSecond()
for the MainClass object, before calling the respective get()s in the "Print Tests".

giova014:

Everytime I try to use a LinkedList get method, my values are not corresponding as they were printed while deserializing.

Are all values are not corresponding or just some of them?
Can you post what are you receiving and an example of what is expected?

I cannot see, in your code, where do you call the:
setDay()
setHour()
setMinute()
setSecond()
for the MainClass object, before calling the respective get()s in the "Print Tests".

The only corresponding values are the values of Day,Hour,Minute and Second, which means these values get methods are just working fine(they are all int type) and thats why I omitted them, my problem lies on linkedlist type get and set method (not entirely sure if linkedlist set method is right, It seems so).

While deserializing all printings are matching and corresponding, but when I call specific print values in the "Print Tests"code part, these values start messing up as soon as I call getEvents() and getFeeds() (More precisely in line 45 of setup_void.cpp.

So considering the given JSON I should receive in the Print test first block the following values :

Serial.println(mainclass.get(0)->getDay()); SHOULD RETURN 1, ITS RETURNING 1.
Serial.println(mainclass.get(0)->getHour()); SHOULD RETURN 14, ITS RETURNING 14.
Serial.println(mainclass.get(0)->getMinute()); SHOULD RETURN 10, ITS RETURNING 10
Serial.println(mainclass.get(0)->getSecond()); SHOULD RETURN 0, ITS RETURNING 0
Serial.println(mainclass.get(0)->getEvents().get(0)->getSetor()); SHOULD RETURN 13, ITS RETURNING 1

Serial.println(mainclass.get(0)->getEvents().get(0)->getDuration()); SHOULD RETURN 180, its returning 0

From here the sketch freezes. But lets proceed with the expected values.

Serial.println(mainclass.get(0)->getEvents().get(0)->getSequencial()); SHOULD RETURN 1
Serial.println(mainclass.get(0)->getEvents().get(0)->getVolumeH2O()); SHOULD RETURN 1.5
Serial.println(mainclass.get(0)->getEvents().get(0)->getFeeds().size()); SHOULD RETURN 1
Serial.println(mainclass.get(0)->getEvents.get(0)->getFeeds().get(0)->getSilo()); SHOULD RETURN Silo 12
Serial.println(mainclass.get(0)->getEvents.get(0)->getFeeds().get(0)->getVol()); SHOULD RETURN 22.356

Edit: Actually, there is the "Implicitly copy assignment operator"

In file "MainClass.cpp", in this block:

MainClass::MainClass(){
    _events = LinkedList<Events*>();
    
}

remove this line:

 _events = LinkedList<Events*>();

you are assigning a local object (LinkedList) to "_events", which will be invalid after the function returns.

In the file "project.h", you are already declaring and initializing object "_events" with this:

class MainClass
{
  ...
   private :
	   ...
	   LinkedList<Events*> _events;
};

That is equivalent to:

class MainClass
{
  ...
   private :
	   ...
	   LinkedList<Events*> _events();
};

Both call the constuctor that takes no parameters.

I should fill the lists with the values by using the set method

I see that you should be able to add a value to a list. That does not require knowing where or how the class maintains the list.

I must also be able to GET the filled list using the get method.

I disagree. You should be able to get the number of items in the list, and to be able to get the nth node from the list (or the data from the nth node would be even better).

Linked lists are lousy data structures on an Arduino. Remember, you only have limited RAM and each node wastes 8 bytes on links alone.

PaulS:
You should be able to get the number of items in the list, and to be able to get the nth node from the list (or the data from the nth node would be even better).

Yes, as you may have seen I'm trying to get the nth node from the list in the print test , am I not? Since you understood the matter, do you know why this is happening? How do I fix it?