Go Down

Topic: Class declaration problem with constructor (Read 347 times) previous topic - next topic

kyrpav

I want to calculate the days that passed in a year since a certain date. I made a class DayNumber.
i made this header file

Code: [Select]
#ifndef DayNumber_H_
#define DayNumber_H_


class DayNumber
{
private:
int _day1YearLoop[];
int _day4YearLoop[];

public:
int Days1YearLoop;
int Days4YearLoop;

DayNumber(int setYear,int setMonth,int setDay);
virtual ~DayNumber();

bool checkLeapYear(int setYear);

};

#endif


i use eclipse. and i made this .cpp file.

Code: [Select]
#include "DayNumber.h"


DayNumber::DayNumber(int setYear,int setMonth,int setDay){

// it does not matter what it does it just give me the number of the day since 1 january of each year for a certain day.


}


DayNumber::~DayNumber(){
}


bool DayNumber::checkLeapYear(int setYear){
if (setYear%4==0){
return true;
}
else{
return false;
}
}


the problem is at the main.cpp. When i do:

Code: [Select]
void loop() {

  DayNumber num1 = DayNumber(2715,10,23);

  Serial.print("Days for one Year Loop(leap)\t");
  Serial.print(num1.Days1YearLoop);
 
  delay (5000);
}


everything is ok. if i try to declare DayNumber num1; befroe everything. i mean before setup like this:

Code: [Select]
#include "Arduino.h"
#include "DayNumber.h"

DayNumber num1;

int main(void)
{
init();
setup();

for (;;)
loop();

return 0;
}

void setup() {               

  Serial.begin(9600);
}

void loop() {

  DayNumber num1 = DayNumber(2715,10,23);

  Serial.print("Days for one Year Loop(leap)\t");
  Serial.print(num1.Days1YearLoop);


  delay (5000);
}


i get this error:
../src/main.cpp:8: error: no matching function for call to 'DayNumber::DayNumber()'
/Volumes/Lion 2/EclipseWorks/DayNumber/lib/DayNumber.h:22: note: candidates are: DayNumber::DayNumber(int, int, int)
/Volumes/Lion 2/EclipseWorks/DayNumber/lib/DayNumber.h:12: note:                 DayNumber::DayNumber(const DayNumber&)
make: *** [src/main.o] Error 1


line 22 at header is DayNumber(int setYear,int setMonth,int setDay);
and line 12 is class DayNumber{

so why is this happening? i did everything by the tutorial of from the site and i mean arduino for making libraries. I just wanted to define an "object" of this class to use it in loop.i have made an RTC (real time clock) and i am also using its library that does the same ( defines the object of RTC class before everything) and it does not give any problem. Can you give me a solution so i can declare the object without initialization  and then call it inside loop and initialize it?

Cybernetician

#1
Jan 12, 2013, 03:12 pm Last Edit: Jan 12, 2013, 03:18 pm by Cybernetician Reason: 1
try that

Code: [Select]

DayNumber num1 = DayNumber(2715,10,23);

int main(void)
{
init();
setup();

for (;;)
loop();

return 0;
}


Problem: way of  declaration.

if you really like that way declaration DayNumber num1;

then add one more constructor.
Code: [Select]

DayNumber(int setYear,int setMonth,int setDay);
DayNumber();

From Idea To Invention

PaulS

The DayNumber class needs a constructor that takes no arguments, and a method that takes the arguments of interest, and returns the value of interest.

Chaul

It should be possible to give default values to the arguments in the constructor and then it would at least pass compilation, but I doubt this is what you wanted either.
DayNumber(int setYear = 2715,int setMonth = 10,int setDay = 23);

Go Up