i've this simple code but i dont where is the error of this all
#include <iostream.h>
#include <fstream.h>
#include <string.h>
using namespace std;
int main(){
string s;
cout<<"Entrer nom de fichier avec le source";
cin s;
ifstream fout;
fout.open(s);
s=fout.getche();
fout.close();
cout<<s;
return 0;
}
cause the errors showing after compiling are:
d:\workespace3.cpp(5) : error C2871: 'std' : does not exist or is not a namespace
d:\workespace3.cpp(8) : error C2653: 'std' : is not a class or namespace name
d:\workespace3.cpp(8) : error C2065: 'string' : undeclared identifier
d:\workespace3.cpp(8) : error C2146: syntax error : missing ';' before identifier 's'
d:\workespace3.cpp(8) : error C2065: 's' : undeclared identifier
d:\workespace3.cpp(10) : error C2146: syntax error : missing ';' before identifier 's'
d:\workespace3.cpp(13) : error C2039: 'getche' : is not a member of 'ifstream'
c:\program files (x86)\microsoft visual studio\vc98\include\fstream.h(98) : see declaration of 'ifstream'
Error executing cl.exe.
workespace3.obj - 7 error(s), 0 warning(s)
Don't use the .h forms of the include files, those are meant for backwards compatibility with C. Use for example #include <string>.
You have many errors:
1- You are using deprecated header files. Standard C++ library headers come in headers without ".h". So that would be:
#include <iostream>
#include <fstream>
#include <string>
2- getche() is not a proper method of ifstream. Here is the complete list of methods of ifstream:
http://www.cplusplus.com/reference/fstream/ifstream/
You probably meant to use get() or getline()
3- You are missing ">>" between "cin" and "s".
4- You are using a very old IDE. There are multiple newer and free IDEs out there. Notably you can have VC++ 2012 Express for free. It will be more standards compliant and also include better tools and support for C++11
If the compiler supports the C++98 standard headers, then use them
#include <iostream> // no .h
If it only supports the ancient pre-ISO headers that you're including, then just leave out any mention of namespace std. In those dark days, the standard library was just dumped into the global namespace.
I would consider using a compiler from this millennium; you'll find it easier to get help from people whose memory of the 1990s is less than perfect.
Related
I followed a video on LinkedIn to setup SFML, but when I tried compiling the code get several errors, some of which are:
C2065 'Fullscreen': undeclared identifier
C3861 'RenderWindow':identifier not found
C2871 'sf': a namespace with this name does not exist
C2653 'Style':is not a class or namespace name
C2065 'VideoMode':undeclared identifier
C3861 'vm' :identifier not found
C2065 'vm' :undeclared identifier
C26444 Don't try to declare a local variable with no name(es.84)
C2146 syntax error:missing ';' before identifier 'vm'
Assuming (from its name) that "pch.h" generates and/or uses the precompiled header for your build, then that has to be the very first header included in any source file. Otherwise, anything 'gleaned' from headers included before it will be lost, as the compiler only looks in that precompiled header and files included afterwards.
So, just rearrange your top three lines as follows:
#include "pch.h" // MUST be the first header included!
#include <iostream>
#include <SFML/Graphics.hpp>
For an interesting (and informative) discussion about precompiled headers in Visual Studio, see this Stack Overflow question, and the answers there: Precompiled Headers.
This
//CSocket.h
#ifndef __SERVER_CSOCKET_H__
#define __SERVER_CSOCKET_H__
#include "winsock2.h"
#include "ws2tcpip.h"
#include <thread>
#include <stdio.h>
#include <string>
(cpp includes only the header)
//CSocket.cpp
#include "CSocket.h"
produces the following error messages in c:\program files (x86)\microsoft visual studio 12.0\vc\include\ratio
ratio(122): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(133): See reference to the instance of the just compiled class-template "std::ratio<_Nx,_Dx>".
ratio(124): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(44): error C2065: 'INTMAX_MAX': undeclared identifier
ratio(217): See reference to the instance of the just compiled class-template "std::_Safe_mult<0x01,0x01>".
ratio(36): error C2338: integer arithmetic overflow
ratio(44): See reference to the instance of the just compiled class-template "std::_Safe_multX<0x01,0x01,false>".
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x01,false>'
ratio(44): error C2065: 'value': undeclared identifier
ratio(44): error C2057: Expected constant expression
ratio(44): error C2039: 'value': Is not an element of 'std::_Safe_multX<0x01,0x0989680,false>'
ratio(219): error C2975: "_Nx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(116): See declaration of '_Nx'
ratio(219): error C2975: "_Dx": invalid template argument for "std::ratio", expected compile-time constant expression.
ratio(117): See declaration of '_Dx'
CSocket.cpp
Including std::thread in the .cpp and not in the header solves all errors but I don't know why it doesn't work in the header.
//CSocket.cpp
#include "CSocket.h"
#include <thread>
The only library I am using is jemalloc.
Might the error come from including jemalloc.h before mutex not from thread itself?
I had to #include <mutex> before #include "jemalloc.h" and not afterwards.
Works fine now, strange errors though.
I am having the same error, but the order of the includes is not useful for me. I think this has something to do with other includes that also use chrono and thread, so you can checkout that.
Are you using Visual Studio? Seems like more people is getting the same error: https://connect.microsoft.com/VisualStudio/feedback/details/800726/compiler-error
I have the same errors with VS2013 update 3. The problem seems to be the fact that INTMAX_MAX is not defined, but it is used in ratio.h.
My solution is to add
#define INTMAX_MAX INT64_MAX
before #include <ratio> in your file (if you do not have the line, you may add it).
The line to be included can be found in stdint.h - in your case, the right side can be different.
PS Another solution is to #include <stdint.h> and to define __STDC_LIMIT_MACROS. In this case, you may get some warnings about duplicate macros.
it occurs to the project when i use the CxImage as third party library and with threadpool in C++11. Separately they are all okay, while merged in the same project, that errors occur.
the solution is add the precompile option of _STDC_LIMIT_MACROS to
Property Pages -> Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions of the Project.
PS: my environment is : MFC/VS2015 && windows7 64bit
may it be helpful to somebody :)
I am getting the following errors when compiling the below code:
3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2059: syntax error : 'constant'
3>c:\hedge\hedge\hedge\AisTarget.h(22) : error C2238: unexpected token(s) preceding ';'
#if !defined(AisTarget_h)
#define AisTarget_h
#include "GeneralAviationItems.h"
#include <string>
namespace HEDGE {
using namespace GeneralAviation;
class AisTarget : public WaypointLatLon {
public:
static const int NO_DATA = -1000; //here is the error
};
} // end namespace HEDGE
#endif
It is likely that NO_DATA is already defined as a macro elsewhere, and so it is expanding into something that does not agree with the compiler's notion of a variable name. Try re-naming NO_DATA to something else.
If there were no such conflict, the code as it were would compile fine, as demonstrated here.
Even if this post has its age: The error can generally occur when multiple redefinitions, even regardless of upper/lower case, coexist. This includes potential preprocessor definitions in the solution's .vcprojx file!. Consider something like
<ItemDefinitionGroup>
<ClCompile>
<PreprocessorDefinitions>$(Configuration);%(PreprocessorDefinitions)</PreprocessorDefinitions>
</ClCompile>
</ItemDefinitionGroup>
in the above mentioned file. Now, having "Debug" and "Release" configurations you will most probably run into some problems and a potential source for the C2059 error. I experienced exaclty this dilemma.
I have a function:
SHFileOperationFunc(string item1, string item2, int operation, CuTest *tc)
It is used to do operations (move, delete or copy) directories. It works fine. But i am now trying to use stuff from dirent.h. When I include the file it causes huge problems with stuff from ShellAPI.h I use in SHFileOperationFunc. I got dirent.h as per http://www.softagalleria.net/dirent.php.
After I include the file I get errors like:
error C2065: 'FO_DELETE' : undeclared identifier
error C2065: 'FO_COPY' : undeclared identifier
error C2065: 'FO_DELETE' : undeclared identifier
error C2065: 'FO_DELETE' : undeclared identifier
These are defined in ShellAPI.h. When I hover over them in VS it says the value they have so it is seeing them. Anyone know why including this file is having this effect?
Using dirent.h on a Windows machine is unadvisable, it is a Unix header. The file you downloaded screws up the rest of your #includes. Either put if after #include <windows.h> or edit the file and delete the #define for WIN32_LEAN_AND_MEAN. Putting that in a header is, well, mean. It causes a large number of declarations to be skipped in the Windows headers.
I am compiling a Matlab mex file (Using VS2010 under Windows), and the following 2 includes:
#include <algorithm> // for std::copy
#include "mex.h"
give me compile error:
1>d:\svn\trunk\dev\matlab\extern\include\matrix.h(337): error C2371:
'char16_t' : redefinition; different basic types
I have tried putting it in a namespace:
namespace Algo {
#include <algorithm>
}
But then I get tons of other compile errors, without even using anything defined in <algorithm>, for example:
Error 1 error C2039: 'set_terminate' : is not a member of '`global namespace'' C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception 192
Error 2 error C2873: 'set_terminate' : symbol cannot be used in a using-declaration C:\Program Files (x86)\Microsoft Visual Studio 10.0\VC\include\exception 192
How can I solve this?
Putting an standard header in a namespace doesn't sound like a good idea, even though you are not using any of the methods or classes there, there is a pretty big chance that another header might (like the mex.h). Putting the namespace around the mex header seems much less probable to create a conflict.
Also including one header before the other might also prevent such a conflict. Some headers take into account that some symbol might already be defined before redefining them (library developer really should take care to do this)
In some cases reading the headers might also give you a good clue what is going on. Some times it might be as simple as defining a symbol, which tells the header to skip the redefinition.