Why I cannot cout the result? - c++

I have this error message:
Error C2678 binary '^': no operator found which takes a left-hand
operand of type 'std::basic_ostream<char,std::char_traits<char>>'
(or there is no acceptable conversion)
with this code:
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << 2 ^ 4;
}
could you help me with this code, please
I use visual studio 2019

operator<< has higher precedence than operator ^, so cout << 2 ^ 4; is interpreted as (cout << 2) ^ 4;. (cout << 2) returns cout itself, which can't be used as operand of operator ^.
Change the code to
cout << (2 ^ 4);

Can you use this code.
#include <iostream>
#include <string>
using namespace std;
int main()
{
cout << (2 ^ 4);
return 0;
}

Related

not able to compare count function used on strings in c++?

here is the code :
#include <iostream>
#include <ostream>
#include <bits/stdc++.h>
#include <string>
int main(){
std::string num {"hello"};
std::string num1 {"hii"};
int nums = std::count(num.begin(),num.end(),num[0]);
int nums1 = std::count(num1.begin(),num1.end(),num1[0]);
std::cout << std::count(num1.begin(),num1.end(),num1[0] ) == count(num.begin(),num.end(),num[0]);
std::cout << nums == nums1 ;
return 0;
}
please help if anyone know problem. updated......
To use == in operator << we need to add parentheses here since overloaded operator << has higher precedence than comparison operator. If you use a mordern compiler, it will give you detailed hints, such as clang 11 will give use such warnings:
warning: overloaded operator << has higher precedence than comparison operator [-Woverloaded-shift-op-parentheses]
Then we can fix it as:
#include <algorithm>
#include <iostream>
#include <ostream>
#include <string>
int main() {
std::string num{"hello"};
std::string num1{"hii"};
std::size_t nums = std::count(num.begin(), num.end(), num[0]);
std::size_t nums1 = std::count(num1.begin(), num1.end(), num1[0]);
std::cout << (std::count(num1.begin(), num1.end(), num1[0]) ==
count(num.begin(), num.end(), num[0]));
std::cout << (nums == nums1);
return 0;
}
Online demo

error c2679 binary '=' no operator found which takes right hand operand of type std::tuple

I know there are multiple other questions like this but I've tried all the suggestions I've seen to no avail. I am attempting to write a vector (made of tuples containing a bool, int, and another int) with ostream. I've made sure to add std:: in front of everything it might need as I know that can be the problem often. I would appreciate any help. (the error in question happens at the bottom two lines, specifically the std::copy one, not sure if the rest of the code is important).
#include "Options.h"
#include <fstream>
#include <iostream>
#include <istream>
#include <ostream>
#include <string>
#include <vector>
#include <tuple>
#include <stdbool.h>
#include <stdint.h>
using namespace std;
using namespace smf;
int main(int argc, char** argv) {
Options options;
options.process(argc, argv);
MidiFile midifile;
midifile.read("lowtest.mid");
midifile.linkNotePairs();
midifile.sortTracks();
midifile.joinTracks();
midifile.deltaTicks();
std::tuple <bool, int, int> out;
std::vector <std::tuple<bool, int, int>> outlist;
int track = 0;
int i = 0;
for (int event = 0; event < midifile[track].size(); event++) {
if (midifile[track][event].isNoteOn()) {
out = std::make_tuple(true, (int)midifile[track][event][1], (int)midifile[track][event].getTickDuration());
outlist.push_back(out);
std::cout << std::get<0>(outlist[i]) << ' ' << std::get<1>(outlist[i]) << ' ' << std::get<2>(outlist[i]);
i = i + 1;
std::cout << " " << "I = " << i << std::endl;
}
}
std::cout << "done";
std::ofstream output_file("./output.txt");
std::ostream_iterator<std::tuple <bool, int, int>> output_iterator(output_file, "\n");
std::copy(outlist.begin(), outlist.end(), output_iterator);
return 0;
}
error:
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'const _Ty' (or there is no acceptable conversion) midiparser C:\Program Files (x86)\Microsoft Visual Studio\2019\Community\VC\Tools\MSVC\14.28.29910\include\iterator 336

C++ cout not accepting strings or strings with + [duplicate]

This question already has an answer here:
What is "pch.h" and why is it needed to be included as the first header file?
(1 answer)
Closed 3 years ago.
class Book {
public:
string title;
string author;
void readBook() {
cout << "Reading" + this->title + " by " + this->author << endl;
}
};
This is causing the following error.
binary '<<': no operator found which takes a right-hand operand of type 'std::basic_string<char,std::char_traits<char>,std::allocator<char>>'
Secondly
cout << part1 << endl;
This is causing this error.
Error C2679 binary '<<': no operator found which takes a right-hand operand of type 'std::string'
My include statements
#include <string>
#include "pch.h"
#include <iostream>
#include <vector>
#include <exception>
#include <stdio.h>
#include <Windows.h>
using namespace std;
All in VS 2017.
I can get the strings to print with
cout << part1.c_str() << part2.c_str() << endl;
Can someone explain to me why it won't print the strings without .c_str() and why it won't accept multiple strings? I am following a tutorial and the tutor is able to process these variables without error.
cout << somethig is implemented by operator overloading..
operator <<( const char * ) is exist..
but operator <<( string ) is not exist..
c_str() returns const char *. so, You can use it with cout <<
This will work..
cout << "Reading" << this->title.c_str() << " by " << this->author.c_str() << endl;

Why is writing a std::string to cout causing an unknown operator << error?

I am getting an error when I try to output the return value from one of my methods:
Error: No operator "<<" matches these operands. Operand types are: std::ostream << std::string
Main.cpp
#include <iostream>
using namespace std;
#include "Book.h"
int main()
{
book.setTitle("Advanced C++ Programming");
book.setAuthorName("Linda", "Smith");
book.setPublisher("Microsoft Press", "One Microsoft Way", "Redmond");
book.setPrice(49.99);
cout << book.getBookInfo(); // <-= this won't compile because of the error above.
int i;
cin >> i;
return 0;
};
Method which should return string:
string Book::getBookInfo()
{
stringstream ss;
ss << title << endl << convertDoubleToString(price) << endl;
return ss.str();
}
#include <string> is missing.
How did the code get the definition of string? The header <string> also declares the stream inserter.

C++ cin and strnicmp not working

void searchFlight(cust flights[] ,int row)
{
clrscr();
cout << "Search for the flight you are looking for.\n";
string airport;
cout << "Enter Departing Flight : ";
cin >> airport; //error
for (int r=0;r<row;r++)
{
if (strnicmp(airport, flights[r].airport[20], strlen(airport) ==0) //error
{
clrscr();
cout << flights[r].name[20] <<endl;
cout << flights[r].airport[20] <<endl;
cout << flights[r].destination[20] <<endl;
cout << flights[r].ticket <<endl;
cout << flights[r].passangers <<endl;
cout << flights[r].opCost <<endl;
cout << flights[r].income <<endl;
cout << flights[r].netProfit <<endl;;
pressKey();
}
}
pressKey();
}
For the cin error:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
For the strnicmp error:
error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'
I have searched for solutions to this problem and could not fix it. Apologies if there is a similar post on here which could have solved my problems.
For the cin error: error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::istream' (or there is no acceptable conversion)
Add #include <string> to your CPP file.
For the strnicmp error: error C2664: 'strlen' : cannot convert parameter 1 from 'std::string' to 'const char *'
Confirm that you have #include <cstring> to your CPP file, and replace your call with:
strnicmp(airport.c_str(), flights[r].airport[20], strlen(airport.c_str()) ==0.
I suspect that flights[r].airport[20] is also incorrect, but I can't know because you didn't post a complete program.
If cust::airport is declared like std::string airport;, then you need flights[r].airport.c_str().
If cust::airport is declared like char airport[20];, then you need flights[r].airport.
Have you included the following :
<fstream>
<istream>
<iostream>
<string>
I'm pretty sure you forgot the <string>
Here's a test code I ran and it worked flawlessly!
#include <string>
#include <iostream>
using namespace std;
int main(int, char**)
{
string foo;
cin >> foo;
cout << foo;
}