简体   繁体   中英

Why do i get an error no match for 'operator^'

I am getting an error

10:13: error: no match for 'operator^' (operand types are 'std::basic_ostream<char>' and 'int')
10:13: note: candidates are:
In file included from /usr/include/c++/4.9/ios:42:0,
             from /usr/include/c++/4.9/ostream:38,
             from /usr/include/c++/4.9/iostream:39,
             from 2:
/usr/include/c++/4.9/bits/ios_base.h:161:3: note: std::_Ios_Iostate std::operator^(std::_Ios_Iostate, std::_Ios_Iostate)
operator^(_Ios_Iostate __a, _Ios_Iostate __b)
^

the code is

// Example program
#include <iostream>
#include <string>

int main()
{
int a=1;
int b=2;

std::cout<<a^b;
}

What are the operands that can be used with operator ^ ?

According to the Operator Precedence , operator<< has higher precedence than operator^ . So std::cout<<a^b; is equivalent with (std::cout<<a)^b; ; (std::cout<<a) will return std::cout by reference, which is a std::basic_ostream<char> ; Just as the error message said, you can't call operator^ with std::cout ( std::basic_ostream<char> ) and int .

You could use parentheses to specify the precedence how the operands should be bound to operators.

std::cout << (a^b);
//           ~   ~

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM