constructor problem with declaration of object

In my main.cpp i call the constructor of a class.In the constructor i set the variables of his class from some static functions. All the problem is about setting time from NTP server.

The static functions which are a getters use a while loop in order to check a flag.The flag is false if time is not set and true if at least one time the NTP has responded.If the flag is false the while loop has to try to get time forever (cause there is no reason to continue without time)and if it is true it returns the time.

The constructor sets the variables as i said by calling the getters.

When i declare the object of that class in main.The program does not run cause i get nothing on serial print.If i commented the declaration it works.Also it does not act like stucking inside the while cause i placed a serial print inside there and it did not appear also.I give the code below:

the constructor:

 #import "DayTime.h"
    #include "NTP.h"
    
    DayTime::DayTime(){
    	hour = NTP::getLCThour();
    	minutes = NTP::getMin();
    	seconds = NTP::getSec();
    }

the main.cpp

#include <Arduino.h>
    #include <SPI.h>
    #include "NTP.h"
    #include "DayTime.h"
    #include "DayNumber.h"
    
    DayTime DT;
    DayNumber DN;
    
    int main(void)
    {
    	init();
    
    	setup();
    
    	for (;;)
    		loop();
    
    	return 0;
    }
    
    void setup() {
    	Serial.begin(9600);
    	NTP::startEthernetAndUdp();
    
    }
    
    void loop() {
    
    	NTP::getTime();
    //	Serial.println(DT.hour);
    //	Serial.println(DT.getTimeToDec());
    	delay(3000);

here the getTime function works if i comment the declaration of the DayTime object named DT.

The NTP.cpp parts:

  1. the first getter:
int NTP::getLCThour(){
	while(timeNTPSet==false){
		Serial.print("kolaw");
			getTime();
		}
	return lcthour;

}

  1. the declaration of the flag on header:

static bool timeNTPSet;

this is public variable

3)its definition at the start of the .cpp file

bool NTP::timeNTPSet= false;

this is set in .cpp after the include.

And at last part of the function that sets the flag to true:

NTP NTP::getTime(){
    
    	if (testNtpServer()){
    		timeNTPSet = true;
    		// We've received a packet, read the data from it
    		Udp.read((unsigned char*)packetBuffer,NTP_PACKET_SIZE);

the testNTPServer function just test 3-4 different NTP servers in order to take time from at least from one.Also it works fine if i comment the declaration of the DayTime object as i said before.

The getTime function inside loop has serial prints and this function has nothing to do with the DayTime object.So i should run normally.
If i declare the object inside the loop like this

DayTime DT=DayTime();

the program works fine but i do not want to create an object in every loop.
Any help?

 #import "DayTime.h"

Say what?

3)its definition at the start of the .cpp file

Code:

bool NTP::timeNTPSet= false;

this is set in .cpp after the include.

That declares a new variable. It does not initialize the existing one.

If i declare the object inside the loop like this

Code:

DayTime DT=DayTime();

the program works fine but i do not want to create an object in every loop.

That's not correct. You never invoke the constructor directly.

yes ok about import. Can you give me solutions to the other 2 problems you found?

How do i initialize a static variable in cpp?

Also i know that when you make an object you type the name of the class then the name of the object and then you make it equal to the constructor.

Well except these 2 probs? what is the problem with the program cause these problems you describe does not stop the program to work correctly if i comment the declaration of the DayTime object

i did some research and i found another opinion that says that this way of declaration is correct:

bool NTP::timeNTPSet= false;

answer me plz you said that this way i declared and initialized another variable not the static that i declared in the header file.

answer me plz you said that this way i declared and initialized another variable not the static that i declared in the header file.

The bool at the beginning of that statement declares a new variable. Loose it to initialize the existing variable.

kyrpav:
i did some research and i found another opinion that says that this way of declaration is correct:

bool NTP::timeNTPSet= false;

It's correct (it defines and initialises the variable) if you only do it in one place.

if i loose that in the beginning i get error at the compiler.

Read this before posting a programming question

Please read point 6, in particular "Post your complete sketch (program code)! " and " if possible post a "minimal" sketch that demonstrates the problem - not hundreds of lines of code.".

In other words, make a small sketch that should compile (you believe), however doesn't.

Not dozens of snippets with a lot of chat in between. Then we can show what line to change to make it work.