You need to create an instance of the class using the specified size of dynamic memory. I tried to register Stroka *stroka = new Stroka[10]; , but the message "expression must have class type" appears.

Error C2228 The expression to the left of ".vvod" must represent a class, structure, or union.

Also as I understand it, you need to implement a destructor.

 ~Stroka() // деструктор { delete[] str; }; 

Program code below:

 #include "pch.h" #include <iostream> #include <iomanip> #include <stdlib.h> #include <conio.h> #include <stdio.h> #include <windows.h> #include <string.h> #include <malloc.h> using namespace std; class Stroka { char str[80]; public: Stroka(char*); //конструктор с параметром, ссылка на объект не фиксированного размера Stroka(const char*); //конструктор с параметром, ссылка на объект фиксированного размера Stroka() {}; //конструктор без параметров Stroka(const Stroka&); // Конструктор копирования void vvod(); void vyvod(); } ; Stroka::Stroka(char *string) { strcpy(str, string); } Stroka::Stroka(const Stroka& s) { strcpy(str, s.str); } void Stroka::vvod() { cout << "Введите текст:" << endl; cin >> str; } void Stroka::vyvod() { cout << "Вывод текста на экран:" << endl; cout << str << endl; } int main(int) { setlocale(0, ""); // установить русскую локацию Stroka *s1 = new Stroka[80]; //Stroka s1; s1.vvod(); s1.vyvod(); } ; 

3 answers 3

s1 declared as a pointer, which is why you need to use an arrow rather than a point to access the object's field:

 s1->vvod(); s1->vyvod(); 

With the dot, you are trying to call methods at the pointer itself, which in principle does not possess them (the object behind the pointer possesses them).

The compiler therefore requires a “class, structure, or union” that only in them can fields and methods be declared. The pointer itself is too primitive for this. And I will immediately answer the potential question: “why it is impossible to use a dot everywhere, if the pointer basically does not have its own fields”: there are still smart pointers that are full-fledged classes and have their own methods besides the indicated object.

    Select the dynamic memory:

     void * mem = operator new (sizeof(Stroka)); 

    For convenience, we make a link to the desired type:

     Stroka & str = (* ((Stroka *) mem )); 

    Call the object constructor at this place:

     new(mem) Stroka(); 

    Then we DO NOT forget to call the destructor:

     str.~Stroka(); 

    And do not forget to free the memory:

     operator delete ( mem ) ; 

    Your destructor is NOT needed, since the str [80] array is deleted along with the object.

     ~Stroka() // деструктор { // delete[] str; не надо }; 
    • one
      Why use operator new and operator delete directly, and manually call the constructor and destructor, instead of just using new and delete ? In addition, the constructor is not called this way, you need to use placement-new. - HolyBlackCat
    • @HolyBlackCat I forget the syntax - obscurantism this beautiful new. I try to guess the condition of the problem and figured it out. Probably need to clarify what you need? - AlexGlebe

    @HolyBlackCat I forget the syntax - obscurantism this beautiful new. I try to guess the condition of the problem and figured it out. Probably need to clarify what you need?

    You need to make a variant of the constructor that creates an instance of the class, using the specified size of dynamic memory. I just learn the language, I suggested that in the simplest version it is somehow done through new (for me it is just a familiar syntax, although I read that in C ++ it is a bad form).

    I can not understand how to use your advice. Could you put it inside my example, it would be so much clearer.

    I did it like this:

     int main(int) { setlocale(0, ""); // установить русскую локацию Stroka *s1; s1 = new Stroka; //Stroka s1; s1->vvod(); s1->vyvod(); delete s1; }