Earlier today (actually yesterday due to my time-zone) I was attempting a programming interview using Visual Studio 2012 for C++ on Interview Street (which uses g++).
To be brief, I came across several compilation errors1 when I was using
#include <cstring>
which was provided by the skeleton code in one of the question, and after turning to
#include <string>
all compilation errors magically disappeared.
However, upon submission to Interview Street, I had to add c back; otherwise I got compilation errors.
It was the first time I was bitten by non-standardization....
My question is: what inside <string> and <cstring> took me (precious) more than half an hour?
1 For anyone who is curious:
One error by Visual Studio 2012 if using <cstring> is:
error C2338: The C++ Standard doesn't provide a hash for this type.
in
c:\program files (x86)\microsoft visual studio 11.0\vc\include\xstddef
possibly for string as key in unordered_map
One error by g++ if using <string> is:
'strlen' was not declared in this scope
The cstring header provides functions for dealing with C-style strings — null-terminated arrays of characters. This includes functions like strlen and strcpy. It's the C++ version of the classic string.h header from C.
The string header provides the std::string class and related functions and operators.
The headers have similar names, but they're not really related beyond that. They cover separate tasks.
<cstring> has the C string code from the C header string.h. C++ has a convention where C headers have the same base name, except for a leading c and no trailing .h. All the contents are available under the std:: namespace.
<string> has the standard library std::string and related functions
In C++, you wouldn't use #include <somefile.h>, but instead #include <somefile>. Now C++ has its string classes in <string>, but the c-string functions are also available, which would be in <string.h>. C++ uses for 'traditional' c- include files. Therefore, <cstring> and <string>
http://www.cplusplus.com/reference/clibrary/cstring/
Related
I have included stdio.h into my C++ project, why am I still getting this error? Also, after I added #include , printf(), in my code, was no longer underlined in red to suggest that there was any error.
Also, I would like to use the function, format(). Which library is that found in?
you must include stdio.h instead of cstdio.h
#include <stdio.h>
Use #include< cstdio>
using namespace std;
after that you can use printf()
I'm using turboc++ 4.0 and Visual studio 2013. I just started learning programming. when I write the code.
#include<iostream.h>
#include<conio.h>
int main()
{
cout<<"hello!";
getch();
return 0;
}
it works well in turbo, but visual stdio shows an error
fatal error C1083: Cannot open include file: 'iostream.h': No such file or directory.
and when I use
using namespace std;
it shows another error about using getch();.
Does every compiler have its own syntax?
"Does every compiler have it's own syntax?"
No, there's a standard every compiler needs to implement.
Turbo-C++ was made before any standards were established and is merely the only compiler still around, that doesn't implement them.
The standard compliant way to write your program looks like:
#include <iostream>
int main() {
std::cout<<"hello!" << std::endl;
char dummy;
std::cin >> dummy;
}
Note: You shouldn't use using namespace std;, but explicitly put the std:: scope when needed, or use using std::cout cout;, etc.
Turbo C++ is from the middle or early 1990's, before the standardization of C++.
At that time the effective standard for C++ was the ARM, the Annotated Reference Manual by Bjarne Stroustrup and Margaret Ellis (IIRC), and it used <iostream.h>.
With the first standardization in 1998 <iostream.h> was dropped, and replaced with just <iostream>. The standard header places cin and cout in namespace std, so you can't just change the header name. It's not guaranteed, but you may possibly be able to make your code work by writing
#include <iostream>
using namespace std;
So, I have this question. Why does cout throws
error C2065: 'cout' : undeclared identifier
I am using Visual Studio 2012 as an IDE and I am writing a school project. I have everything done except an example file. So I am trying to write something on the screen like this:
#include "iostream"
#include "stdafx.h"
using namespace std;
int main()
{
cout<<"example";
return 0;
}
So the problem is with cout... printf works fine, but I want to use cout.
EDIT:
I've changed "" to <> but it is not helping. Also I am using this code only for example... This is not the whole project.
stdafx.h shall be the first include directive in your source file.
Switch files and convert the second include to <>, as other suggested.
#include "stdafx.h"
#include <iostream>
See this post for more information.
First of all:
#include <iostream>
instead of #include "iostream"
Secondly, it is generally considered bad practice to write using namespace std;, even though most courses start with that. It is better to only use what you actually need, in your case:
using std::cout;
#include "iostream"
should be
#include <iostream>
Quoting from this post:difference-between-iostream-and-iostream-quotes-in-include
By courtesy of #Jerry Coffin's answer:
When you use < >, the compiler only looks in the system-designated directory/directories (e.g., whatever you've set in the include environment variable) for the header.
When you use " ", the compiler looks in the local directory first, and if that fails, re-searches just like you'd used < >. Technically, (i.e., according to the standard) that doesn't have to be the "local" directory, but that's how it works in essentially every compiler of which I'm aware).
EDIT:
However, the root cause is that stdafx.h is a precompiled header. Visual C++ will not compile anything before the #include "stdafx.h" in the source file, unless the compile option /Yu'stdafx.h' is unchecked (by default); it assumes all code in the source up to and including that line is already compiled. However, it is still better to use <> with iostream not to confuse reader of the code.
If you use #include <iostream> with the <> instead of "" then it should work. Right now, the compiler doesn't know where to find the iostream library.
Also, you might want to change cout<<"example"; to cout<<"example"<<endl; for a new line so that it formats correctly.
Came across this issue while trying to build a Dynamic Linked Library. Make sure that instead of the #include stdafx.h you specify the following include on the first line of your .cpp file:
#include "pch.h"
This should also be the case for VS2017 or earlier.
This error also occurred in the Visual Studio 2017 IDE. Moving stdafx.h to the top solved the error.
For more on stdafx.h, see What's the use for "stdafx.h" in Visual Studio?
I have created a file called untitled1.cpp in dev-cpp with the following script:
#include <iostream.h>
using namespace std;
int main(){
cout << "C++";
return 0;
}
But the compiler shows errors like:
1 F:\Dev-Cpp\include\c++\3.4.2\backward\iostream.h:31,
from F:\Dev-Cpp\Untitled1.cpp In file included from
include/c++/3.4.2/backward/iostream.h:31, from
F:\Dev-Cpp\Untitled1.cpp 32:2
F:\Dev-Cpp\include\c++\3.4.2\backward\backward_warning.h #warning This
file includes at least one deprecated or antiquated header. Please
consider using one of the 32 headers found in section 17.4.1.2 of the
C++ standard. Examples include substituting the header for the
header for C++ includes, or instead of the deprecated
header . To disable this warning use -Wno-deprecated.
What is the error that I have? How do I fix it?
In C++ you import the standard library without using the .h suffix.
#include <iostream>
So your fixed example:
#include <iostream>
int main(int argc, char **argv) {
std::cout << "C++";
return 0;
}
Your code is not standard C++. You should say #include <iostream> (no ".h"!). Whatever source you have been learning this from is about 25 years out of date, and you should consider getting some more modern material.
(The "iostreams.h" header was part of a very early non-standard library in the early 1990s, and so it's being kept around for "compatibility" reasons, or to catch very inert programmers and give them a helpful hint.)
Use header file as #include<iostream> instead of #include<iostream.h>
Include iostream instead of iostream.h
This is just a warning.
I think that you could try to include iostream instead of iostream.h in order to fix it.
It says that the header, in this case, iostream.h is deprecated or antiquated. (You only have one header, so that's the one! Just read the error message!)
So you'll have to use iostream, not iostream.h.
You've posted the reason in your question already!
This file includes at least one deprecated or antiquated header.
The real question should therefore be: "Which one is antiquated, how do I replace it?", not "What's the error". Answer: Use <iostream>. The <*.h> versions are pre-standard, legacy headers.
So: Read error messages, people.
i am getting this error which i unable to remove in visual studio 2010. i am using one third party library which uses its own definition for "string" Also visual studio's xstring file is there in the folder where it gets installed. now when i am trying to compile code i am getting following error
1>...\xyz.cpp(24): error C2872: 'string' : ambiguous symbol
1> could be 'third party library path\string.h(31)
1> or 'c:\program files (x86)\microsoft visual studio 10.0\vc\include\xstring(2063) : std::string'
compiler is not able to understand which string definition it should use. How can i remove this error in visual studi 2010. I want the code to use third party string definition.
i tried to set third party path in include directory but still i am seeing this error.
Please help me. Thanks in advance
This is an example of a namespace clash. You probably have in your code:
#include <3rdPartyString.h> // declaration of 3rd party string type
#include <string> // declaration of std::string
using namespace 3rdPartyNamespace;
using namespace std;
...
string myStr; // which string type?
Compiler now doesn't know which string you want to use - one from 3rd party library or STL one. You can resolve this ambiguity by prepending namespace name to the type:
3rdPartyNamespace::string myStr; // if you want to use string from 3rd party library
or
std::string myStr; // if you want to use STL string
Never place using namespace namespace_name; in headers but try to avoid it in source files as well. The best practice is to prepend type name as this does doesn't pollute your current namespace with the other one thus avoiding namespace clashes.
Namespaces were invented to prevent these ambiguities. Make sure you never use using namespace std (that specific using namespace is bad practice anyway, and "std::" isn't that long to type) and it should be fine, just use std::string if you want the standard string.
// Without "using namespace std;"
string foo; // string from third party library used.
std::string bar; // string from standard library used.
The two definitions of string are clashing with each other so the compiler doesn't know what to use so you need a way to differentiate the two and that's where namespaces come in.
You can use the namespace that your third party string uses when referring to that string as the errors you are showing implies you have using namespace std in your code.
I ran into this issue recently by adding a Class Library project to a very large solution that included an in-house String library, the using namespace System; in the pre-generated header was causing the ambiguity.
There is System.String. If not intended to use, make sure to indicate otherwise. Example System::String, or Custom::String