2

Using Visual Studio Express2013 for Windows Desktop, with a "Win32 Console Application" C++ project

(I'm doing a project for a course. In order to start, I have to import all of the project files that the instructor provided - there are tons (all .cpp and .h files), so I can't really copy-paste any of them here... but I don't think that's the issue anyway).

When I hit "run debugger," I get nearly 200 syntax errors in math.h Even though math.h is what came with Visual Studio.

All the errors seem really simple like:

"Missing ')' before identifier _X'"
"'_X' : undefined identifier"
"'floor' : definition of dllimport data not allowed"
"syntax error : missing ';' before '+'"


etc... these syntax errors go on into the 200s.

(I would copy-paste the entire error log, but there are 262 errors, and they all have the file directory path in them, so it would be impossible for you guys to read)

Why do I have so many syntax errors in math.h if math.h is a file that comes from Visual Studio itself?

(I have been trying to figure this out for weeks, and I can't actually start working on the project until it compiles)

Frustrated
  • 33
  • 1
  • 5
  • 1
    `math.h` is a C header, use `cmath` instead. –  Jan 31 '15 at 22:04
  • Check your includes, include `math.h` before other stuff (move it to the top of include list). – Roman R. Jan 31 '15 at 22:04
  • `math.h` is a text file that is simply "pasted" into your code by the preprocessor. Something in your own code located above the inclusion point might easily "screw up" the proper handling of the "injected" `math.h`. For example, what you experience is easily achievable by redefining a keyword as a macro. (Also, albeit less likely, incompatible project settings might be to blame here.) – AnT stands with Russia Jan 31 '15 at 22:05
  • My guess would be, you have `double` defined as a macro. Or maybe `float`. – Igor Tandetnik Jan 31 '15 at 22:06
  • 2
    How about a simple program? `#include int main() { double d = sqrt(4.0); }` What does this do? If it compiles, then it is all of those headers that are causing the problem -- it has nothing to do with Visual Studio. – PaulMcKenzie Jan 31 '15 at 22:11
  • 1
    Changed it to cmath.h and moved it to the top - only 16 errors instead of 262 not, but still errors - I'll take a look and get back to y'all. – Frustrated Jan 31 '15 at 22:14
  • 1
    `` is a C header. It's perfectly legal to use it in C++ code, but the C++-specific version `` is preferred (the difference is that `` puts the declarations into the `std` namespace). Changing it to `` probably would reduce the number of errors -- and the first is likely to be that `` doesn't exist; it's ``, *not* `. Your question needs to include the source file you're compiling, preferably a reduced version that reproduces the problem. And you don't have to show us hundreds of lines of error messages, but the first few lines would help. – Keith Thompson Jan 31 '15 at 22:55
  • @Isaiah: It's legal to use `` in C++ code. `` is preferred, but that's not the problem. – Keith Thompson Jan 31 '15 at 22:56
  • @KeithThompson Ok ty, was unsure myself –  Feb 01 '15 at 00:05
  • I see you've accepted the answer that suggests using `` rather than ``. Both header names are valid, though `` is preferred; it's unlikely that using `` actually solved your problem. Please (a) update your question to show us the actual code that caused the problem (your own code, not the `` header), and (b) let us know how you actually fixed it, if you have. – Keith Thompson Feb 01 '15 at 02:51
  • I'm only going by results. I changed all the `math.h` to `cmath` and it compiled and is now working. – Frustrated Feb 01 '15 at 21:13

2 Answers2

2

The problem could be the header itself - C++ has provided its own equivalent libraries for old C libraries. They take the format of:

c[library name]

Where [library name] is replaced by the old C library MINUS the .h extension.

To include math.h from the C library in a C++ program, you would do this:

#include <cmath>

You can also try some of the things the others are stating.

Note: I'm unsure whether the old C headers are the source of the problem, but since C++ did introduce some incompatibilities, this could very well be the problem.

  • Hey, this is what I'm having trouble with now... instead of writing #include , I wrote #include and changed cmath to cmath.h with administrator privilege... That kind of caused a "cascade" because other things rely on cmath, and me changing it to cmath.h messed up those calls... now I'm not allowed to edit those files (i.e. xlocnum won't let me edit it) Is there a way for me to to change cmath.h back to just cmath? – Frustrated Jan 31 '15 at 22:28
  • Remove the .h by showing extensions, right-click on the file and select rename, and pressing yes on the confirmation. –  Jan 31 '15 at 22:30
  • so it turns out the 16 errors of stuff not finding cmath.h must have just been masking my original 260 errors... because now they're all back (and all in math.h even though I'm not using math.h anywhere...) – Frustrated Jan 31 '15 at 22:38
  • Ok, try a full reinstall of VS –  Jan 31 '15 at 22:41
  • I agree... I accidentally deleted math.h too :( – Frustrated Jan 31 '15 at 22:42
  • 1
    … Ok, here's a tip -- Don't mess with system/compiler header and ource files unless there is a VERY particular reason to do so (such as the compiler's support team recommending it to you). –  Jan 31 '15 at 22:43
  • @Isaiah: Changing the name of the file, as you suggested in your earlier comment, is a really bad idea. Now the OP's installation is corrupted. – Keith Thompson Feb 01 '15 at 00:33
  • @Kieth I changed cmath to cmath.h on my own in the first place and I was asking how to change it back - he wasn't the one to suggest me to change it. – Frustrated Feb 01 '15 at 21:10
  • And as an update, with the re-installation, and changing math.h to cmath, everything is working perfectly. – Frustrated Feb 01 '15 at 21:13
0

It appears you might have included some header file before you mentioned #include <math.h>in your cpp file. That header file might have missed ; at end of class/function declaration causing errors in math.h file. For example,

#include "test.h"

#include <math.h>

If test.h has class/data type/function declaration missing ; at end it results errors in math.h

Atul
  • 3,778
  • 5
  • 47
  • 87