'undeclared identifier" while trying to make an instance in c++ - c++

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.

Related

How I can use friend functions?

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

Basic Class Inheritance in C++

I am typing the following code and I am getting the following error at line-1
[Error] no matching function for call to 'int_adder::add()
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
As pointed by others in the comment, I have corrected it:-
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1
cout << ia.add(10, 20); //LINE-2
return 0;
}
The statement adder::add() will overide the function add() present in int_adder.
Can't fing exact dupe, but you can make overloads from base class visible by using using directive, example:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
using adder::add; // expose base class overload as our own
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.add(); //LINE-1
ia.adder::add(); // explicit name also works
cout << ia.add(10, 20); //LINE-2
return 0;
}
As the other answer mentions using base class name scope also works. It all depends on your needs and class design.
Basically defining an overload in a derived class prevents implicit method look up from matching base class overloads, so you have to be explicit about it in one way or another.
You have totally two different versions of add in the first place. You are not overriding, you are overloading. You are just providing a new add function that has nothing to do with the other add function of the parent.
So first of all, do you want to override or overload?
Overriding the parent add would like the following:
#include <iostream>
using namespace std;
class adder{
public:
void add(){ cout <<"adder::add() "; }
};
class int_adder : public adder{
public:
int add() override {cout <<"int_adder ::add() ";};
int add(int a, int b){
return (a + b);
}
};
int main(){
int_adder ia;
ia.adder::add(); //LINE-1 <- would work displays adder::add()'s message
cout << ia.add(10, 20); //LINE-2
adder ia = int_adder{}; // now this is interesting
ia.add(); // would work displays adder::add()'s message - cause you did not ask for virtuality
return 0;
}

Issue with a friend function in a class in C++?

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

Program in c++ to use 2 classes and find maximum of 2 numbers

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

boost::phoenix::static_cast_ compile errors

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