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

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;

Related

Why I cannot cout the result?

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;
}

Cout and Cin throwing errors despite using headings #include<iostream> [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}

This pointer to print strings C++ [duplicate]

Why I cannot cout string like this:
string text ;
text = WordList[i].substr(0,20) ;
cout << "String is : " << text << endl ;
When I do this, I get the following error:
Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) c:\users\mollasadra\documents\visual studio 2008\projects\barnamec\barnamec\barnamec.cpp 67 barnamec**
It is amazing, that even this is not working:
string text ;
text = "hello" ;
cout << "String is : " << text << endl ;
You need to include
#include <string>
#include <iostream>
You need to reference the cout's namespace std somehow. For instance, insert
using std::cout;
using std::endl;
on top of your function definition, or the file.
There are several problems with your code:
WordList is not defined anywhere. You should define it before you use it.
You can't just write code outside a function like this. You need to put it in a function.
You need to #include <string> before you can use the string class and iostream before you use cout or endl.
string, cout and endl live in the std namespace, so you can not access them without prefixing them with std:: unless you use the using directive to bring them into scope first.
Above answers are good but If you do not want to add string include, you can use the following
ostream& operator<<(ostream& os, string& msg)
{
os<<msg.c_str();
return os;
}
You do not have to reference std::cout or std::endl explicitly.
They are both included in the namespace std. using namespace std instead of using scope resolution operator :: every time makes is easier and cleaner.
#include<iostream>
#include<string>
using namespace std;
Use c_str() to convert the std::string to const char *.
cout << "String is : " << text.c_str() << endl ;
If you are using linux system then you need to add
using namespace std;
Below headers
If windows then make sure you put headers correctly
#include<iostream.h>
#include<string.h>
Refer this it work perfectly.
#include <iostream>
#include <string>
int main ()
{
std::string str="We think in generalities, but we live in details.";
// (quoting Alfred N. Whitehead)
std::string str2 = str.substr (3,5); // "think"
std::size_t pos = str.find("live"); // position of "live" in str
std::string str3 = str.substr (pos);
// get from "live" to the end
std::cout << str2 << ' ' << str3 << '\n';
return 0;
}

Simple C++ program not compiling using wstrings and cout

I´m building this simple C++ program using Visual Studio 2012:
#include <stdafx.h>
#include <string>
#include <iostream>
int main()
{
std::wcout << "Hello World...";
std::string input_data;
std::string output_data("Hello. Please type your name");
std::wcout << output_data;
std::wcin >> input_data;
std::wcout << "Your name is " << input_data;
return 0;
}
I can´t compile. Getting the followig errors:
error C2678: binary '>>' : no operator found which takes a left-hand operand of type 'std::wistream' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<wchar_t, std::char_traits<wchar_t>> << std::string
IntelliSense: no operator "<<" matches these operands
operand types are: std::wostream << std::string
IntelliSense: no operator ">>" matches these operands
operand types are: std::wistream >> std::string
Can someone help me to fix that ?
You should try changing all std::string ocurrences for std::wstring... or all wcin/wcout for cin/cout... (in the first case, prefix the strings like L"aaa" This, for instance, works perfectly:
#include <string>
#include <iostream>
int main()
{
std::wcout << L"Hello World...";
std::wstring input_data;
std::wstring output_data(L"Hello. Please type your name");
std::wcout << output_data;
std::wcin >> input_data;
std::wcout << L"Your name is " << input_data;
return 0;
}
Alternatively, you could switch everything to narrow strings:
#include <string>
#include <iostream>
int main()
{
std::cout << "Hello World...";
std::string input_data;
std::string output_data("Hello. Please type your name");
std::cout << output_data;
std::cin >> input_data;
std::cout << "Your name is " << input_data;
return 0;
}

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.