Answer the question
In order to leave comments, you need to log in
How to put a "non-trivial" object in a structure?
Good time of the day!
I don’t know how to formulate my question correctly, since I encountered this for the first time, searched a bunch of forums, but I didn’t really understand what the point was.
I have the following project structure. (In a truncated format and without implementing methods, I will insert only existing constructors)
class Point : public PointBase
{
public:
Point(double X, double Y, double Z)
{
}
private:
double X;
double Y;
double Z;
};
class Edge : public EdgeBase
{
public:
Edge(Point* firstPoint, Point* secondPoint)
{
this->firstPoint = firstPoint;
this->secondPoint = secondPoint;
}
private:
Point* firstPoint;
Point* secondPoint;
};
template<typename type_t>
struct item
{
item* next;
type_t data;
};
template <typename type_t>
class Array : public ArrayBase
{
public:
Array();
friend class IArray<typename type_t>;
private:
item<type_t> *head;
item<type_t> *tail;
};
template <class type_t>class Array;
template <class type_t>
class IArray
{
public:
IArray() {};
IArray(Array<type_t>&);
private:
Array<type_t>* arr;
item<type_t>* currentItem;
};
class Model : public ModelBase
{
public:
Model();
Model(const Model& model)
{
};
Model& operator=(const Model& model)
{
}
private:
IArray<Point> points;
IArray<Edge> edges;
Model(Model&&);
};
item<type_t>* newItem = new item<type_t>;
template<typename type_t>
struct item
{
item* next;
type_t data;
item() {};
item(const item<type_t>&) {};
item(item<type_t>&&) {};
};
Answer the question
In order to leave comments, you need to log in
One of the fields (in this case data
, the implementation item<Edge>
) does not have a default constructor. There are three ways.
ONCE. Figure out how to make the constructor still exist.
class Edge : public EdgeBase
{
public:
Edge (Point* firstPoint, Point* secondPoint) { init(firstPoint, secondPoint); }
Edge () { init(NULL, NULL); }
void init (Point* firstPoint, Point* secondPoint)
{
this->firstPoint = firstPoint;
this->secondPoint = secondPoint;
}
private:
Point* firstPoint;
Point* secondPoint;
};
#include <iostream>
class ImmutableInt
{
public:
explicit ImmutableInt(int x) : fData(x) {}
int data() const { return fData; }
private:
int fData;
};
template <class Payload>
class ListItem
{
public:
Payload payload;
ListItem* next;
ListItem() : next(NULL) {}
ListItem(const Payload& x) : payload(x), next(NULL) {}
};
// Так писать нельзя — эта конструкция расшаблонивает все функции,
// а конструктора по умолчанию как не было, так и нет!
//template class ListItem<ImmutableInt>;
int main()
{
ListItem<ImmutableInt> li(ImmutableInt(42));
std::cout << li.payload.data() << std::endl;
return 0;
}
#include <iostream>
class ImmutableInt
{
public:
explicit ImmutableInt(int x) : fData(x) {}
int data() const { return fData; }
private:
int fData;
};
template <class Payload>
class ListItem
{
public:
Payload payload;
ListItem* next = nullptr;
template<class... Args>ListItem(Args... args) : payload(args...) {}
};
int main()
{
ListItem<ImmutableInt> li(42);
std::cout << li.payload.data() << std::endl;
return 0;
}
explicit
not necessary, I nevertheless noted it - just to show that in the second case we pass ImmutableInt<42> as a parameter, and in the third - 42. template<typename type_t>
struct item
{
item* next;
type_t data;
};
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question