Given: the number N and the sequence a1, a2, ... aN

Create a class template that generates dynamic one-dimensional arrays with elements of various types (real, integer, character, etc.). The data type and result are parameters in relation to the class, the program must have initialization methods, a constructor, a destructor, a method for viewing the values ​​of the created array, according to the specified algorithm.

(a1*a1), (a1*a2), …, (a1*aN); 

Code

 #include "stdafx.h" #include "iostream" using namespace std; template <typename T> class Mas { private: TN; T* mas; public: void set(TN); void print(); }; template <typename T> Mas<T>::Mas(TN) { mas = new T [N]; for(int i(0);i<=N;i++) { cin>>mas[i]; } } template <typename T> Mas<T>::~Mas() { delete [] mas; } template <typename T> Mas<T>::Mas() {} template <typename T> void Mas<T>::set(TN) { mas = new T [N]; for(int i(0);i<=N;i++) { cin>>mas[i]; } }; template <typename T> void Mas<T>::print() { for(int i(0);i<=N;i++) { cout<<mas[i] = mas[1]*mas[i]; } } int _tmain(int argc, _TCHAR* argv[]) { Mas<int> a; a.set(3); a.print(); return 0; } 

It produces errors:

  Ошибка 1 error C2039: {ctor}: не является членом 

"Mas <T>" c: \ users \ user \ desktop \ dz 6 \ dz 6 \ dz 6.cpp 33 1 DZ 6

 Ошибка 2 error C2039: {dtor}: не является членом 

"Mas <T>" c: \ users \ user \ desktop \ dz 6 \ dz 6 \ dz 6.cpp 40 1 DZ 6

 Ошибка 3 error C2039: {ctor}: не является членом 

"Mas <T>" c: \ users \ user \ desktop \ dz 6 \ dz 6 \ dz 6.cpp 44 1 DZ 6

How to get rid of errors, and did I solve the problem correctly?

  • @MeWn, If you are given an exhaustive answer, mark it as correct (click on the check mark next to the selected answer). - Vitalina

1 answer 1

Corrected a pack of errors, read the comments in the code. It is now compiled, like doing something. Right or not, I don’t know, I’m not your teacher.

 #include "iostream" using namespace std; template <typename T> class Mas { private: TN; T* mas; public: void set(TN); void print(); Mas(TN); // нужно объявлять конструкторы и деструкторы Mas(); ~Mas(); }; template <typename T> Mas<T>::Mas(TN) { mas = new T [N]; for(int i = 0; i < N; i++) // в оригинальной записи есть выход за пределы { cin>>mas[i]; } } template <typename T> Mas<T>::~Mas() { delete [] mas; } template <typename T> Mas<T>::Mas() {} template <typename T> void Mas<T>::set(TN) { // эта функция может быть причиной утечек. Представьте, что ее вызвали дважды в коде mas = new T [N]; for(int i = 0; i < N; i++) { cin>>mas[i]; } }; template <typename T> void Mas<T>::print() { for(int i = 0; i < N; i++) { //cout<<mas[i] = mas[1]*mas[i]; тут бред написан. // если это печать, то почему она пытается изменять элементы массива? // может просто вывести элементы? cout << mas[i] << " "; } } int main(int argc, char* argv[]) { Mas<int> a; a.set(3); a.print(); return 0; }