No match for 'operator>>'in 'std::operator

Hi, Im creating a simple program but i cant compile because it gives me that error.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
    ofstream users;
    users.open ("database.txt");
    
    string user[20], password[16], password2[16];
    
    cout << "Enter your username";
    cin >> usuario >> endl;
    
    users >> usuario;
    
    cout << "Enter your password" << endl;
    cin >> password ;
    cout << "Repite your password" << endl;
    cin >> password2 ;
    
    if (password & password2)
    {
                 users >> password;
                 }
                 else 
                 {
                      cout << "Sorry your password's are not the same, try again!";
                      }
                      
}
    


Thank you, and sorry for my english.
Because std::ofstream has no operator>> :)
std::ofstream has operator<<

http://www.cplusplus.com/reference/fstream/ofstream/
So, is ifstream users?

Because i want write on it.
std::ofstream is for writing to a file.
std::ifstream is for reading from a file.
Also look at line 14. You are trying to input to endl;?
I correct the endl;
But i dont understand why my file give error another time
So is okay with ofstream, ty.
>> is for reading in data.
<< is for writing out data.
If you don't understand the error message, ¿why did you remove information from it? (as it is, we can't know which objects is referring to, or what line is it happening)
I realize that the message is quite lengthy, so send it to a file and upload that.

You may want to take a look at those lines that say `error'
g++ foo.cpp 2>&1 | grep error
foo.cpp:14:9: error: ‘usuario’ was not declared in this scope
foo.cpp:19:6: error: no match for ‘operator>>’ in ‘std::cin >> password’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘std::string [16] {aka std::basic_string<char> [16]}’)
foo.cpp:21:6: error: no match for ‘operator>>’ in ‘std::cin >> password2’ (operand types are ‘std::istream {aka std::basic_istream<char>}’ and ‘std::string [16] {aka std::basic_string<char> [16]}’)
foo.cpp:23:17: error: invalid operands of types ‘std::string [16] {aka std::basic_string<char> [16]}’ and ‘std::string [16] {aka std::basic_string<char> [16]}’ to binary ‘operator&’
foo.cpp:25:9: error: no match for ‘operator>>’ in ‘users >> password’ (operand types are ‘std::ofstream {aka std::basic_ofstream<char>}’ and ‘std::string [16] {aka std::basic_string<char> [16]}’)
14: trying to use a non-existent variable

19,21: trying to read an array. Probably you intended to write
1
2
3
4
std::string usuario, password, password2;
std::cin >> usuario;
std::cin >> password;
std::cin >> password2;
note that the length of the strings is not limited

23: don't guess the syntax. I suppose that you want to compare the strings, so if( password == password2 ) provided that you made the previous change

25: what ResidentBiscuit said users << password, the same applies to line 16.


Also, learn to indent.
Or better, don't. Simply let your IDE to handle that.


Edit:
output to file stream -> ofstream
input from file stream -> ifstream
Last edited on
Topic archived. No new replies allowed.