I have a class with 3 fields a,b and c. I want to calculate the volume of a box with sides a,b or c. I want to do this with a friend function. However, when I compile the program the Compiler gives an error No global operator found which takes type Box. I would like to ask why is that?
#include "pch.h"
#include <iostream>
using namespace std;
class Box {
double a, b, c;
public:
Box(double sa, double sb, double sc) {
a = sa;
b = sb;
c = sc;
}
friend double calcVolume(Box bx) {
return bx.a*bx.b*bx.c;
};
};
int main() {
Box a(5.67, 6.43, 7.00),b(90,32.76,44.18);
cout << calcVolume(a)<<endl;
return 0;
}
There is a mistake in you code return bx.a*bx.b*bx*c;, which should be return bx.a*bx.b*bx.c; (the last dot)
#include <iostream>
using namespace std;
class Box {
double a, b, c;
public:
Box(double sa, double sb, double sc) {
a = sa;
b = sb;
c = sc;
}
friend double calcVolume(Box &bx) {
return bx.a * bx.b * bx.c;
};
};
int main() {
Box a(5.67, 6.43, 7.00),b(90,32.76,44.18);
cout << calcVolume(a)<<endl;
return 0;
}
A couple of things : use const when you are not modifying the argument, second do not do using namespace std. Now, this works for me
#include <iostream>
class Box
{
double a;
double b;
double c;
public:
Box(double ain, double bin, double cin) {a = ain; b = bin; c = cin;}
friend double calcVol(const Box& rOther)
{
return rOther.a*rOther.b*rOther.c;
}
};
int main()
{
Box a(14.8, 10.8, 11.01), b(8,5.0,6.2);
std::cout<<"vol a :: "<<calcVol(a)<<std::endl;
std::cout<<"vol b :: "<<calcVol(b)<<std::endl;
return 0;
}
Related
I started to learn some oop and I have a question, why I can't put the function in first class, I know there is a way to write the friend method down under second class. If I put it in the first class where it belongs my compiler shows the error C2027: "Error C2027 use of undefined type 'Calculator'"
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <ctime>
using namespace std;
class Calculator;
class PlacadeBaza {
char denumire_procesor[100];
public:
char* getDenumire() {
return denumire_procesor;
}
void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
a.memorie_RAM = memorie;
strcpy(b.denumire_procesor, denumire); //C2027
}
};
class Calculator {
int memorie_RAM;
public:
int getMemorie_RAM() {
return memorie_RAM;
}
friend void PlacadeBaza::set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]);
};
int main() {
Calculator a;
PlacadeBaza b;
int memorie;
char denumire[100];
cin >> memorie >> denumire;
b.set(a, b, memorie, denumire);
cout << a.getMemorie_RAM();
cout << b.getDenumire();
}
Setting aside other issues with the code, if you wish PlacadeBaza to be able to access private members of Calculator you need to declare that PlacadeBaza is a friend class of Calculator.
#include <iostream>
#include <ctime>
using namespace std;
class PlacadeBaza;
class Calculator {
int memorie_RAM;
public:
int getMemorie_RAM() {
return memorie_RAM;
}
friend class PlacadeBaza;
};
class PlacadeBaza {
char denumire_procesor[100];
public:
char* getDenumire() {
return denumire_procesor;
}
void set(Calculator a, PlacadeBaza b, int memorie, char denumire[100]) { //C2027
a.memorie_RAM = memorie;
strcpy(b.denumire_procesor, denumire); //C2027
}
};
int main() {
Calculator a;
PlacadeBaza b;
int memorie;
char denumire[100];
cin >> memorie >> denumire;
b.set(a, b, memorie, denumire);
cout << a.getMemorie_RAM();
cout << b.getDenumire();
}
The C++ code below keeps giving me a nearly constant output of (0.999976..) as calculated by the Psat() function, whereas, the value is supposed to change meaningfully when I adjust saturation temperature, Tsat???
#include <iostream>
#include <cmath>
using namespace std;
class components{
private:
double A, B, C, Tsat;
public:
void AntParam(double A, double B, double C, double Tsat){
A = A, B = B, C = C, Tsat = Tsat ;}
void Psat(){cout << "Vapor Pressure is "
<<pow(10,(A - B/(C + Tsat)))<<" torr"<<endl;}
};
int main(){
components water;
water.AntParam(8.07131, 1730.63, 233.426, 100);
water.Psat();
return 0; };
The deep, dark secret is Shadowing. The double A parameter is narrower in scope than double A the member variable, so the parameter is the sole owner of the identifier A inside the function. You could this->A = A,..., you could use the member's full name components::A = A, ..., you could rename the parameter, or you could rename the member variable.
Me, I'd use a constructor and make use of the member initializer list.
#include <iostream>
#include <cmath>
using namespace std;
class components
{
private:
double A, B, C, Tsat;
public:
components(double A, double B, double C, double Tsat):
A(A), B(B), C(C), Tsat(Tsat)
{
}
void Psat()
{
cout << "Vapor Pressure is " << pow(10, (A - B / (C + Tsat))) << " torr"
<< endl;
}
};
int main()
{
components water(8.07131, 1730.63, 233.426, 100);
water.Psat();
return 0;
}
I want to find the maximum of 2 numbers but instead of the simple method, i need to use 2 classes and friend functions. How to implement it?
I am using the following code but the code isn't working.
#include<iostream>
using namespace std;
class one
{
int a;
public:
friend int cal(one a);
};
class two
{
int b;
public:
friend int cal(one a,two b);
};
cal(int f,int g)
{
int ans=(x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cal(10,20);
}
#include<iostream>
using namespace std;
class biggest
{
private:
int a,b;
public:
void input();
void display();
};
void biggest::input()
{
cout<<"Enter 2 nos.:";
cin>>a>>b;
}
void biggest::display()
{
if(a>b)
cout<<"Biggest no.:"<<a;
else
cout<<"Biggest no.:"<<b;
}
int main()
{
biggest b;
b.input();
b.display();
}
Output
Enter 2 nos.:133 21
Sample Output
Biggest no.:133
By setting a function as a "friend", you give it access to private members of a class. Example looked really strange, but I guess this will do it. Both classes here give private members access to "cal" function.
#include<iostream>
using namespace std;
class one;
class two;
class one
{
int a = 10;
public:
friend int cal(one a,two b);
};
class two
{
int b = 20;
public:
friend int cal(one a,two b);
};
int cal(one x,two y)
{
return (x.a>y.b)?x.a:y.b;
}
int main()
{
one x;
two y;
cout << cal(x,y);
}
I'm encountering a compile error when trying to static cast an argument inside of a boost phoenix lambda. The errors themselves are way too long, so I've posted them to pastebin here.
I've created a minimal example showing what I am trying to do. If I make the variable b into an A pointer and thusly don't cast, then everything compiles and runs correctly. Does anyone know why this is happening?
Minimal example (compile with "clang++ -lboost_thread phoenix_test.cpp"):
#include <boost/phoenix.hpp>
#include <iostream>
using namespace boost;
using namespace phoenix;
using namespace arg_names;
using namespace local_names;
class A
{
public:
A(int a)
: mA(a)
{};
int GetX() const {return mA;};
protected:
int mA;
};
class B : public A
{
public:
B(int a)
: A(a)
{};
int GetX() const { return mA + 1; }
};
int main (void)
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (static_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
The compiler used was clang 3.4, and gcc 4.6.3 was also tried.
The error messages seem to imply that you were using dynamic_cast_ there.
Dynamic casts require virtual classes. Add a virtual member to the base class (e.g. the destructor)
Sample:
Live On Coliru
#include <boost/phoenix.hpp>
#include <iostream>
namespace px = boost::phoenix;
using namespace px::arg_names;
using namespace px::local_names;
class A {
public:
virtual ~A() {}
A(int a) : mA(a){};
int GetX() const { return mA; };
protected:
int mA;
};
class B : public A {
public:
B(int a) : A(a){};
int GetX() const { return mA + 1; }
};
int main()
{
const A* a = new A(3);
const A* b = new B(2);
BOOST_AUTO(test, (_1->*&A::GetX)() + (px::dynamic_cast_<const B*>(_2)->*&B::GetX)());
std::cout << test(a, b) << std::endl;
delete a;
delete b;
return 0;
}
Prints
6
this is my code:
#include "stdafx.h"
#include <iostream>
int main()
{
Box *b = new Box(1,2,3);
}
class Box
{
private:
int a;
int b;
int c;
public:
Box (int aa, int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
};
it doesn't compile.
(i did not split it to an h file and a cpp file)
thanks in advance
Put main after your class
class Box
{
private:
int a;
int b;
int c;
public:
Box (int aa, int bb, int cc)
{
a=aa;
b=bb;
c=cc;
}
};
int main()
{
Box *b = new Box(1,2,3);
}
The forward declaration class Box; at top in this case won't work, however it will work if you only use Box* b;
You need to declare your class above the code that uses it.