I asked this question earlier and you told me to mention the errors,
so I will mention them now(I couldn't figure out how to continue the last thread I started,all I saw was an "add a comment" button and an "answer your question" button, so I had to correct the questio and ask it again, sorry about that):
My question is the following:
I'm using visual studio 2010 and I'm writing a win32 application(not a console application).
I need to know how to write to a file from this application.
I included these headers: windows.h, stdlib.h, string.h and tchar.h
I wrote a very simple hello world application and it ran just fine.
But when I tried to include iostream and fstream
in my project the compiler gave me the following errors.
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2039: 'abort' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(21): error C2873: 'abort' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2039: 'exit' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib(24): error C2873: 'exit' : symbol cannot be used in a using-declaration
IntelliSense: the global scope has no "abort" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 21 13
IntelliSense: the global scope has no "exit" c:\program files (x86)\microsoft visual studio 10.0\vc\include\cstdlib 24 13
And when I include fstream.h,I get:
error C1083: Cannot open include file: 'fstream.h': No such file or directory c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
IntelliSense: cannot open source file "fstream.h" c:\users\user\documents\visual studio 2010\projects\helloworld\helloworld\main.cpp 5 1 helloworld
Same thing for iostream.h
Why are these errors occurring?
In C++, you should use <cstdlib> instead of <stdlib.h>, <cstring> instead of <string.h> (assuming you mean the C style strings. If you want C++ std::string, use <string> [without .h].
And you should use <fstream>, not <fstream.h>.
Please check that your :
#include "stdafx.h"
is the first include of you .cpp file.
Write other includes after this one :
#include "stdafx.h"
#include <iostream>
// ... and so on
int main(...) {
}
... this is a quite frequent mistake with Microsoft compilers (see C++ cout gives undeclared identifier).
Probably you wrote #include "iostream" instead of #include <iostream>
May be this helps .. Try to Check your errors reasons from
http://msdn.microsoft.com/en-us/library/et4zwx34%28v=vs.80%29.aspx
Related
I have a project in Visual Studio that is set up with the following architecture:
src
main.cpp
math.cpp
include
math.h
math.h contains function prototypes for the functions found in math.cpp. When using #include "math.h" after adding include\ to the project's C/C++ Additional Include Directories, I find the compiler is not able to find the standard library. I get several errors like this:
...
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(261,18): error C2039: 'sinhf': is not a member of '`global namespace''
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(261,23): error C3861: 'sinhf': identifier not found
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(265,18): error C2039: 'sqrtf': is not a member of '`global namespace''
1>C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Tools\MSVC\14.31.31103\include\cmath(265,23): fatal error C1003: error count exceeds 100; stopping compilation
Of course, I can successfully include include\ by using #include "..\include\math.h" but I know there must be a way to tell Visual Studio to look in \include for files to include. Any ideas on what I am doing incorrectly?
In my simple OpenGL program I get the following error about exit redefinition:
1>c:\program files\microsoft visual studio 8\vc\include\stdlib.h(406) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\gl\glut.h(146) : see declaration of 'exit'
I'm using Nate Robins' GLUT for Win32 and get this error with Visual Studio 2005 or Visual C++ 2005 (Express Edition). What is the cause of this error and how do I fix it?
Cause:
The stdlib.h which ships with the recent versions of Visual Studio has a different (and conflicting) definition of the exit() function. It clashes with the definition in glut.h.
Solution:
Override the definition in glut.h with that in stdlib.h. Place the stdlib.h line above the glut.h line in your code.
#include <stdlib.h>
#include <GL/glut.h>
or this...
To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons.
EDIT: Solved! See bottom.
I am trying to make my C++/OpenGL game compatible with windows XP.
Currently I am using Visual Studio 2013 express. I followed the instructions here and changed the Platform Toolset to v120_xp, but now I am getting errors in winbase.h and some other files.
My windows specific includes look like this:
#include <Windows.h>
#include <tchar.h>
#include <strsafe.h>
#include <direct.h>
#include <sys/types.h>
#include <dirent.h>
Here are some of the errors I am getting:
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\winbase.h(6133): error C2226: syntax error : unexpected type 'LPSTR'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\winbase.h(6141): error C2226: syntax error : unexpected type 'LPWSTR'
1>C:\Program Files (x86)\Microsoft SDKs\Windows\v7.1A\include\stralign.h(113): error C2065: 'wcscpy_instead_use_StringCchCopyW_or_StringCbCopyW' : undeclared identifier
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cstdio(46): error C2039: '_gets_instead_use_StringCchGetsA_or_StringCbGetsA' : is not a member of '`global namespace''
1>C:\Program Files (x86)\Microsoft Visual Studio 12.0\VC\include\cstring(20): error C2039: 'strcat_instead_use_StringCchCatA_or_StringCbCatA' : is not a member of '`global namespace''
There are many other similar errors.
I would provide more code but this is quite a large project. If there is anything specific you need let me know. Any help would be appreciated!
Solution:
I figured it out when following Jerry's advice.
In one of my files I was including tchar.h and strsafe.h before Windows.h
Including Windows.h above them solved the problem.
I believe have dug myself into a hole. While breaking up my single file, couple thousand line chunk of code into multiple files I appear to have grossly screwed up the structure of the program.
I'm new to C++ and its header files and namespace management so I've been learning as I go.
I guess I don't fully understand how #include, using, and namespaces interrelate and what transfers over to other files, etc.
Reading through MSDN documentation I can see bits and pieces of my problem but the solution eludes me.
As of now I have four .cpp files and headers:
1) A Main File
2) A GameData .cpp and its header that is contained in the namespace pData
3) A GameSettings .cpp and its header that is contained in the namespace pSettings
4) A GeneralScreens .cpp and its header that is contained in the namespace pScreens
Upon compile the debugger spews over 100 of error like:
>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2039: 'pair' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(463): error C2955: 'pScreens::std::pair' : use of class template requires template argument list
1> c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(173) : see declaration of 'pScreens::std::pair'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(33): error C2873: 'wcsrtombs' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2039: 'wctob' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(41): error C2873: 'wctob' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemchr' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemchr' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcmp' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcmp' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2039: 'wmemcpy' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(42): error C2873: 'wmemcpy' : symbol cannot be used in a using-declaration
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\cwchar(43): error C2039: 'wmemmove' : is not a member of '`global namespace''
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2039: 'nothrow_t' : is not a member of 'std'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(93): error C2143: syntax error : missing ',' before '&'
1>c:\program files (x86)\microsoft visual studio 10.0\vc\include\new(99): error C2039: 'new_handler' : is not a member of 'std'
Are these errors indicative of something?
I'm assuming something is wrong with the way I'm referencing the std library though I have no idea how to pinpoint the error because all the errors are in library files.
Most likely you're missing a semicolon on the end if a class or struct definition in a header file. The compiler then treats every identifier it sees as an instance of that class instead of what you're expecting. Then the compiler complains because it has no idea what to do with the stray {}s it sees.
Check the header that's #included just before <utility> in your implementation file; that header is most likely where the fault lies.
Side note: To avoid this problem, always #include standard headers before your own headers in a .CPP file. For example, instead of
// MyGameThing.cpp
#include "MyGameThing.hpp"
#include <utility>
#include <string>
// code
do
// MyGameThing.cpp
#include <utility>
#include <string>
#include "MyGameThing.hpp"
// code
That way you won't get compile errors like this buried in the standard headers.
The first compiler error is going to be your best clue:
c:\program files (x86)\microsoft visual studio 10.0\vc\include\utility(454): error C2447: '{' : missing function header (old-style formal list?)
I'm guessing that what's happening here is you may have a class or struct declaration with no ending semicolon. So you have something like this:
struct Foo
{
} // <--- no semicolon here will generate funky errors
But overall, pay close & special attention to the first compiler error.
As #Billy mentions in the comments below, the rules for struct and class are the same. In either case, you need the semicolon. Also, old-school C code will often typedef structs like this:
typedef struct tag_Foo
{
} Foo; // <-- still need the semicolon
Here, you still need the semicolon.
If using Visual C++, open the first cpp file and press CTRL+F7, this will compile only the cpp file, check the first error and try to correct this. When fixed go to the next cpp file if the error persists.
Billy's attempt is a decent one, but the usual response to a missing ';' is for the compiler to say so. A missing '}' on the other hand can seriously confuse standard headers included afterward because all their content is within the scope of either the namespace or class that should have been closed. Also, ';' missing is more immediately detectable while a missing '}' isn't necessarily an error so the compiler just pisses a fit at the first thing that doesn't make sense in that scope.
Furthermore, at line 454 in utility for the 2010 compiler is the opening brace for a specialization of tuple_size for std::pair. Post preprocessor that definition would look like so:
template < ... >
struct tuple_size< ::std::pair<_Ty1, _Ty2> >
If you have forgotten a '}' closing a namespace then that's not where pair<> exists anymore. Instead, since it's declared like so:
namespace std { template < ... > struct pair ... }
Whatever scope wasn't closed is now where std is being declared and struct tuple_size< ::std::pair<...> > doesn't make any sense to the parser. Since it's not a valid name it tends to pretend it's not even there and then '{' at the global scope without anything of sense before it is pretty f'n confusing.
BTW, an often unspoken benefit of making minimal examples that cause the problem is that you end up FINDING the problem trying to make it happen.
In my simple OpenGL program I get the following error about exit redefinition:
1>c:\program files\microsoft visual studio 8\vc\include\stdlib.h(406) : error C2381: 'exit' : redefinition; __declspec(noreturn) differs
1> c:\program files\microsoft visual studio 8\vc\platformsdk\include\gl\glut.h(146) : see declaration of 'exit'
I'm using Nate Robins' GLUT for Win32 and get this error with Visual Studio 2005 or Visual C++ 2005 (Express Edition). What is the cause of this error and how do I fix it?
Cause:
The stdlib.h which ships with the recent versions of Visual Studio has a different (and conflicting) definition of the exit() function. It clashes with the definition in glut.h.
Solution:
Override the definition in glut.h with that in stdlib.h. Place the stdlib.h line above the glut.h line in your code.
#include <stdlib.h>
#include <GL/glut.h>
or this...
To fix the error, right click on the project name in the Solution Explorer tab and select Properties -> C/C++ -> Preprocessor -> Preprocessor definitions and append GLUT_BUILDING_LIB to the existing definitions, seperated by semicolons.