C ++ Simple Boost Option

I am trying to create a list of objects using the boost option.

#include <string>
#include <list>
#include <iostream>
#include <boost/variant.hpp>

using namespace std;
using namespace boost;   

class CSquare;

class CRectangle {
public:
  CRectangle();
};

class CSquare {
public:
  CSquare();
};

int main()
{   typedef variant<CRectangle,CSquare, bool, int, string> object;

    list<object> List;

    List.push_back("Hello World!");
    List.push_back(7);
    List.push_back(true);
    List.push_back(new CSquare());
    List.push_back(new CRectangle ());

    cout << "List Size is: " << List.size() << endl;

    return 0;
}

Unfortunately, the following error occurs:

/tmp/ccxKh9lz.o: In function `main':
testing.C:(.text+0x170): undefined reference to `CSquare::CSquare()'
testing.C:(.text+0x203): undefined reference to `CRectangle::CRectangle()'
collect2: ld returned 1 exit status

I understand that everything will be fine if I use the form:

CSquare x;
CRectangle y;
List.push_back("Hello World!");
List.push_back(7);
List.push_back(true);
List.push_back(x);
List.push_back(y);

But I would like to avoid this form, if at all possible, since I would like to keep my objects unnamed. This requirement is important for my system - is there a way to avoid using named objects?

+3
source share
3 answers

Just need to change a few things, and it works:

#include <iostream>
#include <list>
#include <string>
#include <boost/variant.hpp>
using namespace std;
using namespace boost;   

class CRectangle
{
public:
 CRectangle() {}
};

class CSquare
{
public:
 CSquare() {}
};

int main()
{
 typedef variant<CRectangle, CSquare, bool, int, string> object;
 list<object> List;
 List.push_back(string("Hello World!"));
 List.push_back(7);
 List.push_back(true);
 List.push_back(CSquare());
 List.push_back(CRectangle());

 cout << "List Size is: " << List.size() << endl;

 return 0;
}

, CRectangle CSquare ( ) CSquare(), new CSquare() .. , "Hello World!" const char *, string("Hello World!") push_back bool ( , ).

+5

List.push_back ( CSquare());

List.push_back(CSquare());

defination

+1

CRectangle::CRectangle() CSquare::CSquare().

, :

CRectangle::CRectangle()
{ 
    // :::
}; 

... :

class CRectangle {
public:
  CRectangle()
  { 
    // :::
  }
}; 

... :

class CRectangle {
public:
}; 
0

Source: https://habr.com/ru/post/1785794/


All Articles