Borland Turbo C++ 3.1 "Declaration syntax error"

Hello!
I am currently working on a game for MS-DOS, but I ran into a "Declaration syntax error" while defining a class in a seperate file... The error occurs in the header file.

1
2
3
4
5
6
7
8
9
10
11
12
#ifndef MAP_H
#define MAP_H

class Map {
    public:
        int indexX, indexY;
        Map();
        void generate();
        
};

#endif // MAP_H 


Sorry if it is obvious!

If you need either MAIN.CPP or MAP.CPP, I can post them too.

Ans bevore someone tells me that Turbo C++ is outdated, I know, but loke is said before, i am making a game for MS-DOS so i cannot use something newer.

Edit: Forgot to tell that the error happens in the line
class Map {
Last edited on
Can you copy and paste the exact, verbatim error message with all information it might include?

MS-DOS? As in the operating system from the 80s? I must ask, why torture yourself by making a game for an OS that 99.9999% of people no longer have?

And yes, you might as well paste your other files as well.

What you should do if you want the best chance of us being able to help you is make a complete, minimal example (preferably, only 1 file) that reproduces the same error in the fewest lines of code possible.

__________________________________

My guess is a syntax error in the place above where you #include "Map.h"
Last edited on
So, first of all, the complete error message I get from the compiler is:
•Error MAP.H 4: Declaration syntax error

2nd, I am making it for MS-DOS just for fun. I like seeing my programs working on 20 years old machines... Dont ask further about this xD

3rd, here is MAP.CPP:
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
#include <stdio.h>
#include <stdlib.h>
#include <iostream.h>
#include <conio.h>

#include "common.h"
#include "map.h"            //DOS filenames were not case sensitive

int indexX = 0, indexY = 0;

Map::Map() {

}

void Map::generate() {
    FILE* mapFile = fopen(/*path to file*/);

    char TMP;

   while(TMP != EOF) {
        TMP = fgetc(mapFile);

        if(TMP == '\n') {
            indexY++;
            indexX = 0;
        } else {
            g_map[indexX][indexY] = TMP;
            indexX++;
        }
    }
}


where /*path to file*/ is, is actually a path

COMMON.H:
1
2
3
4
5
6
#ifndef COMMON_H
#define COMMON_H

extern char g_map[36][48];

#endif // COMMON_H 


And MAIN.CPP is about 1000 lines long and mist of it is actually the main menu, but the part that is important:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include "common.h"
#include "map.h"

//Menu function declaration

int main() {
    //All of the startup and menu stuff

    //After some menu stuff and keypress checks
    switch(selectedMenuItem) { //declared
        case '1':                                 //actually works with chars...
            map.generate();
            break;

        //more cases
    }

    //rest of the stuff
    return 0;
}


Hope i dont have to write the whole MAIN.CPP out on mobile... my DOS machine does not have an Internet connection...
And i tried with a small sample program, same error...
No, I'm sure you don't have to post *everything*.

Sorry, I assumed the compiler would give at least somewhat of a better error message. I overestimated :P... I'm not that knowledgeable of Turbo-C++, by the way. But still figured I'd reply because I doubt you'd get a timely solution either way.

I'm not seeing anything immediately wrong with what you've posted. 'Declaration syntax error' can apparently mean a variety of different problems, but it seems most likely that it means you missed a semi-colon or comma or something, or otherwise incorrectly declared a variable. Or, it could be a mismatched {, }, #if, #ifndef, #endif, etc.

Can you show the lines around where you declare your map object?
Also, where you define your actual g_map (not just the extern declaration).

Unrelated, but on line 9 int indexX = 0, indexY = 0;
Maybe you already know this, but these variables have nothing to do with your class variables.

_____________________________

Furthermore, I want to re-iterate: You said you had a lot of lines of code. I sincerely suggest making a new project (so you don't screw up what you currently have), copying your current code into it, moving all your code into one file, and remove all irrelevant logic and lines of code that don't affect the appearance of your error message. This will immensely help narrow down the issue.

Remove some lines of code, try to compile. Remove some lines of code, try to compile. etc, etc.

If I'm correct, you should be able to whittle down your program to just be two dozen or so lines.



EDIT: I saw your other post
And i tried with a small sample program, same error...

Post the sample program, please.
Last edited on
So, In the MAIN.CPP if you mean that:
1
2
3
4
5
Map map;

//later on

map.generate();


also after a bit of research, it could be that the .H file is somehow recognized from the compiler as a .C file...

Also i found out that the "Declaration syntax error" occurs, when Turbo C++ does not know that Datatype. But the class datatype is listed in the help index in turbo c++

Edit: corrected typos in this post
Last edited on
Sorry I was editing my post a lot as well to clarify things.

Yeah your map variable declaration looks correct, it was another guess.

Could you post your small sample program that you made? Maybe I could try compiling on virtual machine... Also, I don't know what Datatype is, mind clarifying?
Last edited on
Some examples for what I mean by datatype: int, char

and my Sample code looks like this:

MAIN.CPP:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream.h>
#include <conio.h>

#include "common.h"
#include "test.h"

Test cTest;
char g_test[2][2];

int main() {
    cTest.getTest();
    cout << g_test[0][0] << endl;    //I know, not the most efficient
    cout << g_test[1][0] << endl;
    cout << g_test[0][1] << endl;
    cout << g_test[1][1] << endl;
    char UNUSED = getch();
    UNUSED = UNUSED;    //If I dont do this, Turbo C terminates with a variable not used warning
    return 0;
}


COMMON.H:
1
2
3
4
5
6
#ifndef COMMON_H
#define COMMON_H

extern char g_test[2][2]; //to have similar conditions like my game

#endif // COMMON_H 


TEST.H:
1
2
3
4
5
6
7
8
9
10
#ifndef TEST_H
#define TEST_H

class Test {   //Error is here
    public:
        Test();
        void getTest();
};

#endif // TEST_H 


and last but not least, TEST.CPP:
1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream.h>

#include "common.h"
#include "test.h"

Test::Test() {

}

void Test::getTest() {
    g_test[0][0] = '1'; //Just to set some data
    g_test[0][1] = 'd';
}


Also it is almost 12am or midnight in germany, the next time that i would reply would be in about 7-9 hours... just so you know

I just read the edit... The g_map is declared in MAIN.CPP and declared as extern in COMMON.H
Last edited on
So, the last thing I've done before i go to sleep, i tried to compress the sample code into one file:
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
#include <iostream.h>
#include <conio.h>

char g_test[2][2];

class Test {
    public:
        void getTest() {
            g_test[0][0] = '1';
            g_test[0][1] = 'd';
        }
};

Test test;

int main() {
    test.getTest();

    cout << g_test[0][0] << endl;
    cout << g_test[1][0] << endl;
    cout << g_test[0][1] << endl;
    cout << g_test[1][1] << endl;

    char UNUSED = getch();
    UNUSED = UNUSED;
    return 0;
}


And that worked fine!

so the problem has to do with the header files...
i will research more later
Last edited on
TL;DR --- I could not figure out why you are getting that error.

I... partially figured it out. But not completely.
See the post below this one.
Ignore the rest of this post, it isn't helpful.


I followed this guide http://www.horstmann.com/ccc/help/tc30dos.html

I was getting stupid linker errors for a while, but then added random spacing at the ends of files, according to a suggestion on StackOverflow.

I finally now have the same error as you!
... And wow, this is atrocious. Not your code, just... Turbo C++.
________________________________________________

This user had the same error as you, under similar circumstances.
https://www.reddit.com/r/learnprogramming/comments/7egrzy/declaration_syntax_error_in_turbo_c/
He said the problem is that Turbo C++ is detecting the .H file as a C file instead of a C++ file. But that doesn't make sense, because .H files aren't compiled, they are included in the .CPP files that are then compiled. And Turbo C++ should know that .CPP means C++.

I went to Options -> Compiler -> C++ Options -> [check] C++ always, but that changed nothing. Maybe still try it though.

Instead of doing "Build All" I tried to compile TEST.CPP and MAIN.CPP separately. "Compile" works fine, but then Make or Link produce Linker errors for me, complaining,
"Linker Error: Test::getTest() defined in module TEST.CPP is duplicated in module TEST.CPP"
"Linker Error: Test::Test() defined in module TEST.CPP is duplicated in module TEST.CPP"

I have no idea how to fix those... and googling is failing me. Sorry.
Last edited on
I THINK I FOUND IT (edit: well, partially)
DON'T ADD THE ACTUAL .H FILES TO THE PROJECT
(This sort of makes sense, since .H files aren't actually compiled.)
(It's still dumb and I hate Turbo-C++ more than I did before, but it was kinda nostalgic...)

I made a NEW project, and only added the 2x *.CPP files.
THEN, open COMMON.H (but don't add it to your project), and change "extern" to "static"

But now, since it's static instead of extern, don't re-define it in main.cpp.
Or... define it as extern in main.cpp?
Okay, at this point I'm lost. There's some possibly non-standard syntax going on that makes declaring it extern bad inside the header file, or something. When I run the program, it doesn't print the characters, but does print a "Hello" I added.

But hey, at least we've narrowed down the problem to be being non-standard extern behavior. Maybe you can google that.

I'm going to bed.


See this Reddit thread, very helpful:
https://www.reddit.com/r/learnprogramming/comments/7ekhxn/cant_get_the_files_in_a_project_to_link_in_turbo_c/
https://www.reddit.com/r/learnprogramming/comments/7ekhxn/cant_get_the_files_in_a_project_to_link_in_turbo_c/dq5sxou/
Last edited on
Thank you for the help! :D
I think the variable should be fine, but now even though it compiles, it crashes MS-DOS and with the debugger it says it is in my mainMenuDraw function... I think i let MS-DOS go for quite some time and go back to Windows.

Greetings TimCoding
Topic archived. No new replies allowed.