C++ compile error, after fixing The include file error [duplicate] - c++

This question already has answers here:
What is the difference between private and protected members of C++ classes?
(19 answers)
What are public, private and protected in object oriented programming?
(7 answers)
Closed 1 year ago.
Dear all I have this code running on visual studio . I am getting error as below
I have taken example from books. I have installed visual studio recently. I have tested basic code working well.
I am getting Error for Include files so try to correct Include path error. Kindly suggest me what can be done
Link video
IntialError
Correction Made
#include <iostream>
#include <cstring>
using namespace std;
class BaseClass1
{
private:
int Num1;
public:
void GetNumber(int);
};
class BaseClass2
{
private:
int Num2;
public:
void GetNumber1(int);
};
class Derived :public BaseClass1,public BaseClass2
{
public:
void Display_Result(void);
};
//************************************************************
void BaseClass2::GetNumber1(int b)
{
Num2=b;
}
//************************************************************
void BaseClass1::GetNumber(int a)
{
Num1=a;
}
//************************************************************
void Derived::Display_Result()
{
cout<<"Num1"<<Num1<<endl;
cout<<"Num2"<<Num2<<endl;
cout<<"Result"<<(Num2*Num1)<<endl;
}
int main(int argc, char** argv) {
Derived D;
D.GetNumber(50);
D.GetNumber1(100);
D.Display_Result();
return 0;
}
Error I am getting
C:\Desktop\C++ coding\Visual_basic> cd "c:Desktop\C++ coding\Visual_basic\" ; if ($?)
{ g++ Inheitance.cpp -o Inheitance } ; if ($?) { .\Inheitance }
Inheitance.cpp: In member function 'void Derived::Display_Result()':
Inheitance.cpp:55:16: error: 'int BaseClass1::Num1' is private within this context
cout<<"Num1"<<Num1<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~
Inheitance.cpp:56:16: error: 'int BaseClass2::Num2' is private within this context
cout<<"Num2"<<Num2<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:19: error: 'int BaseClass2::Num2' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:17:7: note: declared private here
int Num2;
^~~~
Inheitance.cpp:57:24: error: 'int BaseClass1::Num1' is private within this context
cout<<"Result"<<(Num2*Num1)<<endl;
^~~~
Inheitance.cpp:9:7: note: declared private here
int Num1;
^~~~

Related

C++, conflicting between library function and inhertited class function [duplicate]

This question already has an answer here:
c++ member function hides global function
(1 answer)
Closed 5 months ago.
#include <iostream>
#include <unistd.h>
using namespace std;
class A{
public:
bool close(){ return true; }
};
class B: public A{
public:
void fun(){
(void) close(1);
}
};
int main() {
B b;
b.fun();
return 0;
}
In class B I want to call the function close(1) which is in library unistd.h. FYI: close(int fileDes) used to close the file descriptors in process.
So how to overcome the issue. I got the below error:
source.cpp: In member function 'void B::fun()':
source.cpp:11:27: error: no matching function for call to 'B::close(int)'
11 | (void) close(1);
| ^
source.cpp:6:14: note: candidate: 'bool A::close()'
6 | bool close(){ return true; }
| ^~~~~
source.cpp:6:14: note: candidate expects 0 arguments, 1 provided
So how to overcome the issue, so that It will call the function in unistd.h file.
In the scope of the member function fun the name close as an unqualified name is searched in the scope of the class
void fun(){
(void) close(1);
}
And indeed there is another member function close in the base class with such a name.
So the compiler selects this function.
If you want to use a function from a namespace then use a qualified name as for example
void fun(){
(void) ::close(1);
}

virtual method of header give errors during g++ compilation

I'm initiating to C++ and I'm struggling with a compiling problem
I have a source file "binomial.cpp" in which I define the methods of my classes :
#include "binomial.hpp"
using namespace std;
int Bernoulli::operator()(){
return (rand() < p*RAND_MAX) ? a : b;
};
int Binomial::operator()(){
int result(0);
for(int i(0);i<n;++i){
int a;
a = B();
result += a;
};
return result;
};
and a header file "binomial.hpp" where I declare all my classes :
#include <iostream>
#ifndef BINOMIAL
#define BINOMIAL
class RandVar {
virtual int operator()() =0;
};
struct Bernoulli : public RandVar {
Bernoulli(int a = -1,int b = 1, double p = 0.5) : a(a), b(b), p(p) {};
int operator()(){};
private:
int a,b;
double p;
};
class Binomial : public RandVar {
public:
Binomial(Bernoulli B, int n=2)
: B(B), n(n) {}
int operator()(){};
private:
Bernoulli B;
int n;
};
#endif
But when I try to compile that through g++ using the command : g++ -Wall binomial.cpp -o binomial those errors occur :
binomial.hpp: In member function ‘virtual int Bernoulli::operator()()’:
binomial.hpp:14:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.hpp: In member function ‘virtual int Binomial::operator()()’:
binomial.hpp:26:19: warning: no return statement in function returning non-void [-Wreturn-type]
int operator()(){};
^
binomial.cpp: At global scope:
binomial.cpp:4:5: error: redefinition of ‘int Bernoulli::operator()()’
int Bernoulli::operator()(){
^~~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:14:6: note: ‘virtual int Bernoulli::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
binomial.cpp:8:5: error: redefinition of ‘int Binomial::operator()()’
int Binomial::operator()(){
^~~~~~~~
In file included from binomial.cpp:2:0:
binomial.hpp:26:6: note: ‘virtual int Binomial::operator()()’ previously defined here
int operator()(){};
^~~~~~~~
I don't really know how to fix that so if someone can take some time to help a beginner it would be great !
You should replace both
int operator()(){};
with
int operator()();
in the header files. You meant to provide declarations, not definitions. To provide just a declaration (not provide the code right away), just drop the {}.

Accessing nested class in C++

I came across this question in an online test that I was taking. The task is to alter this program to get rid of compilation errors.
#include<iostream>
#include<iomanip>
class Vehicle
{
public:
static Car* createCar()
{
return new Car;
}
class Car
{
public:
string name;
};
private:
int seats;
};
void useVehicle()
{
Vehicle::Car *c = Vehicle::createCar();
c->name = "BMW";
}
int main(int argc, char *argv[])
{
useVehicle();
return 0;
}
The compilations errors are like:
error: ‘Car’ does not name a type
error: ‘string’ does not name a type
In function void useVehicle():
error: ‘createCar’ is not a member of ‘Vehicle’
How do I get it right ? I tried few things but could not resolve these errors.
error: ‘Car’ does not name a type
At the point of
static Car* createCar()
Car is not yet known. Move the definition of class Car above the function
error: ‘string’ does not name a type In function ‘void useVehicle()’:
#include <string>
also use std:: to qualify string
error: ‘createCar’ is not a member of ‘Vehicle’
This error will disappear once you fix the other two issues. The compiler wasn't able to parse the function declaration because it didn't know what its return type was.

object as function argument

I have written a simple program.
I am getting this error:
time.cpp: In function ‘int main()’:
time.cpp:22:9: error: expected ‘;’ before ‘a’
time.cpp:23:4: error: ‘a’ was not declared in this scope
time.cpp:24:4: error: ‘b’ was not declared in this scope
time.cpp:25:4: error: ‘c’ was not declared in this scope
This is my code:
#include<iostream>
using namespace std;
class time
{
int hour;
int min;
public:
void gettime(int h,int m)
{
hour=h;
min=m;
}
void puttime()
{
cout<<hour<<endl<<min;
}
void sum(time x,time y)
{
min=x.min+y.min;
hour=min/60;
min=min%60;
hour=hour+x.hour+y.hour;
}
};
int main()
{
time a,b,c;
a.gettime(2,45);
b.gettime(3,35);
c.sum(a,b);
a.puttime();
b.putime();
c.puttime();
return 0;
}
Remember that there is a standard function named time.
This is the one main reason you should refrain from using namespace std;.
b.putime() must be b.puttime() here. Otherwise this code compiled

c++: Class X has no member named Y

I´ve got this CRac class and it´s giving me some problems with its members.
//Definition TAD CRac.hpp
#ifndef CRAC_H
#define CRAC_H
namespace bblRac{
struct Racional{
int num, denom;
};
class CRac{
public:
CRac();
void read();
void asignarVal (const CRac& otroRac);
void write();
void add(const CRac& otroRac)const;
private:
Racional rac;
void simplif();
}; //End of class CRac
} //End of namespace bblrac
#endif
In a ccp file I have
#include "CRac.hpp"
using namespace bblRac;
void CRac::add(const CRac& otroRac)const{
CRac res;
res.num= rac.num + otroRac.num; //line 98
res.denom= rac.denom + otroRac.denom;
}
And when I run it, the output is
CRac.cpp:98: error: ‘class bblRac::CRac’ has no member named ‘num’
CRac.cpp:98: error: ‘const class bblRac::CRac’ has no member named ‘num’
CRac.cpp:99: error: ‘class bblRac::CRac’ has no member named ‘denom’
CRac.cpp:99: error: ‘const class bblRac::CRac’ has no member named ‘denom’
I have tried to fix it with the pointer this, but it continues giving the same mistake..
Thank you!
This will fix it.
#include "CRac.hpp"
using namespace bblRac;
void CRac::add(const CRac& otroRac)const{
CRac res;
res.rac.num= rac.num + otroRac.rac.num; //line 98
res.rac.denom= rac.denom + otroRac.rac.denom;
}
Your CRac class contains a Racional member variable, num and denom are not members of CRac.
res.rac.num = whatever;
otroRac.rac.num = whatever;
Please look your code over before you hit copy/paste into StackOverflow.