No operator << matches these operands WITH #include<string> - c++

I've been having this same error for two days now: the best version of my code till now is below, and the compiler keeps complaining that "no operator << matches these operands", though I did #include as was suggested in an other topic.
Also I don't know if I'm supposed to put all there headers and stuff in my post cause it's rather crowded that way..
The whole program isn't really all that relevant, I am trying to create a pure virtual function "vector Toestand (int)" (so it should return a vector and have an int as argument). For some reason this never works and that's why I used an other program of which I was certain it did work and I totally stripped it. Still, no luck so far..
I marked the most important pieces that show at what points c++ disagrees with me
Base class header:
#ifndef BasisToestand_H
#define BasisToestand_H
#include<vector>
using std::vector;
#include <string>
using std::string;
class BasisToestand
{
public:
BasisToestand (const string="geen naam");
virtual ~BasisToestand();
//void setName( const string );
// Get Functions are all declared const
const string getName() const;
virtual const double getVal(double) const = 0; //THIS WORKS
//virtual const vector<double> Toestand(int) const = 0; //THIS DOES NOT
// Overloaded operators is also declared const and virtual
virtual const double operator()(double = 0.) const = 0; //THIS WORKS
private:
string T_Naam;
};
#endif
Base class cpp:
#include <iostream>
using std::cout;
using std::endl;
#include<vector>
using std::vector;
#include <string>
using std::string;
#include <cstdlib>
using std::exit;
#include "BasisToestand.h"
BasisToestand::BasisToestand (const string nieuwe_naam)
: T_Naam(nieuwe_naam)
{
cout << "calling base class BasisToestand constructor for " << T_Naam << endl;
}
BasisToestand::~BasisToestand()
{
cout << "calling base class BasisToestand destructor for " << T_Naam << endl;
}
const string BasisToestand::getName () const
{
return T_Naam;
}
Derived class header:
#ifndef T_Tsunami_H // preprocessor wrapper
#define T_Tsunami_H
#include <string>
using std::string;
#include "BasisToestand.h" // base class header
const static double PI = 3.1415926535897932384626433832795;
class T_Tsunami : public BasisToestand
{
public:
// Constructor with default arguments
T_Tsunami (const double norm = 1., const double mean = 0.,
const double sigma = 1., const string="T_Tsunami");
~T_Tsunami(); // destructor
// Set Functions
void setNorm( const double norm = 1. );
void setMean( const double mean = 0. );
void setSigma( const double sigma = 1. );
// Get Functions are all declared const
const double getNorm() const;
const double getMean() const;
const double getSigma() const;
virtual const double getVal(double) const; //THIS WORKS
//virtual const vector<double> Toestand(int) const; //PROBLEM
// Overloaded operators is also declared const
virtual const double operator()(double = 0.) const; //THIS WORKS
private:
double p0;
double p1;
double p2;
};
Derived class .cpp
#include <iostream>
using std::cout;
using std::endl;
#include <cmath>
#include "T_Tsunami.h" // Only T_Tsunami header file needed
T_Tsunami::T_Tsunami (const double norm, const double mean,
const double sigma, const string nieuwe_naam)
: BasisToestand(nieuwe_naam),
p0(norm),
p1(mean),
p2(sigma)
{
cout << "calling derived class T_Tsunami constructor for " << getName() << endl;
}
T_Tsunami::~T_Tsunami()
{
cout << "calling derived class T_Tsunami destructor for " << getName() << endl;
}
const double T_Tsunami::getVal(double x) const
{
return p0/p2/(sqrt(2*PI))*exp(-pow((x-p1),2)/(2*pow(p2,2)));
}
const double T_Tsunami::operator()(double x) const // overloaded () operator WORKS
{
return getVal(x);
}
void T_Tsunami::setNorm (const double norm)
{
p0 = norm;
}
void T_Tsunami::setMean (const double mean)
{
p1 = mean;
}
void T_Tsunami::setSigma (const double sigma)
{
p2 = sigma;
}
const double T_Tsunami::getNorm() const
{
return p0;
}
const double T_Tsunami::getMean() const
{
return p1;
}
const double T_Tsunami::getSigma() const
{
return p2;
}
//THIS IS WHAT MY VIRTUAL FUNCTION "TOESTAND" SHOULD DO FOR THIS DERIVED CLASS
const vector<double> BasisToestand::Toestand(int GOLF) const
{
vector<double>T_1;
for( int i = 0; i < GOLF; i++ )
{ double y = 0.25*(1-tanh(double(i-75)/5));
T_1.push_back(y);
}
cout<< "Creating vector T_1" << endl;
return T_1;
}
Main function:
#include <iostream>
using std::cout;
using std::endl;
using std::scientific;
#include <string>
using std::string;
#include <cmath>
#include <iomanip>
using std::setw;
using std::setprecision;
#include <vector>
using std::vector;
#include "BasisToestand.h"
#include "T_Tsunami.h"
int main()
{
T_Tsunami myTsunami_1;
BasisToestand *funPtr1 = &myTsunami_1;
BasisToestand& funRef1 = myTsunami_1;
cout << "value at x=0 using pointers is " << funPtr1->getVal(0.) << endl; //WORKS
cout << "value at x=0 using references is " << funRef1(0.) << endl; //WORKS
cout << "Testing Tsunami" **<<** funPtr1->Toestand(10) << endl;
//THIS DOES NOT WORK, the bold thing is where I get the error.
return 0;
}

Your question can be reduced to this:
#include <vector>
#include <iostream>
int main() {
std::vector<int> v;
std::cout << v;
}
main.cpp:5:19: error: no match for 'operator<<' (operand types are 'std::ostream {aka std::basic_ostream}' and 'std::vector')
And that error pretty much explains the problem. You can't simply send a container to a stream with operator <<. You can either use functionality that someone has written to do this (Pretty-print C++ STL containers), or simply loop over the contents of the vector and do it yourself.
cout << "Testing Tsunami ";
const vector<double>& Toestand = funPtr1->Toestand(10); //get a reference to the vector
for(int i=0; i<Toestand.size(); ++i) //for each element
cout << Toestand[i] << ','; //print the element and a comma
cout << endl;

Related

`friend struct std::is_invocable` can't access private members of class it is friend to

I have made struct std::is_invocable be a friend of class D, but it is unable to access the private member of D. This happens with gcc, clang, and MS Visual Studio 2017, so it is not one of MSVC's quirks.
#include <iostream>
#include <string>
#include <type_traits>
#include <iomanip>
#include <cmath>
using std::cin;
using std::cout;
const double PI = 3.1415926535898;
class A
{
public:
A(double xx) : x(xx)
{
s = sin(x);
}
double sinx()
{
return s;
}
private:
double x;
double s;
};
class B
{
public:
double sinx(double x)
{
return sin(x);
}
};
class C
{
public:
static double sinx(double x)
{
return sin(x);
}
};
class D
{
private:
double sinx(double x)
{
return sin(x);
}
public:
template<class T, class... Args>
friend struct std::is_invocable;
};
class E
{
public:
double sinx;
};
int main()
{
std::string s;
A a0(PI/2.);
cout << std::boolalpha;
cout << std::is_invocable<decltype(&A::sinx), A&>::value << "\n";
cout << std::is_invocable<decltype(&B::sinx), B&, const double>::value << "\n";
cout << std::is_invocable<decltype(&B::sinx), const double>::value << "\n";
cout << std::is_invocable<decltype(&C::sinx), const double>::value << "\n";
cout << std::is_invocable<decltype(&D::sinx), D&, const double>::value << "\n"; // shouldn't this compile if
// std::is_invocable is friend?
cout << std::is_invocable<decltype(&E::sinx), E&, const double>::value << "\n";
cout << "Press ENTER to exit.\n";
std::getline(cin, s);
}
The error is:
C2248 "'D::sinx': cannot access private member declared in class 'D'"
Why can't std::is_invocable access D::sinx()?
Why can't std::is_invocable access D::sinx() ?
Because it isn't trying to. main is trying to access D::sinx. Which it can't. Remember: accessibility is about who can use the name of the member. You typed the name "D::sinx" in main. Therefore, it is main that is trying to access the name.
So you're giving access to the wrong thing.

Constructor taking Base& is not called

I'm working on a simple program for Boolean algebra, but the double negation does not work as expected.
I have the following classes:
Operator:
#ifndef OPERATOR_H
#define OPERATOR_H
class Operator {
public:
virtual int getArity(void) const = 0;
virtual bool calc(void) const = 0;
};
#endif // OPERATOR_H
False:
#ifndef FALSE_H
#define FALSE_H
#include "operator.h"
class False : public Operator {
public:
int getArity() const {
return 0;
}
bool calc(void) const {
return false;
}
};
#endif // FALSE_H
Not:
#ifndef NOT_H
#define NOT_H
#include "operator.h"
class Not : public Operator {
public:
Not(Operator& child) : m_child(child) {
std::cout << "not constructor called" << std::endl;
}
int getArity(void) const {
return 1;
}
bool calc(void) const {
return !m_child.calc();
}
private:
Operator& m_child;
};
#endif // NOT_H
My main.cpp:
#include <iostream>
#include "operator.h"
#include "not.h"
#include "false.h"
using namespace std;
int main(int argc, char *argv[]) {
False f;
Not n = Not(f);
Not d = Not(n);
cout << "n.calc(): " << n.calc() <<endl;
cout << "d.calc(): " << d.calc() <<endl;
return 0;
}
Since d = Not(Not(False())) I expect it to be false.
The output is:
not constructor called
n.calc(): 1
d.calc(): 1 <== should be 0
Why is the constructor of the class Not not called with an object of type Not as child?
Not d = Not(n); invokes the copy constructor of Not, because the argument is also of type Not. The copy constructor's signature matches better and it's therefore selected.

Operation Overload Multiplication inside a class

I am new to operation overloading. I doing a lab where I need to be able to show the Area and circumference of a circle. The user inputs the radius and the center x,y point. My problem is that I am lost in how to properly execute the operation overload on multiplication. Could some one please help me?
This is the portion of the main.cpp
cout << "====================================================" << endl;
cout << "Radius is: " << circle1.setRadius << endl;
cout << "Area is: " << circle1.setArea << endl;
cout << "Circumference is: " << circle1.setCircumference << endl;
This is my circleTypeImp.cpp
#include <iostream>
#include "circleType.h"
#include "pointType.h"
class CircleType;
const float PI = 3.14;
void CircleType::setRadius (float r)
{
float radius const=r;
}
void CircleType::printCircle() const
{
}
void CircleType::setArea ()
{
return PI * radius * radius;
}
void CircleType::setCircumference ()
{
return 2 * PI * radius;
}
and this is my CircleType.h
#ifndef CIRCLETYPE_H
#define CIRCLETYPE_H
#include "pointType.h"
#include <iostream>
using namespace std;
class CircleType : public PointType
{
public:
void setRadius(float r);
void printCircle() const;
CircleType& operator* (const CircleType& radius);
void setArea();
void setCircumference();
private:
float radius const;
};
#endif
Thank you
Here's what I could come up with to help you out, based on your clarifications.
I've pasted and edited the code you had, and added comments to it when I made changes to detail why those changes where made.
A few of the highlights:
When calling any function, even those without parameters, you still need the ().
Functions that return a value, should not return void but rather the type of the return value. (i.e. float in the cases below).
When you want to set a member variable it's customary to use setVariableName or something similar and when you're returning the value of a member variable (or simple operation on) you can use getVariableName
so as to avoid confusion over what the function should do.
You should avoid using namespace std in header files (.h/.hpp files), so as not to force everyone using your code to also use the same namespaces.
radius shouldn't be declared const as you want to be able to set it's value after creating the object.
when using PI you often want the most accurate representation for your variable type. You can often find this on many systems in cmath as M_PI (#include <cmath>).
I haven't done really anything with the PointType class or the overloaded multiplication operator as 1. I have no clue what the PointType class looks like and the multiplication operator doesn't seem necessary.
Below is a sample program using this class to illustrate how to use the member functions.
CircleType.h
#ifndef CIRCLE_TYPE_H
#define CIRCLE_TYPE_H
#include "PointType.h"
#include <iostream>
// It's common practice to not put "using namespace" in a header file.
// If you do, anyone including your header file has to use it.
class CircleType : PointType {
public:
void setRadius(const float r);
void printCircle() const;
CircleType operator * (const circleType& c) const;
float getRadius() const;
float getArea() const;
float getCircumference() const;
private:
float radius; // Note because you want to set radius
// after creation of a circle object
// radius should not be const
}
#endif
CircleType.cc
#include "CircleType.h" // PointType.h will also be included
#include "PointType.h" // But it's also fine to explicitly include it
#include <iostream>
using namespace std;
// you don't need a dummy class here, In fact that will likely cause a compiler issue
// You should probably use a more accurate value of Pi
const float PI = 3.1415927; // Or use M_PI in "cmath" (if it's defined on your system)
void CircleType::setRadius(const float r){
radius = r;
}
void CircleType::printCircle() const {
// Do whatever you need to print a circle
}
// As far as I can tell, you don't actually need to overload
// multiplication to do any the tasks you mentioned.
CircleType CircleType::operator * (const CircleType& c) const {
CircleType tmp;
// I have no clue what multiplying two circles gives you,
// this is the general form of the multiplication operator
return tmp;
}
// It is customary to name a function that returns a private variable
// or the result of simpler operations on private variables with getXXX
// like the ones below -- Note the return type is not void but
// the actual type you expect.
float CircleType::getRadius() const {
return radius;
}
float CircleType::getArea() const {
return PI * radius * radius;
}
float CircleType::getCircumference() const {
return 2 * PI * radius;
}
main.cc
#include "CircleType.h"
#include "PointType.h"
#include <iostream>
using namespace std;
int main(int argc, char ** argv){
CircleType c1;
float tmp;
cout << "Input radius of circle: ";
cin >> tmp;
c1.setRadius(tmp);
cout << "=============================================" << endl;
// note even for functions that take no parameters, you still need the () to call it
cout << "Radius is: " << c1.getRadius() << endl;
cout << "Area is: " << c1.getArea() << endl;
cout << "Circumference is: " << c1.getCircumference() << endl;
return 0;
}

Compiler Error - Identifier undefined

I have a fairly simple C++ code that doesn't seem to be compiling properly. Essentially, I have some globally defined functions declared in my GLOBAL.HPP file, and are defined in my GLOBAL.CPP file. Then I have a class, EuroOption, that consists of a struct datamember. The class EuroOption has its own member functions that essentially do the same exact thing that the global functions do--so I defined them similarly, and just called global functions inside of the EuroOption member function definitions. Please see below:
//
//GLOBAL.HPP
//
#ifndef GLOBAL_HPP
#define GLOBAL_HPP
#include <iostream>
#include <math.h>
#include <boost/math/distributions/normal.hpp>
#include <boost/math/distributions.hpp> // For non-member functions of distributions
using namespace std;
//using namespace boost::math;
namespace GLOBAL // Encapsulate Point in the Global namespace
{
struct EuroOptionData
{
double r; // Interest rate
double sig; // Volatility
double K; // Strike price
double T; // Expiry date
double b; // Cost of carry
};
double n(double x);
double N(double x);
double CallPrice(EuroOptionData od, double S);
double PutPrice(EuroOptionData od, double S);
double PutParity(EuroOptionData od, double S);
double CallParity (EuroOptionData od, double S);
} // Close namespace GLOBAL
#endif
Here is the EuroOption.HPP file:
//
//
//
#ifndef EUROOPTION_HPP
#define EUROOPTION_HPP
#include <string>
#include "Global.hpp"
using namespace std;
using namespace GLOBAL;
class EuroOption
{
private:
public:
struct EuroOptionData od;
//EuroOption class functions
EuroOption(); // Default call option
EuroOption(const EuroOption& option2); // Copy constructor
virtual ~EuroOption(); //Destructor
//EuroOption Global Function Calls
double EuroCallPrice(EuroOptionData od, double S);
double EuroPutPrice(EuroOptionData od, double S);
double EuroCallParity(EuroOptionData od, double S);
double EuroPutParity(EuroOptionData od, double S);
//EuroOption class operators
EuroOption& operator = (const EuroOption& option2); //Assignment Operator
};
#endif
And a snippet of the EuroOption.CPP file:
//
//
//
#include "EuroOption.hpp"
#include <cmath>
#include <iostream>
using namespace GLOBAL;
{
double EuroOption::EuroCallPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
double EuroOption::EuroPutPrice(EuroOptionData od, double S)
{
return CallPrice(od,S);
};
.....
...
}
And finally, a snippet of my Test.CPP file where I test functionality:
//
//
//
#include "Global.hpp"
#include "EuroOption.hpp"
#include <iostream>
using namespace GLOBAL;
int main()
{
EuroOption Batch1; //Initialize EuroOption class object Batch1
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << EuroCallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << EuroPutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< EuroCallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< EuroPutParity(Batch1.od, S1)<<endl;
//****None of these functions compile. They all state "identifier EuroCallPrice (..etc.) is undefined."
cout << "S1: "; double S1; cin >> S1;
cout << "Stock Call Option: " << CallPrice(Batch1.od, S1) << endl;
cout << "Stock Put Option: " << PutPrice(Batch1.od, S1) <<endl;
cout << "Put Call Parity - Call Option:"<< CallParity(Batch1.od, S1)<<endl;
cout << "Put Call Parity - Put Option: "<< PutParity(Batch1.od, S1)<<endl;
//****These functions all compile properly. They are the original global functions.
I realize this is a lot of code to sift through, but any ideas would be greatly appreciated. As noted in the above code, the original global functions work perfectly, but I want to use the class EuroOption function to call that global function.
Many thanks!
Silly me! All i needed to do was call the EuroCallPrice...etc functions on Batch1.
Thanks for all your help!

Trouble with abstract classes in c++

main:
#include <iostream>
#include <string>
#include "serviceChargeChecking.h"
int main()
{
serviceChargeChecking newAccount("Crim", 111222, 50.00, 100, 1.00);
system("PAUSE");
return 0;
}
serviceChargeChecking.h:
#ifndef H_serviceChargeChecking
#define H_serviceChargeChecking
#include "checkingaccount.h"
#include <string>
class serviceChargeChecking: public checkingAccount
{
public:
void setMonthlyFee(double);
void writeCheck(int);
void getMonthlyStatement() const;
serviceChargeChecking(std::string =" ",int = 0, double = 0.00, int= 0, double = 0.00);
private:
double serviceCharge;
};
#endif
serviceChargeChecking.cpp:
#include "serviceChargeChecking.h"
#include <iostream>
using std::string;
void serviceChargeChecking::setMonthlyFee(double fee)
{
serviceCharge=fee;
}
void serviceChargeChecking::getMonthlyStatement() const
{
checkingAccount::getMonthlyStatement();
std::cout<< "Service Charge: " << serviceCharge << std::endl;
}
void serviceChargeChecking::writeCheck(int ammount)
{
if(checkingAccount::getChecks()>0)
{
checkingAccount::setChecks(checkingAccount::getChecks()-ammount);
}
else
{
std::cout<<"No checks available." << std::endl;
}
}
serviceChargeChecking::serviceChargeChecking(string name, int acct, double bal, int numCheck, double sCharge)
{
bankAccount::setAcctOwnersName(name);
bankAccount::setAcctNum(acct);
bankAccount::setBalance(bal);
checkingAccount::setChecks(numCheck);
serviceCharge=sCharge;
}
checkingAccount.h:
#ifndef H_checkingAccount
#define H_checkingAccount
#include "bankAccount.h"
#include <iostream>
class checkingAccount: public bankAccount
{
public:
virtual void writeCheck()=0;
void deposit(double);
void withdraw(double);
void getMonthlyStatement() const;
int getChecks();
void setChecks(int);
private:
int numChecks;
};
#endif
checkingAccount.cpp:
#include "checkingAccount.h"
#include <iostream>
int checkingAccount::getChecks()
{
return numChecks;
}
void checkingAccount::setChecks(int c)
{
numChecks=c;
}
void checkingAccount::deposit(double d)
{
bankAccount::setBalance(bankAccount::getBalance()+d);
}
void checkingAccount::withdraw(double w)
{
bankAccount::setBalance(bankAccount::getBalance()-w);
}
void checkingAccount::getMonthlyStatement() const
{
bankAccount::getMonthlyStatement();
}
bankAccount.h:
#ifndef H_bankAccount
#define H_bankAccount
#include <string>
class bankAccount
{
public:
std::string getAcctOwnersName() const;
int getAcctNum() const;
double getBalance() const;
void getMonthlyStatement() const;
void setAcctOwnersName(std::string);
void setAcctNum(int);
void setBalance(double);
virtual void withdraw(double)=0;
virtual void deposit(double)=0;
private:
std::string acctOwnersName;
int acctNum;
double acctBalance;
};
#endif
bankAccount.cpp:
#include "bankAccount.h"
#include <iostream>
using std::string;
string bankAccount::getAcctOwnersName() const
{
return acctOwnersName;
}
int bankAccount::getAcctNum() const
{
return acctNum;
}
double bankAccount::getBalance() const
{
return acctBalance;
}
void bankAccount::setAcctOwnersName(string name)
{
acctOwnersName=name;
}
void bankAccount::setAcctNum(int num)
{
acctNum=num;
}
void bankAccount::setBalance(double b)
{
acctBalance=b;
}
void bankAccount::getMonthlyStatement() const
{
std::cout << "Name on Account: " << getAcctOwnersName() << std::endl;
std::cout << "Account Id: " << getAcctNum() << std::endl;
std::cout << "Balance: " << getBalance() << std::endl;
}
I know this is a lot of code to go through but can anyone help me understand why i cannot create an object from the class serviceChargeChecking the error is telling me that i cannot create an object from the abstract class but it doesn't seem to be abstract to me.
serviceChargeChecking implements void writeCheck(int), but the pure virtual function from checkingAccount has type void writeCheck(), so it's still pure in serviceChargeChecking, which makes the class abstract.
You have this in the abstract class checkingAccount:
virtual void writeCheck()=0;
but implement this in the derived class serviceChargeChecking:
void writeCheck(int);
The signature must be the same.
The writeCheck() method has different signatures in serviceChargeChecking and checkingAccount.
If you use C++11, use override in order to avoid this kind of error.
It's because your CheckingAcount has writeCheck() and serviceChargeChecking has writeCheck(int);
This probably due to the fact that you failed to Override checkingAccount's, writeCheck method, the abstract prototype was was
in checkingAccount class
virtual void writeCheck()=0;
and in serviceChargeChecking class
void writeCheck(int);
note the parameters, you didn't override checkingAccount's writeCheck you probably inherited it (implicitly), the serviceChargeChecking made a new writeCheck with an int parameter.