Program has errors, binary operators - c++

#include <iostream>
#include <fstream>
using namespace std;
class InsurancePolicy
{
friend fstream& operator<<(fstream&, InsurancePolicy);
friend istream& operator>>(istream&, InsurancePolicy&);
private:
int policyNum;
string lastName;
int value;
int premium;
};
fstream& operator<<(fstream& out, const InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
istream& operator>>(istream& in, InsurancePolicy& pol)
{
in >> pol.policyNum >> pol.lastName >> pol.value >> pol.premium;
return in;
}
int main() {
ofstream outFile;
outFile.open("Policy.txt");
InsurancePolicy aPolicy[10];
for (int count = 0; count < 10; ++count)
{
printf("Enter the policy number, the holder's last name, the value, and the premium.");
cin >> aPolicy[count];
outFile << aPolicy[count] << endl;
}
return 0;
}
This program will not compile because of the following errors:
Severity
Code
Description
Project
File
Line
Suppression State
Error
C2679
binary '>>': no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
21
Error
C2679
binary '<<': no operator found which takes a right-hand operand of type 'const std::string' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
16
Error
C2679
binary '<<': no operator found which takes a right-hand operand of type 'InsurancePolicy' (or there is no acceptable conversion)
Project6
c:\users\preston freeman\source\repos\jave.cpp
32
How do I fix these errors?
Thank you for your time.

You are using
ofstream outFile;
in main.
But your operator overloading is
friend fstream& operator<<(fstream&, InsurancePolicy);
Note that ofstream is not derived from fstream, that's why overloading operator << for fstream won't make it work for ofstream
You can simply change fstream to ofstream
ofstream& operator<<(ofstream& out, const InsurancePolicy pol)
{
out << pol.policyNum << " " << pol.lastName << " " << pol.value << " " << pol.premium << endl;
return out;
}
And better to pass in InsurancePolicy by const& since ofstream won't change it, and copy a user defined object can be expensive
ofstream& operator<<(ofstream& out, const InsurancePolicy& pol)
You may also need to #include <string>
Demo:
https://ideone.com/rJiw21

Related

C++ - ofstream overloaded operator '<<' is ambiguous?

I am having trouble solving this error.
Use of overloaded operator '<<' is ambiguous (with operand types 'std::ofstream' (aka 'basic_ofstream') and 'std::string' (aka 'basic_string<char, char_traits, allocator>'))
My first assumption was because of the similar ostream operator, but no luck.
Any help is much appreciated.
maintest.cpp
#include <iostream>
#include <fstream>
#include <string>
#include "player.h"
int main() {
//#########################################################################
// TEST - Overloading Stream Operators
//**************************************************************
// Test ostream & istream
/*std::cout << "\n---ISTREAM/OSTREAM TEST---\n";
Player playerS1;
std::cin >> playerS1;
std::cout << "Test output: ";
std::cout << playerS1;*/
//**************************************************************
// Test ofstream
std::cout << "\n---OFSTREAM TEST---\n";
Player playerS2;
int playerScoreS2(66);
std::string playerNameS2("Tanner");
playerS2.SetName(playerNameS2);
playerS2.SetScore(playerScoreS2);
std::ofstream outFile;
outFile.open("testFile");
if(!outFile.is_open()) {
std::cout << "Could not open file." << std::endl;
}
else {
std::cout << "File opened." << std::endl;
}
outFile << playerS2;
std::cout << "File closed." << std::endl;
outFile.close();
// Return here!!!
return 0;
}
player.h
#ifndef PLAYER_H // MACRO guard
#define PLAYER_H
#include <iostream>
#include <fstream>
#include <string>
class Player {
public:
Player(); // Default constructor
Player(const int &score);
Player(const std::string &name);
Player(const std::string &name, const int &score);
// Output functions
std::string toString() const;
std::string toFileString() const;
friend std::ostream& operator<<(std::ostream& out, const Player& line);
friend std::ofstream& operator<<(std::ofstream& outFS, const Player& line);
friend std::istream& operator>>(std::istream& in, Player& line);
friend std::ifstream& operator>>(std::ifstream& inFS, Player& line);
private:
std::string name;
int score;
};
player.cpp
/*********************
OTHER MEMBER FUNCTIONS
*********************/
std::string Player::toString() const {
// What is the format????
// Using comma delimited for now.
return name + ',' + std::to_string(score);
}
std::string Player::toFileString() const {
return name + ',' + std::to_string(score);
}
/****************************
INPUT/OUTPUT STREAM OPERATORS
****************************/
/*std::ostream& operator<<(std::ostream& out, const Player& line) {
out << line.toString() << std::endl;
return out;
}
std::istream& operator>>(std::istream& in, Player& line) {
std::cout << "\nEnter Name: ";
in >> line.name;
std::cout << "Enter Score: ";
in >> line.score;
return in;
}*/
/*std::ifstream& operator>>(std::ifstream& inFS, Player& line) {
inFS >> line.name >> line.score;
return inFS;
}*/
std::ofstream& operator<<(std::ofstream& outFS, const Player& line) {
outFS << line.toFileString() << std::endl;
return outFS;
}
My assumption, based on the assignment, was that the ostream and ofstream operators were essentially the same but using two different functions outputing in different formats. (They currently output the same format).
Any advice?

Error: No match for 'operator>>' Overloading istream operator

learning C++ right now and ran into a bit of a problem. While trying to complete an example and make sure it works ran into the error:
error: no match for 'operator>>' (operand types are 'std::istream' and 'const int')
conversion of argument 1 would be ill-formed
Here is my code,
#include <iostream>
#include <sstream>
#include <cstdlib>
using namespace std;
class Distance {
private:
int feet;
int inches;
public:
Distance() {
feet = 0;
inches = 0;
}
Distance(int f, int i) {
feet = f;
inches = i;
}
friend ostream &operator<<( ostream &output, const Distance &D ) {
output << D.feet << "\'" << D.inches << "\"" << endl;
return output;
}
friend istream &operator>>( istream &input, const Distance &D ) {
input >> D.feet >> D.inches;
return input;
}
};
int main() {
Distance D1(11,10), D2(5,11), D3;
cin >> D3;
cout << "First Distance : " << D1 << endl;
cout << "Second Distance : " << D2 << endl;
cout << "Third Distance : " << D3 << endl;
return 0;
}
Trying to overload the istream and ostream operators, but running into problems with the istream operator >>.
First thought to convert the variable D.feet and D.inches to char* but that doesn't seem right considering that I have to feed an int into the variables. Not sure what is wrong with my code, can anyone help?
Remove const in >> operator overload.
Your Distance is const'd.
[SOLVED]
Figured out the problem in this was that the 'const' in
ostream &operator>>( istream &input , const Distance &D )
Can't explain the actual processes and why this is a conflict, but perhaps somebody else could please explain? I'd really like to know it in depth.
Thanks!

Why overloaded operator << works sometimes but other times doesn't

I don't know why cout << da << '\n' works fine,but std::cout << next_Monday(da) << '\n' went wrong. Why the direct Date object can output, but the return Date can't.
Why overloaded operator << works sometimes but other times doesn't.
here is my code..
#include <iostream>
#include <stdlib.h>
struct Date {
unsigned day_: 5;
unsigned month_ : 4;
int year_: 15;
};
std::ostream& operator<<(std::ostream& out,Date& b)
{
out << b.month_ << '/' << b.day_ << '/' << b.year_;
return out;
}
std::istream& operator>>(std::istream& in,Date& b);
Date next_Monday(Date const &d);
int main()
{
Date da;
std::cin >> da;
std::cout << da << '\n';
std::cout << next_Monday(da) << '\n';
return 0;
}
this is what clang said: (I use g++ to invoke)
excercise19DateManipulate.cpp:114:18: error: invalid operands to binary
expression ('ostream' (aka 'basic_ostream<char>') and 'Date')
std::cout<< next_Monday(da) <<'\n';
~~~~~~~~^ ~~~~~~~~~~~~~~~
Because you can't bind a temporary to a non-const lvalue reference. Change the operator to take a const reference:
std::ostream& operator<<(std::ostream& out, const Date& b)
You never defined the "next_Monday()" function as far as I can see, you only declared it.

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

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