Expeceted ; when I have one

Hi Sorry I this may seem like a stupid question but I am a beginner in C++, I really don't knonw where this error is coming from because at the previous line of code from the error line there is a ;.
Can you please explain to me why I am seeing this error at line "Date_SetDay_v(DATE_pst, DAY_u8);" and also the two line below it.
Thank you a lot for your help in this issue.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58

#include "../common/Typedefs.h"
#include "Date.h"
#include <string>
#include <ctime>
// Macro Date_SetDay_v(): Sets the day of the month of a date object
#define Date_SetDay_v (DATE_pst, DAY_u8)\
{\
time_t now;\
struct tm DATE_pst;\
now=time(NULL);\
DATE_pst = *localtime(&now);\
DAY_u8 = DATE_pst.tm_mday;\
cout<<"Today's day of the month is:"<<DAY_u8<<"."<<endl;\
cin.get();\
return0; \
}





// Macro Date_SetMonth_v(): Sets month of a date object
#define Date_SetMonth_v(DATE_pst, Month_u8)\
{\
time_t now; \
struct tm DATE_pst; \
	now = time(NULL); \
	DATE_pst = *localtime(&now); \
	Month_u8 = DATE_pst.tm_mon+1; \
	cout << "Today's month is:" << Month_u8 << "." << endl; \
	cin.get(); \
	return0; \
}
// Macro Date_SetDay_v(): Sets year of a date object
#define Date_SetYear_v(DATE_pst, YEAR_u16)\
{\
time_t now; \
struct tm DATE_pst; \
	now = time(NULL); \
	DATE_pst = *localtime(&now); \
	YEAR_u16 = DATE_pst.tm_year+1900; \
	cout << "Today's month is:" << YEAR_u16 << "." << endl; \
	cin.get(); \
	return0; \
}
int main()
{
	time_t now;
	struct tm DATE_pst;
	u8 DAY_u8 = DATE_pst.tm_mday;
	DATE_pst = *localtime(&now);
	u8 Month_u8 = DATE_pst.tm_mon + 1;
	u16 YEAR_u16 = DATE_pst.tm_year + 1900;
	Date_SetDay_v(DATE_pst, DAY_u8);
	Date_SetMonth_v(DATE_pst, Month_u8);
	Date_SetYear_v(DATE_pst, YEAR_u16);
}
What doese return0 mean in this context? Why do you use macros instead of real functions?

Macros do have all kinds of side effects. If you mean return 0 (note the blank) it would jump out of main (or where ever it is called).
I have tried to remove all the retun 0; lines but that didn't make any difference either, I use macros because that is the requirement of this assignment. Thank a lot for your comment.
Please post the entire error message (with the correct line numbers) since it is not compilable.
I use macros because that is the requirement of this assignment

I think we also need those two headers, “Typedefs.h” and “Date.h”, together with your assignment text.
It doesn’t make much sense to me to include a “Date.h” header to define date related functions by macros, so I’d like to read the original assignment.
Anyway, the first half of your code is the clone of the first half of the macro “Date_SetYear_v”, but that macro looks like it is supposed to accept parameters.
Maybe I don’t understand what you’re doing, that’s why I’d like to check the requirements.
Apart from inclusion guards my C++ code does not need macros often. I do not recall defining a parametrized macro. Ever. Appears doable though:
1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

#define foo(bar, gaz)\
bar = gaz

int main()
{
  int answer {};
  foo(answer, 42);
  std::cout << answer << '\n';
}

Nevertheless, to me macros are "an advanced topic". If they are "advanced", then they would be studied after mastering "regular" C++. The code in OP, however, does not look "regular, mastered". More like gibberish.

Reuse of names definitely does not help.

I do presume that the preprocessor, when encountering a macro, substitutes parameters to macro and then macro to code. For example:
1
2
3
4
#define foo(bar, gaz) bar = gaz

// use
foo(answer, 42);

evolves to:
1
2
3
4
#define foo(answer, 42) answer = 42

// use
foo(answer, 42);

and finally:
1
2
// use
answer = 42;


How should we explain that process for a code, where "DATE_pst" is name of variable in actual code, name of parameter of a macro, name of local variable within the code of a macro, ...
Hello petrean,

As coder777 has mentioned you need to post the whole code. This means the header files "Typedefs.h" and "Date.h". Without them the code will generate more errors than it needs to and there is no way to test the program.

I also noticed your macros end with return0;. I think you meant return 0; with a space. Careful though your macro shows no return value, but you are trying to return an int. This may cause an error. A return statement in a void f does work, but is not necessary for the function to return when its finished.

To help the people who are helping you it is best to include all the code, header files and complete error messages that you have. You do not waste time going back and forth with questions that could have been answered with the complete code.

Hope that helps,

Andy
macros with ; in them can be troublesome. The fastest way to fix this IMHO is to make a new program with nothing but your macros. Call your macros one by one in the new program and ensure each one works using the same syntax you wanted to use in the main program.
Hello petrean,

Something you may not be understanding or understanding well is how your "#define"s are working. Along with a thought that really caught my attention after seeing jonnin's response.

Given the code:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

#define CLS mstd::cls()  // <--- Note no semi-colon here.

int main()
{
	CLS;  // <--- Semi-colon used here.

	 // <--- Other code.

	return 0;
}


When you compile the code, and if I understand correctly, the preprocessor will make note of the "#define" and when it finds something after the "#define" it will replace it before the compiler compiles the code.

So what the compiler actually sees when the preprocessor is done is:
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

#define CLS mstd::cls()  // <--- Note no semi-colon here.

int main()
{
	mstd::cls();

	 // <--- Other code.

	return 0;
}


Now what your program is doing is defining a complete function using the "#define" and when you reach a line like: Date_SetDay_v(DATE_pst, DAY_u8); you are defining an inline function in your code replacing Date_SetDay_v(DATE_pst, DAY_u8) with the entire function and ending the function with a semi-colon that you do not need.

If you error message refers to a line of code in main it is more likely referring to something in the macro that was expanded and that you do not see.

I have not noticed any missing semi-colon in the functions. There is the "return0;" that is missing a space and is not needed in the first place and that you end up with a semi-colon after the closing brace } of the function that is not needed.

Sorry without the missing header files there is no way to test this without completely rewriting the program.

Hope that helps,

Andy
If we replace your define references with what the compiler substitutes, we get the following.

We're lucky we can just cut and paste since the argument names are the the sames as your variables. This is however a poor practice since it can be confusing to the reader.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//  #include "../common/Typedefs.h"
//  #include "Date.h"
#include <string>
#include <ctime>
#include <iostream> 
using namespace std;

int main()
{
    time_t now;
    struct tm DATE_pst;
    int DAY_u8 = DATE_pst.tm_mday;
    DATE_pst = *localtime(&now);
    int Month_u8 = DATE_pst.tm_mon + 1;
    int YEAR_u16 = DATE_pst.tm_year + 1900;
    //  Date_SetDay_v(DATE_pst, DAY_u8); 
    {\
        time_t now;\
    struct tm DATE_pst;\
        now = time(NULL);\
        DATE_pst = *localtime(&now);\
        DAY_u8 = DATE_pst.tm_mday;\
        cout << "Today's day of the month is:" << DAY_u8 << "." << endl;\
        cin.get();\
        return0; \
    };
    //  Date_SetMonth_v(DATE_pst, Month_u8);
    {\
        time_t now; \
    struct tm DATE_pst; \
        now = time(NULL); \
        DATE_pst = *localtime(&now); \
        Month_u8 = DATE_pst.tm_mon + 1; \
        cout << "Today's month is:" << Month_u8 << "." << endl; \
        cin.get(); \
        return0; \
    };
    //  Date_SetYear_v(DATE_pst, YEAR_u16);
    {\
        time_t now; \
    struct tm DATE_pst; \
        now = time(NULL); \
        DATE_pst = *localtime(&now); \
        YEAR_u16 = DATE_pst.tm_year + 1900; \
        cout << "Today's month is:" << YEAR_u16 << "." << endl; \
        cin.get(); \
        return0; \
    };
}


Note that lines 25 and 36 do not make sense. if
return0;
really should be return 0; or is a macro which expands to return 0;, your program will exit unconditionally at line 25. If
return0
is not defined as a macro, you will get compile errors because the compiler is expecting a space between return and 0.

Line 47 has the same problem, except that a return 0; here is harmless.
Last edited on
man gcc wrote:
-E Stop after the preprocessing stage; do not run the compiler proper. The output is in the form of preprocessed source code, which is sent to the standard output.
that means we get to see the macros expansion

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
int main() {
	time_t now;
	struct tm DATE_pst;
	u8 DAY_u8 = DATE_pst.tm_mday;
	DATE_pst = *localtime(&now);
	u8 Month_u8 = DATE_pst.tm_mon + 1;
	u16 YEAR_u16 = DATE_pst.tm_year + 1900;
	(DATE_pst, DAY_u8) {// ¡!
		time_t now;
		struct tm DATE_pst;
		now = time(NULL);
		DATE_pst = *localtime(&now);
		DAY_u8 = DATE_pst.tm_mday;
		cout << "Today's day of the month is:" << DAY_u8 << "." << endl;
		cin.get();
		return0;
	}
	(DATE_pst, DAY_u8); // ¡!
	{
		time_t now;
		struct tm DATE_pst;
		now = time(NULL);
		DATE_pst = *localtime(&now);
		Month_u8 = DATE_pst.tm_mon + 1;
		cout << "Today's month is:" << Month_u8 << "." << endl;
		cin.get();
		return0;
	};
	{
		time_t now;
		struct tm DATE_pst;
		now = time(NULL);
		DATE_pst = *localtime(&now);
		YEAR_u16 = DATE_pst.tm_year + 1900;
		cout << "Today's month is:" << YEAR_u16 << "." << endl;
		cin.get();
		return0;
	};
}

note that the parameters of the macro in SetDay were not substituted
if you compare it against the other two, you'll notice an extra space
1
2
3
#define Date_SetDay_v (DATE_pst, DAY_u8)\
#define Date_SetMonth_v(DATE_pst, Month_u8)\
#define Date_SetYear_v(DATE_pst, YEAR_u16)\ 



> I use macros because that is the requirement of this assignment
there is something fundamentally wrong with your class then.
Topic archived. No new replies allowed.