0

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;
}
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
midcore
  • 5
  • 1

6 Answers6

1

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

J. Chen
  • 367
  • 1
  • 7
  • 'code' '#include #include #include "stdafx.h" int main() { std::string forName; std::cout << "Write your name below please:/n"; std::cin >> forName; std::cout << forName; }' – midcore Jul 21 '17 at 15:18
  • What development environment/compiler are you using? – J. Chen Jul 21 '17 at 15:20
  • Try putting `#include "stdafx.h"` at the top of the code, above all the other `include` statements. Also notice the quotation marks instead of the angular brackets -- originally thought stdafx.h was a library but realized that it's generated by VS. Let me know if this helps – J. Chen Jul 21 '17 at 17:39
1

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>
user4581301
  • 33,082
  • 7
  • 33
  • 54
0

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++?

Erik Godard
  • 5,930
  • 6
  • 30
  • 33
0

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>
Ghulam Moinul Quadir
  • 1,638
  • 1
  • 12
  • 17
-1

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.

Tanuj Yadav
  • 1,259
  • 13
  • 21
-1

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.

cjaybo
  • 1
  • 1
  • This sounds a little bit unprecise. Use `#include ` for C string functions (e.g. `strcpy()`). Use `#include ` for C++ string classes (e.g. `std::string`). Due to the existance of keyword `using`, your wording _could_ be misunderstood. – Scheff's Cat Jul 21 '17 at 15:24