i just started working on structures and i am having this problem:
i created a structure "point" thus :
struct point
{
double x,y;
};
i created another structure "rectangle" that contains a point as the origin like this:
struct rectangle {
point origin;
double width, height;
};
in the function main, i created a rectangle carton like this :
point origin={0,0};
rectangle carton={origin,20,60};
but the compiler was telling me that :
error C2440: 'initializing' : cannot convert from 'struct point' to 'double'
No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
but when i created a rectangle like this:
rectangle carton={{0,0,},0,0};
there was no problem, the program run fine
This is the whole source code :
// program test the behavior of structures
#include <iostream>
using std::cout;
using std::endl;
struct point
{
double x,y;
};
struct rectangle {
point origin;
double width, height;
};//declaration of a structure rectangle
void swapReference(point& slave){
double temp = slave.x;
slave.x = slave.y;
slave.y = temp;
}//functions(with ampersand & in parameter) affects the point in question
//for example to reduce the size from main function
void swapValue(point slave){
double temp = slave.x;
slave.x = slave.y;
slave.y = temp;
}//Functions (with no ampersand & in parameter) wont affect the initial point used as arguement
//for example if you want to make requests from the point like
void printPoint(point slave){
cout<<slave.x<<" "<<slave.y<<endl;
}// function to print the point
rectangle increaseSize(rectangle& box,double width,double height){
box.width += width;
box.height += height;
return box;
}
void printRectangle(rectangle box){
cout<<"\nWidth : "<<box.width<<" height : "<<box.height<<endl;
}
int main(){
//playing with points
//declaration of variables
point highest={ 84, 100};
swapValue(highest);
printPoint(highest);
swapReference(highest);
printPoint(highest);
//playing with rectangles
//declaration of variable
point origin={0,0};
rectangle carton={origin,20,60};
increaseSize(carton, 100, 20);
printRectangle(carton);
return 0;
}