1

I am creating a sorter superclass with subclasses for the specific sorts. I am planning to use a vector called data for all sorts but am wondering why this is giving a syntax error(see the code below)

I have declared a protected member data so it can be accessible by all the subclasses but the compiler does not like it.

template<typename T>
class Sorter{
protected:
   vector<T> data;
public:
    Sorter();
    Sorter(T& x):data(x){}
    void setData(const std::vector<T>& x) {
        data = x;
    }
    virtual void sort() = 0;



};

template <typename T>
//bubble sort
class MysterySorterA: public Sorter<T>{
public:

    virtual void sort(){


        //auto& t1 = this->data[0];
        //auto& t2 = this->data[1];

            for (unsigned long i = 0; i < data.size()-1; i++)

            // Last i elements are already in place
            for (unsigned long j = 0; j < data.size() - i - 1; j++)
                if (data[j] > data[j+1])
                    swap(&data[j],&data[j+1]);
        /*if(t1 < t2){
            cout << "Comparing is OK" << endl;
        }*/
        cout << "Mystery Sorter A" << endl;
    }


    void swap(int *x, int *y){
        int temp = *x;
        *x = *y;
        *y = temp;
    }

};

I am expecting to see no error messages but I am getting a "use of undeclared identifier" EDIT: "Data" is the undeclared identifier

exact message: "use of undeclared identifier 'data'"

P. Tarala
  • 55
  • 5
  • And the *exact* error is? – robthebloke Oct 18 '19 at 00:28
  • If the undeclared identifier is `swap()`, put its implementation before the implementation of your `sort ()` function. Can you clarify/confirm which identifier is undeclared? –  Oct 18 '19 at 00:29
  • "data" is the undeclared identifier. sorry I should have specified that – P. Tarala Oct 18 '19 at 00:32
  • 1
    @P.Tarala Thanks. That is weird.It's confusing to me too. Could you [edit your question](https://stackoverflow.com/posts/58442349/edit) and copy and paste the full error in verbatim? –  Oct 18 '19 at 00:36
  • /home/student/Desktop/CSE2341-F19-Pravik-Tarala/Sprint3/Project3/sorter.h:34: error: use of undeclared identifier 'data' here is the exact message. I don't even know what to think. – P. Tarala Oct 18 '19 at 00:40
  • 2
    Possible duplicate of [accessing protected members of superclass in C++ with templates](https://stackoverflow.com/questions/4010281/accessing-protected-members-of-superclass-in-c-with-templates) – Amadeus Oct 18 '19 at 00:41
  • @P.Tarala In order to compile, present the name of the protected member in the inherented class. i.e. `using Sorter::data;` See the duplicate for a better explanation – Amadeus Oct 18 '19 at 00:42
  • @P.Tarala Ah, see the linked possible duplicate. It's because you're working with templates. –  Oct 18 '19 at 00:48
  • Thank you all I figured out the issue was in another question! – P. Tarala Oct 18 '19 at 00:49
  • The whole idea does not make sense: (1) STL already contains sort method that are adequate 99% of the time. (2) It make the code harder to understand for people that know STL and existing sort methods. (3) STL was designed to avoid virtual calls. (4) You don't always need a new container or `std::vector` is not always the best choice. (5) In your code, you initialize a vector from a single item (`Sorter` constructor). (6) If you want virtual `Sort` method, then data should be in derived class too (so for example, you could use a `std::set` for example. (7) No possibility for custom comparison – Phil1970 Oct 18 '19 at 00:51
  • Possible duplicate of [Why do I have to access template base class members through the this pointer?](https://stackoverflow.com/questions/4643074/why-do-i-have-to-access-template-base-class-members-through-the-this-pointer) – Davis Herring Oct 18 '19 at 01:16

0 Answers0