A
OOP

An instance of its class as a C++ field?

Good day,
I have been programming in C# for a long time, and I decided to try the pros. And they broke my head with such a seemingly simple concept as a class field. In C#, field = another class = another structure = base structure(byte,int,char...). But in C++ when writing

class MyClass1
{
   public:
   MyClass2 mc2;
   ..{пустой конструктор}
};
class MyClass2
{
   public:
   int A,B,C;
   ..{пустой конструктор}
};

The compiler swears at a syntax error that it is necessary to put ";" before class type. But when using standard types or itself, everything is fine. Maybe it's impossible to store classes in one file?
sorry, I forgot to write here about the semicolon, it is written in the project.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2016-04-24
@Ruins

Просто описание класса заканчивайте точкой с запятой. Сразу после }. Ну и ещё ссылки вперёд запрещены. Либо описывайте MyClass2 перед MyClass1, либо просто объявите его:

class MyClass2;
class MyClass1
{
   public:
   MyClass2 mc2;
   ..{пустой конструктор}
};

class MyClass2
{
   public:
   int A,B,C;
   ..{пустой конструктор}
};

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question