2

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

2
  • ^ ha a low operator precedence: Use parentheses std::cout << (a^b); Commented Oct 8, 2016 at 10:01
  • The actual question "What are the operands that can be used with operator ^ ?" is to broad. Commented Oct 8, 2016 at 10:18

1 Answer 1

6

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); // ~ ~ 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.