I want to have my grid class constructor to take in drivers_location parameters , but it keeps giving me these errors.
https://imgur.com/a/y4MZqso
#include <iostream>
#include <string>
using namespace std;
class drivers_location {
public:
drivers_location() = default;
drivers_location(string name, float xx, float yy){
x = xx;
y = yy;
name = driver_name;
}
private:
float x{};
float y{};
string driver_name;
};
class grid {
public:
grid() = default;
grid(drivers_location(string name, float xx, float yy));
private:
};
int main() {
drivers_location p;
float pointx{ 2.0 };
float pointy{ 3.0 };
grid m[5];
m[0] = { {"abdul" , pointx, pointy }};
}
I want the grid to take in parameters of drivers_location without using inheritance if that's possible
The correct syntax for declaring a constructor takes argument of type driver_location is as shown below. Note that you don't have to specify the 2 parameters of driver_location when defining the constructor for grid that has a parameter of type driver_location.
class grid {
public:
grid() = default;
//---vvvvvvvvvvvvvvvv---->this is how we specify that this ctor has a parameter of type drivers_location
grid(drivers_location){
//add your code here
}
private:
};
I would also recommend using a good c++ book.
Related
So i created the class Point and want to use it as the parameter of the constructor in the class Circle , but the error : There is no default constructor for class "Point" shows up and I dont know how to fix it. The code is represented below this text:
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
Circle(Point q, int r) {
centre = q;
radius = r;
}
};
int main() {
Point obj = Point(3, 4);
Circle obj = Circle(obj, 3);
}
The first problem is that when the constructor Circle::Cirlce(Point, int) is implicitly called by the compiler, before executing the body of that ctor, the data members centre and radius are default initialized. But since you've provided a user-defined ctor Point::Point(int, int) for class Point, the compiler will not synthesize the default ctor Point::Point(). Thus, the data member centre cannot be default initialized.
To solve this you can use constructor initializer list as shown below. The constructor initializer list shown below, copy initialize the data member centre instead of default initializing it.
class Point {
private:
int x, y;
public:
Point(int X, int Y) {
x = X;
y = Y;
}
};
class Circle {
private:
int radius;
Point centre;
public:
//--------------------------vvvvvvvvvvvvvvvvvvvv--->constructor initializer list used here
Circle(Point q, int r): radius(r), centre(q)
{
}
};
int main() {
Point obj = Point(3, 4);
Circle circleObj(obj,4);
}
Demo
Additionally, you had 2 objects with the same name obj inside main.
So I'm a total noob at C++, I decided to learn C++ and skipped directly to the Object-oriented programming. I'm coding a class called KineticEnergy that has a constructor with the parameters x and y which is assigned to the variables mass and velocity.
I have a class method called result() which calculates the Kinetic Energy using its formula. I want to call the parameters from my constructor within the formula but I have no idea what I'm exactly doing here (bad english, don't know how to explain). I am getting errors like "[Error] x was not declared in this scope". Here is the code I written:
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return (1/2) * (x * (pow(y, 2)));
} // What am I gonna do here for this to work?
};
int main()
{
double a = 12.1;
double b = 6.4;
KineticEnergy ke(a, b);
cout << ke.result();
return 0;
}
It is not necessary. your constructor parameters is saved in "mass" and "velocity" as class members.
double result()
{
return (1./2.) * (mass * (pow(velocity , 2.)));
}
Parameters of the parameterized constructor are not member variables. That's why you are storing param values in member variables inside of the parameterized constructor. So that, you should use member variables inside of the result() function.
try this
#include <iostream>
#include <cmath>
using namespace std;
class KineticEnergy
{
public:
double mass;
double velocity;
KineticEnergy(double x, double y) {
mass = x;
velocity = y;
}
double result()
{
return 0.5 * (mass * pow(velocity, 2));
}
};
int main()
{
double a = 12.1;
double b = 6.4;
double Result;
KineticEnergy ke(a, b);
Result = ke.result();
cout << Result;
}
x and y were declared in your constructor, therefore only known by your constructor. you cannot use them outside of it. however, mass and velocity are known variables of your class and can be used anywhere as long as they are public.
in your main you give mass and velocity of your ke object values, that's why you can call any method of your class that uses these variables after(again, as long as they're public)
In the shape.cpp I need set a default constructor for class Point, But I don't know how to do that.
Thanks
Point::Point(double _f, double _g){
f = 1;
g = 1;
}
Rectangle::Rectangle():Point(1, 1) {
x = 1;
y = 1;
}
Rectangle::Rectangle( Point q, double l, double w):x(l),y(w),Point(q) {
}
#include <iostream>
using namespace std;
class Point{
int f,g; //declaring point(f,g)
public :
Point(); //This will be your default constructor
Point(double _f,double _g); // Constructor for class Point
int getf(){ //just for purpose of display function() below. You can remove this once you understand code.
return f;
}
int getg(){ //just for purpose of display function() below. You can remove this once you understand code.
return g;
}
};
class Rectangle{
double l,w; //declaring variable to store length and width of Rectangle
Point pt; //whatever your use of point is
public :
Rectangle(Point q,double l,double w); //constructor for class Rectangle
void display(){ // displaying the class...just for demonstration
cout<<"l = "<<l<<endl;
cout<<"w = "<<w<<endl;
cout<<"pt = ("<<pt.getf()<<", "<<pt.getg()<<")\n";
}
};
//Defining the constructor of class Point
Point::Point(double _f, double _g):f(_f),g(_g){} // I have used initialiser list here
Point::Point():f(0),g(0){} // (your needed default constructor)
Rectangle::Rectangle( Point q, double l, double w):l(l),w(w),pt(q) {} //Defining the constructor of class Rectangle
int main()
{ //Demonstrating object creation and displaying of object r of class rectangle
Point p(1,2);
Rectangle r(p,5,10);
r.display();
}
I have attached the code that would help you understand about constructors and how to define them.
Hope this solves your question !
class Point{
int f,g;
public:
Point();
};
Point::Point(){
f = 1;
g = 1;
}
int main(){
Point *p = new Point() ;
}
I need help building a constructor that would initialized the respective data when instantiated within
the main().
#include <iostream>
using namespace std;
class Entity{
public:
int x, int y, char icon; };
int main(){
Entity pData;
pData.x=4; pData.y=3, pData.icon='1';
cout<<pData.x<<'\n'\; cout<<pData.y<<'\n'\; cout<<pData.icon<<\'n'\;
}
I included an example of what i need only... there no need to include all the program. Anyways I need the constructor to initialized the data in the main as soon as the instance(pData) of Entity is created: I know the constructor has to be something like
Entity::Entity(int x, int y, char icon){};
and once instantiated in the main it would be something like
Entity pData{3,4,'1'};
but obviously this isn't working for me
oh by the way I need a constructor because that's what the assignment is asking in the first place here you go copied right off from the doc file
"write a parameterized constructor for the Entity class that sets x, y, and icon, and use it when creating the instance"
Actually u have not defined constructor for your class Entity(But compiler have defined it for you but it only allocate memory for member variable of Entity).
class Entity {
public:
int x,y;
char icon;
Entity(int _x, int _y,char _icon)
{
x=_x;
y=_y
icon=_icon;
}
};
int main()
{
Entity obj(4,3,'I');
return 0;
}
`
Entity::Entity(int a, int b, char c)
{
x = a;
y = b;
icon = c;
};
Define your constructor like this...
And call it like:-
Entity pData( 1,2,'a');
You don't need to add a constructor because your class is an aggregate. This would work (after fixing some typos)
class Entity
{
public:
int x, y;
char icon;
};
#include <iostream>
int main()
{
using std::cout;
Entity pData{3,4,'1'};
cout << pData.x <<'\n';
cout << pData.y <<'\n';
cout << pData.icon <<'\n';
}
You can for example define a constructor the following way
class Entity{
public:
Entity( int x, int y, char icon ) : x( x ), y( y ), icon( icon ) {}
int x, int y, char icon;
};
And use it like
Entity pData( 4, 3,'1' );
or
Entity pData { 4, 3,'1' };
or
Entity pData = { 4, 3,'1' };
Take into account that for your original class definition you could write
Entity pData = { 4, 3,'1' };
without defining explicitly a constructor.
first of all I know that this is not possible in C++. But I hope someone can tell be a workaround for my problem. I have a class which represents a mathematical function:
class myClass:
{
private:
public:
myClass() {};
double value(double, double){ /* doing some complicated calculation here */} };
double integrate { /*calc*/ return integral; };
}
In integrate() I want to create a struct with a reference to value(). The struct is defined as follows:
struct gsl_monte_function_struct {
double (*f)(double * x_array, size_t dim, void * params);
size_t dim;
void * params;
};
(I need this struct to call the Monte-Carlo integration routines from GSL)
As said before I know that this is forbidden in C++. But is there any possibility to use gsl_monte_function_struct with a member function of myClass? If it is not possible that myClass can integrate itself, is it possible to call gsl_monte_function_struct from outside the class with value() as reference? Thanks in advance!
If understand you corretly, you want a pointer to a member function of myClass. You can achieve this by declaring the member function pointer as:
double (myClass::*value)(double,double)
This function can later be called on an instance as:
(instance.*value)(x,y);
Alternatively you can use std::bind to create a function object which can be called as an ordinary function without having to keep track of the instance on which it is called after the call to std::bind:
auto& value = std::bind(myClass::value, instance);
// ....
value(x,y);
Ok so far I found two solutions:
1) (General solution) Using an abstract base class which has a static pointer to the current instance and a static function that calls a function of the derived class. The static function can be used with a function pointer.
Example:
struct gsl_monte{
double (*f)(double y);
};
class myBase {
private:
static myBase* instance;
public:
myBase(){};
static void setInstance(myBase* newOne);
virtual double value(double x) =0;
static double callValue(double x);//{return value(x);}
};
class myClass : public myBase {
public:
myClass(){};
double value(double x) { return x; };
};
myBase* myBase::instance = new myClass();
double myBase::callValue(double x){return instance->value(x);}
void myBase::setInstance(myBase* newOne){instance=newOne;};
double g(double xx) {return xx;};
int main(int argc, char** argv ){
double x[2]; x[0]=1.3; x[1]=1.3;
myClass* instance = new myClass();
myBase::setInstance(instance);
instance->value(3);
std::cout << "Test " << myBase::callValue(5) << std::endl;
gsl_monte T;
T.f=&myBase::callValue;
double (*f)(double y, void*) = &myBase::callValue;
}
2) (Solution specific to my problem) Fortunatly the desired function accepts a parameter pointer, which I can use to pass the current object:
#include <iostream>
#include <functional>
using namespace std::placeholders;
struct gsl_monte{
double (*f)(double y, void*);
};
class myClass {
public:
myClass(){};
double value(double x) { return x; };
};
double valueTT(double x, void* param) { return static_cast<myClass*>(param)->value(x); };
int main(int argc, char** argv ){
double x[2]; x[0]=1.3; x[1]=1.3;
myClass* instance = new myClass();
instance->value(3);
gsl_monte T;
T.f=&valueTT;
double (*f)(double y, void*) = &valueTT;
}