There are unresolved includes inside <iostream> - c++

I just tried to compile my C++ code and an error appears when I try to do so.
The error appears on line 9
Here are the versions of the gcc and g++ and such
Any help would be appreciated.
Edit:
I am also including Movie.h:
And also Movie.cpp:
https://puu.sh/vb53G/9e9abd1832.png (I was not able to include more than 3 images due to restrictions)

Firstly, in your Movie.h file, you have not included the string header file correctly. It should be:
#include <string> // without the .h extension
error: 'string' does not name a type
Secondly, you have forgotten to add the closing parenthesis of the constructor function of class "Movie". I am assuming that you have added this now, after the edit
As for the marking done by your compiler, you may find the following StackOverflow post helpful:
StackOverflow Post: Unresolved inclusion iostream.
The link is for the Eclipse IDE, but you can find a similar solution for your own IDE (I cannot tell which one you have).

The line under the #include is just a warning (I'm not sure why).
However, the errors are from the "Movie" class:
1. add "using namespace std" on the top of this class.
2. close the parenthesis on the constructor of 'Movie'.

The error messages are fairly clear:
'string' does not name a type
That is, the compiler is unaware of the type string because either:
you have not #include <string> in Movie.h
or you have, but have not brought it into your namespace with a using namespace std;
although why not just refer to it as std::string?

You are missing
#include <string>

Related

'printf': identifier not found

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()

Is it possible to undefine a class in C++ (Visual Studio)?

I know, it may seem strange, but my goal is to undefine a class in C++. The root of the problem is in combining TinyXML2 and Boost unit tests.
Contents of the header file (Configuration.h) which is being tested:
...
#include <tinyxml2.h>
...
And this is the contents of my configurationTests.h file:
#include "unitTests.h"
#include "Configuration.h"
BOOST_AUTO_TEST_SUITE(configuration_test)
BOOST_AUTO_TEST_CASE(basic) {
...
}
BOOST_AUTO_TEST_SUITE_END( )
When I try to compile my tests, I'm getting an error:
error C2371: 'XMLDocument' : redefinition; different basic
types c:\program files (x86)\windows kits\8.0\include\um\msxml.h 10085
Inside this file (msxml.h) on line 10085 we have this class definition:
class DECLSPEC_UUID("CFC399AF-D876-11d0-9C10-00C04FC99C8E")
XMLDocument;
When I remove those two lines, my tests do compile and everything seems fine. Of course, this is not a solution, but that fact prooves that something inside Boost unit tests library includes msxml.h and somehow leads to conflict with TinyXML2 library.
I tried different solutions found in Google (like writing "#define WIN32_LEAN_AND_MEAN"), removing "using namespace tinyxml2" and making changes inside tinyxml2.cpp - nothing actually helps.
So, my question is simple: can I undefine (unload?) previously defined class in compile time in some tricky way? I tried "#undef XMLDocument", "#define XMLDocument 1" + "#undef XMLDocument" - nothing works.
Update: Actually, I kinda solved the problem by writing "#define MSXML_LIBRARY_DEFINED" on the first line of configurationTests.h. But still, I would love to know an answer to this topic question.
I think you have used default namespace for tinyxml2.
Try to delete using namespace tinyxml2 and then use it like in this example:
tinyxml2::XMLDocument xDoc;
try this:
namespace your_name_space
{
#include <tinyxml2.h>
}
From now all classes inside the tinyxml2 are hidden by your namespace.
The namespece must be declared also for the tinyxml2.cpp file.

C++ cout gives undeclared identifier

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?

c++ simple program error

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.

How do I get a handle to split_winmain

I am trying to get a get the boost library program_options working on a simple windows console library.
I have linked in the library
C:\Program Files\boost\boost_1_40\lib\libboost_program_options-vc90-s-1_40.lib
Included the header files
#include <boost/program_options.hpp>
#include <boost/program_options/config.hpp>
#include <boost/program_options/option.hpp>
#include <boost/program_options/detail/cmdline.hpp>
#include <boost/program_options/detail/parsers.hpp >
Defined _WIN32 (But I don't think it is required.)
And I still keep getting the
Error 1 error C3861: 'split_winmain': identifier not found
It should be so simple but I can't get it to work. Can anyone tell me what I need to do here.
Joseph Shanahan
That function is declared in the boost::program_options namespace. If all you do is use its name alone, the compiler doesn't know what you're talking about. You have a few options:
Use the fully qualified name when you call it:
boost::program_options::split_winmain(...);
Tell the compiler which function you mean:
using boost::program_options::split_winmain;
split_winmain(...);
Bring the entire namespace into the current scope:
using namespace boost::program_options;
split_winmain(...);
Make a namespace alias:
namespace po = boost::program_options;
po::split_winmain(...);
I prefer the last one.
Do not define the _WIN32 macro; the compiler will do that for you when it's appropriate.