-3

I am trying to solve this exercise on Exercism website. The exercise is about finding the leap year. I have two files, main one and the header file.

The main one:

#include "leap.h"



int main (){

leap::is_leap_year(int year);

}

The header file:

    #if !defined(LEAP_H)
    #define LEAP_H
    #include <iostream>
    #include <string>
    
    namespace leap {
    bool is_leap_year(int year) {
    bool status=1;
    if (((year%4 == 0)&& (!(year%100==100))) || ((year%100==0)&&(year%400==0))){
    
    return status;
    }
    else{
        status = 0;
       
     return status;
    }
    }
}  

#endif // LEAP_H

Whenever I run this code I keep getting this error: expected primary-expression before 'int'

If I removed int from

leap::is_leap_year(int year);

I will get this error: 'year' was not declared in this scope

cigien
  • 57,834
  • 11
  • 73
  • 112
Wadah
  • 23
  • 5
  • Is there a good reason you're not using `true/false` instead of `1/0` here? `bool status=1;` and `status = 0;` You might also think about marking the function in the header `inline` or declaring it in the header and defining it in a cpp file. If you don't you'll have trouble if you include that header in multiple files. – Retired Ninja May 06 '22 at 22:13

1 Answers1

0

Learn about the differences between function declaration, definition and call. As for your question what you're doing wrong here is that you're not passing any value to your function which it expects. Instead you're just declaring an int and hence the error. Declare and initialise the value before passing it to the function.

int year;  //declare before using it
year = 20;
leap::is_leap_year(year); //passing a value
avm
  • 387
  • 3
  • 16
  • I did the edits you recommend and I got the following error "multiple definition of `leap::is_leap_year(int)'; " By the way I was curious of why my post get downvoted? Did I do something wrong? This is my first time posting here and I am not familiar with etiquette. – Wadah May 08 '22 at 03:14
  • @Wadah Maybe you're defining the function multiple times. That's what the error is telling at least. I have rewritten your code in on file [here](https://www.ideone.com/9mp5pI). Also tried by making a separate header file and works fine. Have a look at the code and maybe you can figure out what you're doing wrong. – avm May 08 '22 at 14:37
  • For information on how to ask good questions here so you don't get downvoted see [here](https://stackoverflow.com/help/how-to-ask) – avm May 08 '22 at 14:37
  • Thank you so much for your help and guidance. I can see that your code is working on my IDE but when I tried in in Exercism website it throws bunch of errors again such as ( In function 'void ____C_A_T_C_H____T_E_S_T____0()': /tmp/leap/leap_test.cpp:10:14: error: 'leap' has not been declared) I believe these errors are related somehow to the test cases the website is using. My guess is that I should write my code in a certain way so that the website can test it. Anyhow I am still new in my journey and will come back to this problem after few weeks and see if I can figure it out – Wadah May 08 '22 at 23:45
  • Thanks for the link. I will do my best to follow these tips – Wadah May 08 '22 at 23:46
  • @Wadah Coding sites have a set of predefined instructions for writing a solution so general solutions may not work. That website only wants you to implement the function and calling is done automatically. So you only need to declare and define the function but not call it. See my [solution](https://exercism.org/tracks/cpp/exercises/leap/solutions/avm-dev). – avm May 09 '22 at 05:42
  • @Wadah Declarations are done in header files(files with .h extension) and definition/implementation is done in separate file(.cpp file). Here are some links to help you understand better about declaration and implementation- [Link1](http://www.cppforschool.com/tutorial/separate-header-and-implementation-files.html) [Link2](https://stackoverflow.com/questions/280033/c-header-files-code-separation/280048#280048) – avm May 09 '22 at 05:45
  • 1
    Thank you so much for all your help. I do really appreciate it. I almost quit using this site while the issue was really simple. again I appreciate the time and the effort you spent here – Wadah May 09 '22 at 14:16