Compiler told me that it cannot find copy constructor but I declared and implemented it for both class Lin and Point... What I did wrong...
Many thanks in advance !
1>------ Build started: Project: Exercise 3 Variant, Configuration: Debug Win32 ------
1> main.cpp
1>c:\program files\boost\boost_1_47\boost\variant\detail\initializer.hpp(95): error C2558: class 'Line' : no copy constructor available or copy constructor is declared 'explicit'
1> c:\program files\boost\boost_1_47\boost\variant\detail\initializer.hpp(90) : while compiling class template member function 'int boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node::initialize(void *,const Line &)'
1> with
1> [
1> BaseIndexPair=boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>,boost::mpl::l_iter<boost::mpl::list2<Point,Line>>>::initializer_node,boost::mpl::int_<1>>,
1> Iterator=boost::mpl::l_iter<boost::mpl::list1<Line>>
1> ]
1> c:\program files\boost\boost_1_47\boost\variant\variant.hpp(1208) : see reference to class template instantiation 'boost::detail::variant::make_initializer_node::apply<BaseIndexPair,Iterator>::initializer_node' being compiled
1> with
1> [
1> BaseIndexPair=boost::mpl::pair<boost::detail::variant::make_initializer_node::apply<boost::mpl::pair<boost::detail::variant::initializer_root,boost::mpl::int_<0>>,boost::mpl::l_iter<boost::mpl::list2<Point,Line>>>::initializer_node,boost::mpl::int_<1>>,
1> Iterator=boost::mpl::l_iter<boost::mpl::list1<Line>>
1> ]
1> c:\program files\boost\boost_1_47\boost\variant\variant.hpp(1327) : see reference to class template instantiation 'boost::variant<T0_,T1>::initializer' being compiled
1> with
1> [
1> T0_=Point,
1> T1=Line
1> ]
1> c:\program files\boost\boost_1_47\boost\variant\variant.hpp(1399) : see reference to function template instantiation 'void boost::variant<T0_,T1>::convert_construct<const T>(T &,int,boost::mpl::false_)' being compiled
1> with
1> [
1> T0_=Point,
1> T1=Line,
1> T=Point
1> ]
1> c:\all my\с++\ha level 8\solution\level 8\exercise 3 variant\main.cpp(28) : see reference to function template instantiation 'boost::variant<T0_,T1>::variant<Point>(const T &)' being compiled
1> with
1> [
1> T0_=Point,
1> T1=Line,
1> T=Point
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Code://line.h
#ifndef LINE_H
#define LINE_H
#include "Shape.h"
#include "Point.h"
#include <string>
#include <iostream>
using namespace std;
class Line : public Shape
{
private:
Point start;
string discrib;
Point end;
public:
//constructors and destructor
Line();
Line(string x, Point P_start, Point P_end);
Line(Line& line); // line copy constructor
~Line();
//getters and setters
void SetStart ( Point SomePoint);
void SetEnd ( Point SomePoint);
Point GetStart() ;
Point GetEnd() ;
Line operator = ( Line& l);
// friend ostream& operator << (ostream& os , const Line& l);
//other useful methods
void ToString();
};
#endif
//point.h
#ifndef POINT_H
#define POINT_H
#include "Shape.h"
#include <iostream>
#include <string>
using namespace std;
class Point : public Shape
{
private:
double Xcoord;
double Ycoord;
public:
Point();
Point (double xNew, double yNew);
Point( Point& point); // copy constructor for Point
~Point();
void X( double Value);
void Y( double Value);
double X() ;
double Y() ;
Point operator = ( Point& p);
friend ostream& operator << (ostream& os , Point& p);
void ToString();
};
#endif
// shape.h
#ifndef Shape_h
#define Shape_h
#include <iostream>
#include "stdlib.h"
#include "Shape.h"
using namespace std;
class Shape
{
protected:
int m_id; // Add a data member for an id number of type int.
public:
int id;
int ID(); // return n_id data
Shape(); // initialises the id using a random number
Shape(const Shape &OtherShape); // copies the id member
Shape operator = (const Shape &OtherShape); // copies the id member
virtual void ToString() ; // returns the id as string e.g. “ID: 123”
};
#endif
//line.cpp
#include <iostream>
//#include <math.h>
#include "line.h"
#include "Point.h"
#include "Shape.h"
#include <string>
using namespace std;
Line::Line(){}
Line::Line(string x, Point P_start, Point P_end)
:discrib(x), start(P_start), end(P_end) {}
Line::Line( Line& line)
{
start = line.start;
end = line.end;
}
Line::~Line() {}
void Line::SetStart( Point SomePoint)
{
start = SomePoint;
}
void Line::SetEnd ( Point SomePoint)
{
end = SomePoint;
}
Point Line::GetStart()
{
return start;
}
Point Line::GetEnd()
{
return end;
}
Line Line::operator = ( Line& l)
{
if (this == &l)
{
return * this;
}
/*member function from base class */
m_id = l.m_id;
/* data from derived class*/
discrib = l.discrib;
start = l.start;
end = l.end;
return *this ;
}
void Line::ToString() // returns the line as string
{
cout << "[[" << discrib << ","
<< start << "," << end << "]]" << endl;
}
//Point.cpp
#include <iostream>
#include "Point.h"
using namespace std;
Point::Point() {}
Point::Point (double xNew, double yNew)
: Xcoord(xNew), Ycoord(yNew) {}
Point::~Point() {} //Destructor
Point::Point( Point& point) // Point copy constructor
{
Xcoord = point.Xcoord;
Ycoord = point.Ycoord;
}
void Point::X( double Value)
{//Assign X-coordinate the Value that is put into the main
Xcoord = Value;
}
void Point::Y( double Value)
{//Assing Y-coordinate the Value that is put into the main
Ycoord = Value;
}
double Point::X()
{
return Xcoord;//Return X-coordinate
}
double Point::Y()
{
return Ycoord;//Return Y-coordinate
}
void Point::ToString()
{
cout << "( " << X() << ","
<< Y() << " )";
}
Point Point::operator = ( Point& p)
{
if (this == &p)
{
return * this;
}
m_id = p.m_id;// member function from base class
Xcoord = p.Xcoord;// data from derived class
Ycoord = p.Ycoord;
return *this;
}
ostream& operator << (ostream& os , Point& p)
{
os << "[" << p.X() << "," << p.Y() << "]";
return os ;
}
//Shape.cpp
#include <iostream>
#include "Shape.h"
using namespace std;
Shape::Shape() // initialises the id using a random number
{
m_id = rand();
///cout << "id of shape is: " << m_id << endl;
}
int Shape::ID() // retrieve the id of the shape
{
return m_id;
}
void Shape::ToString() // returns the id as string
{
// cout << "ID: "<< ID();
}
Shape::Shape(const Shape &OtherShape)
{//copy constructor that copies the id member
id = OtherShape.m_id;
}
Shape Shape::operator = (const Shape& OtherShape)
{//assignment operator that copies the id member
if (this == &OtherShape)
{
return *this;
}
m_id = OtherShape.m_id;
return *this;
}
//main.cpp
//Variant
#include "Point.h"//header file declaration of class Point
#include "Line.h"//header file declaration of class Line
//#include "Circle.h"//header file declaration of class Circle
#include "Shape.h"//header file declaration of class Shape
#include <boost/variant.hpp>//includes boost class for variant
#include <iostream>// C++ style I/O using operator overloading
#include <ostream>
using namespace std;// The C++ logical collection of functions
//defines a shape type that can be of the form Point, Line or Circle
typedef boost::variant<Point, Line/*, Circle*/> ShapeType;
//function that returns a variant with the specified shape
ShapeType shape_type(){
int type;//selector variable
cout<<"Please enter 1 for shape of Point type"<<endl;
cout<<"Please enter 2 for shape of Line type"<<endl;
//cout<<"Please enter 3 for shape of Circle type"<<endl;
cin>>type;
//assigns the user given type to the variant
if(type==1){//creates a point and returns it
return Point();
}
else if(type==2){//creates a line and returns it
return Line();
}
/*else if(type==3){//creates a circle and returns it
return Circle();
}*/
}
//Visitor class
class VisitorClass: public boost::static_visitor<void>{
private:
double m_dx;//x delta to move
double m_dy;//y delta to move
public:
//==================================================================================================
//modifiers
//==================================================================================================
//function that Visits a point and moves its X,Y values
VisitorClass(double xval,double yval):m_dx(xval),m_dy(yval){
}
void operator()(Point& p) const{
//modifies the values of the point according to the user given inputs
p.X(p.X()+m_dx);
p.Y(p.Y()+m_dy);
}
//function that Visits a Line and moves its X,Y values of the starting and ending points
void operator()(Line& l) const{
//modifies the values of the starting point according to the user given inputs
//l.p1().X(l.p1().X()+m_dx);
//l.p1().Y(l.p1().Y()+m_dy);
l.GetStart().X(l.GetStart().X()+m_dx);
l.GetStart().Y(l.GetStart().Y()+m_dx);
//modifies the values of the ending point according to the user given inputs (scaled by 2)
//l.p2().X(l.p2().X()+m_dx*2);
//l.p2().Y(l.p2().Y()+m_dy*2);
l.GetEnd().X(l.GetEnd().X()+m_dx*2);
l.GetEnd().Y(l.GetEnd().Y()+m_dy*2);
}
//function that Visits a Circle and moves its X,Y values of its center
/*void operator()(Circle& c) const{
//modifies the values of the center point with the user given values
c.centerPoint().X((c.centerPoint().X()+m_dx));
c.centerPoint().Y((c.centerPoint().Y()+m_dy));
c.radius(m_dx+m_dy);
}*/
};
int main(){
//========================================================================================================================
// usage of variant data container
//========================================================================================================================
//Assigns a shape type via shape_type function
ShapeType figure=shape_type();
//prints the type of the figure given by the user
const std::type_info &ti=figure.type();//captures the type
cout<<"The created figure type is an object of the "<<ti.name()<<endl;//calls its names and prints it
cout<<figure;//the user assignation is sent to the cout using the overloaded << operator in each shape class
Line l("MyLine",Point(3,4),Point(6,8));//creates a Line
try{
boost::get<Line>(figure)=l;//assigns Line l to the figure variant
}
catch(boost::bad_get e){
std::cout << "***********************bad get error**************************" <<std::endl;
}
boost::apply_visitor(VisitorClass(9,12),figure);//shifts the figure according to a user given values
cout<<figure;//the user assignation is sent to the cout.
return 0;
}

