Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

c++ Array class template with template parameters

i have Created an Array class template with template parameters <element type, size > and
array class members, input, sort, and output functions.

but code does not work below what might i be doing wrong?

#include <iostream>
using namespace std;
template <class T, int n>
  class array {
    T mass[n];
    public:
      void input();
    void output();
    void sort();
  };

template <class T, int n>
  void array < T, n > ::input() {
    for (int i = 0; i < n; i++)
      cin >> mass[i];
  }

template <class T, int n>
  void array < T, n > ::output() {
    for (int i = 0; i < n; i++)
      cout << mass[i] << '\0';
  }

template <class T, int n>
  void array < T, n > ::sort()[T x; int p = 1, m = n;
    while (p) {
      p = 0;
      for (int i = 0; i < m - 1; i++)
        if (mas[i] > mas[i + 1])
          [x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
          }
      m--;
    }
  }

int main() {
  array < int, 10 > a;
  array < float, 5 > b;
  a.input();
  a.sort();
  a.output();
  b.input();
  b.sort();
  b.output();
  return 0;
}

i get the following compiler error what might i be doing wrong in this code ?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

   25 |   void array < T, n > ::sort()[T x; int p = 1, m = n;
      |                                  ^
/tmp/ZOMErK6tKN.cpp:25:33: error: expected ']' before 'x'
   25 |   void array < T, n > ::sort()[T x; int p = 1, m = n;
      |                                 ^~
      |                                 ]
/tmp/ZOMErK6tKN.cpp:25:52: error: 'n' was not declared in this scope
   25 |   void array < T, n > ::sort()[T x; int p = 1, m = n;
      |                                                    ^
/tmp/ZOMErK6tKN.cpp:26:5: error: expected unqualified-id before 'while'
   26 |     while (p) {
      |     ^~~~~
/tmp/ZOMErK6tKN.cpp:32:7: error: 'm' does not name a type
   32 |       m--;
      |       ^
/tmp/ZOMErK6tKN.cpp:33:5: error: expected declaration before '}' token
   33 |     }
      |     ^
/tmp/ZOMErK6tKN.cpp:34:3: error: expected declaration before '}' token
   34 |   }```

>Solution :

You have some typos in your code. In particular, you have use [ instead of { and mas instead of mass. These are correct and highlighted using comments in the below code:

template <class T, int n>
//----------------------------v------------------->[ changed to {
  void array < T, n > ::sort(){T x; int p = 1, m = n;
    while (p) {
      p = 0;
      for (int i = 0; i < m - 1; i++)
//----------vvvv------vvvv----------------------->mas changed to mass
        if (mass[i] > mass[i + 1])
//--------v-------------------------------------->[ changed to {
          {x = mass[i]; mass[i] = mass[i + 1]; mass[i + 1] = x; p = 1;
          }
      m--;
    }
  }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading