no operators "<<" match these operands - c++

I get the error:
no operators ">>" match these operands.
and
string is not a member of std.
#include "iostream"
#include "string.h"
#include "stdafx.h"
int main()
{
std::string forName;
std::cout << "Write your name below please:/n";
std::cin >> forName;
std::cout << forName;
}

Unless you're trying to include your own header files, include statements in C++ should use angular brackets:
#include <iostream>
#include <string.h>
#include <stdafx.h>
Also, if you're writing C++: I suggest that you use <string> instead of <string.h>, because the latter is a C header (not C++) and is deprecated in C++. If not then ignore this bit :)

Everything included above #include "stdafx.h" is ignored by Visual Studio. More information on why this is can be found here: What's the use for "stdafx.h" in Visual Studio?
So
#include "iostream"
#include "string.h"
#include "stdafx.h"
Must be
#include "stdafx.h"
#include "iostream"
#include "string.h"
After that you can optimize the searching for the include files by surrounding headers with the appropriate <> or "". Selecting which to use is covered here: What is the difference between #include <filename> and #include "filename"?
In addition, string.h is a C header for C string utilities. It does not include the class string. For that you need
#include <string>
If you do want the C string utilities, it is recommended you use the C++ version
#include <cstring>

You need to change the quotation marks to angle brackets.
#include <iostream>
#include <string>
#include <stdafx.h>
See this question for more information about the difference between the two: Difference between angle bracket < > and double quotes " " while including header files in C++?

If you have your own header file then include like this:
#include "yourheader.h"
But if you are using standard header file then you have to include like this:
#include <iostream>

You must replace #include "filename" with #include <filename>
For #include "filename" the preprocessor searches in the same directory as the file containing the directive. This method is normally used to include programmer-defined header files.
For #include <filename> the preprocessor searches in an implementation dependent manner, normally in search directories pre-designated by the compiler/IDE. This method is normally used to include standard library header files.

The string.h library is actually deprecated within C++. Try using string instead, or if you need functionality that is from string.h (strcpy, strlen), try using cstring.

Related

Do not-precompiled headers use precompiled headers if they are Included or are they for .cpp files only?

Visual Studio 2022:
I want to Include Precompiled headers in my .cpp file but I don't know if it's worth it since I'll also need to include a non-precompiled header with almost the same headers that are in the precompiled header.
Will the non-precompiled header use the precompiled headers or will it generate the code again on each compilation?
CPP:
#pragma once
#include "Precompiled.h"
#include "No-Precompiled.h" // Basic Headers: Windows.h, Psapi.h
int main()
{
// Functions that I need from "No-Precompiled.h" but I can't Precompile it since changes in it are made on regular basis
}
No-Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
class Template
{
public:
//Functions that need many same Headers.
}
Precompiled.h:
#pragma once
#include <windows.h>
#include <Psapi.h>
#include <d3d11.h>
#include <limits>
#include <complex>
#include <filesystem>
#include <iostream>
#include <string>
#include <chrono>
#include <thread>
#include <tchar.h>
#include <wincred.h>
#include <complex>
#include <math.h>
Should I just Precompile the headers that the .cpp file uses (which is not much) or is there a way to allow No-Precompiled headers to use the Precompiled headers?
Using pre-compiled headers doesn't change that much. In particular, header guards continue to work. The header guard for <windows.h> is also included in the pre-compiled state. Hence, when the compiler sees <windows.h> for the second time, it's immediately skipped.
In your case, the No-Precompiled.h header turns out to be pretty trivial, as all its headers have already been included. You're just compiling the Template.
I'd wonder a bit about the particular set of precompiled headers, though. PSapi and DirectX and IOstream? I can't really imagine a big program where you have many files using all of them. Note that <iostream> is really about std::cout, which doesn't make a lot of sense for DirectX programs.

Here to place header files in CLION

guys am using clion 2017 because my laptop is only 2gb ram am still a beginner and I don't know where to place header files in order to work I downloaded the files like std_lib_facilities.h but i don't know the directory I need to place it in so I usually replace it by
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
inline void keep_window_open(){char ch; cin>>ch;}
but I am looking forward to start using header files correctly so where to place it?

Why do these headers only work outside of the pre-compiled header?

In stdafx.h:
#include <fstream>
#include <sstream>
In example.cpp:
#include <stdafx.h>
std::ifstream in_stream;
std::stringstream st_stream;
If I don't place the fstream and sstream includes in the .cpp file I get a ton of errors, such as:
Error C2079 'in_stream' uses undefined class
'std::basic_ifstream<char,std::char_traits<char>>'
Error C2228 left of '.exceptions' must have class/struct/union
Why do the errors disappear if I place the appropriate includes directly in the .cpp file? Shouldn't the functionality be identical?
This should be written as "stdafx.h" not <stdafx.h>, because "stdafx.h" is not a standard header file (that's just C++ ethics, not rules).
Visual Studio automatically creates this files and adds a bunch of header files to it.
If you have a large project with many source files, and <fstream> is used in many source files, then include <fstream> in "stdafx.h". Otherwise avoid editing this file.
std::ifstream requires <fstream> header files. The required header file is mentioned in relevant help pages. See for example std::ifstream help
Add the relevant header files directly in your "myfile.cpp" file:
//myfile.cpp:
#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
int main(){...}
If you have a small project you can tell Visual Studio to stop using precompiled headers via "Project Settings" -> "C/C++" -> "Precompiled Headers". This way you can remove "stdafx.h" and your source file will be more compatible with different compilers.

too many include in my program

so here the problem i have 13 include files in my program (and i am willing to include more!) but the problem is that the compiler is ignoring the last include and i can say so because i switched between two of them and the error will be always for the last lien the line number 13
here are the include files
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <vector> //for dynamic tables
#include <string>
#include <conio.h>//used for the function getch
#include "checkPassword.hpp"
#include "buffervoider.hpp"
#include "checktyping.hpp"
#include "extractline.hpp"
#include "getchoic.hpp"
#include "tableidentify.hpp"
the error here will be:
|error: 'tableidentify' was not declared in this scope|
but if i switch between #include "getchoic.hpp" and #include "tableidentify.hpp"
the error would be
|error: 'getchoic' was not declared in this scope|
also all my headers have include guards
so how to solve this problem ?
If those hpp files are yours, then remove the #include in one of them that includes the other so if tableidentify.hpp has #includes getchoic or vice versa, remove one of the includes to the other
You probably baby have circular includes: one file includes another, which includes the first.
I'm assuming that some of those headers are user defined (i.e. you made them). If so, try combining all of the user-defined header include statements in another header, then including that.

How to include all source files from folder? (C++, MS VS 2013)

I have simple project where I use tiny ttmath library for C++ (big nums).
This library consists of 13 *.h files.
I have included all these files in a stupid way:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
What is the right way? I expect smth like this:
#include "ttmath\*.h"
but can not find...
That won't work because the preprocessor is not going to expand characters to match things in the way you expect wildcards to work.
My recommendation would be to create a single custom header file of your own, and place all the #include entries in there. For example, in your .c file, you can add your own header:
#include "my_header.h"
And the contents of my_header.h would be:
#include "ttmath\ttmath.h"
#include "ttmath\ttmathbig.h"
#include "ttmath\ttmathdec.h"
#include "ttmath\ttmathint.h"
#include "ttmath\ttmathmisc.h"
#include "ttmath\ttmathobjects.h"
#include "ttmath\ttmathparser.h"
#include "ttmath\ttmaththreads.h"
#include "ttmath\ttmathtypes.h"
#include "ttmath\ttmathuint.h"
#include "ttmath\ttmathuint_noasm.h"
#include "ttmath\ttmathuint_x86.h"
#include "ttmath\ttmathuint_x86_64.h"
Basically, you put everything in a single header, and include that one instead.
The preprocessor doesn't have an "include all" built into it. Neither does it accept wildcards in filenames. You'll have to manually include all of them.
A common solution is to place all the includes in a new .h file and include that one every time you need all of them.