I want to sort array of int, strings or any data type. Therefore I decided to use comparable array as an input to my sort function.

But I get an error when I using it.

Comparable arr[];


the compiler says "comparable is undeclared identifier".

I think I must include specific thing so what's this thing?

Thanks,

Recommended Answers

All 7 Replies

1) Make sure you spelled it correctly
2) If its on another file, make sure you import it using #include "insertHeaderNameHere.h"
3) Else post your code

That's my code and I'm still get the same error

#include<iostream>
#include <stdlib.h>
#include <cstdlib>
using namespace std;
int main(){
   Comparable e[];
   return 0;
}

What exactly do you expect Comparable to be?

Maybe your thinking this is java or something. In C++ this is how you would sort any data type:

#include <vector>
#include <algorithm>
using namespace std;

int main(){
 std::vector<int> integerArray;
 integerArray.push_back(1);  
 integerArray.push_back(102);
 integerArray.push_back(-23);
 integerArray.push_back(1);

 std::sort( integerArray.begin(), integerArray.end() ); //sort the integer array 
}

you would do similar for any data type, although you might have to specify an explicit comparator

@firstperson
yeah you are right. I'm thinking that c++ like java and I can't do what you say because
I already implemented all sorting types method. and my problem now is how to sort any data type.

So how can I do that in c++?

First how are you storing the data types?

in c++ generic funtions catch automatically data type, but to get the right rezult you must comform to client contract, that means you have to provide correct input.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.