Random Access Iterator Error


 
Thread Tools Search this Thread
Top Forums Programming Random Access Iterator Error
# 1  
Old 02-08-2010
Random Access Iterator Error

I am unable to resolve the below code compilation issue:
Code:
class A
{
public:
int x;
};

void sort_A(TArray<A> &_Atype)
{
    std::stable_sort (_Atype.begin(), _Atype.end());
}

bool operator< (const A & _a1, const A & _a2)
{
 return _a1.x < _a2.x;
}

When we compile using gcc 4.1.2 on RH5.2 I am getting the below mentioned error:
Code:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h: In function âvoid std::__insertion_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]â not a class or a defined type:
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3066:   instantiated from âvoid std::__inplace_stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]â
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:3776:   instantiated from âvoid std::stable_sort(_RandomAccessIterator, _RandomAccessIterator) [with _RandomAccessIterator = A*]â
instantiated from âvoid sortA(TArray<A>&)
/usr/lib/gcc/x86_64-redhat-linux/4.1.2/../../../../include/c++/4.1.2/bits/stl_algo.h:2277: error: no match for âoperator<â in â__val < * __firstâ

I tried all the way using {typedef , SmartPointer} to define the type but it constantly throws the above mentioned error. What and how should I should declare and define so that the type (A*) is known by compiler when calling std::stable_sort.

Please help.

Last edited by Scott; 02-08-2010 at 10:56 AM.. Reason: Please use code tags
# 2  
Old 02-08-2010
Try putting the < operator inside your object instead of imposing it from outside. The function itself should be const, too, to reassure the compiler that comparing two objects won't modify their contents.

Code:
bool operator <(const A &other) const { return this.x < other.x; }


Last edited by Corona688; 02-08-2010 at 12:46 PM.. Reason: reread your code
# 3  
Old 02-08-2010
Thanks a lot. It solved my problem.
# 4  
Old 02-10-2010
We have now a different problem in regards to implementation based upon the model above.

When we move the operator < function within the class and another flow wants to call operator < function but with a different logic - how the same can be achieved.
# 5  
Old 02-10-2010
I guess, the operator overloading function "bool operator<", need to be declared as friend inside the class.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Beginners Questions & Answers

550 Access Error

Hi, we have a FTP server on vSphere on a windows 2008 server platform. One of our Unix machines now cannot send files to it, we checked permissions etc. Can login, when using put events.txt it gives a 550 Access Denied error. Nothing has changed from yesterday when it was working apart from... (1 Reply)
Discussion started by: RonT
1 Replies

2. Shell Programming and Scripting

Random script error with rndc

Hello coders, been running into an strange behavior into one of my script and i'm wondering if my code wouldn't be responsible. Bash on rhel 5.7 This is a basic check to see if bind is up and running on server. # rndc check INFO="Checking rndc" for DNS_SERVER in ${DNS_MASTER_SERVERS};... (10 Replies)
Discussion started by: maverick72
10 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. Programming

Need Help in Access Error

Hello Again, Please help with this link problem. Warning: mysql_connect() : Access denied for user 'myuser'@'localhost' (using password: XYZ@123) <?php function connectTo() { $host = "localhost"; $db_name = "myDB"; $username = "myUser"; $password =... (0 Replies)
Discussion started by: AimyThomas
0 Replies

5. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

6. HP-UX

Error in include_std/limit and include_std/rw/iterator

I get following compile time errors on HP ITANIUM machine "/opt/include_std/limit", line 268: error # 2321: data member initializer is not allowed _RWSTD_STATIC_CONST (bool, is_specialized = false); "/opt/include_std/rw/iterator", line 119: error # 2247: class template "std::iterator_traits"... (0 Replies)
Discussion started by: dodo_ind
0 Replies

7. Programming

STL Iterator and user-defined class

Is is possible to make STL-Iterator to work with user defined class ,like the one below? #include <iostream> #include <stdexcept> using namespace std; template <class T> class Array { public: T& operator (unsigned i) throw(out_of_range) { return data_; } protected: ... (2 Replies)
Discussion started by: johnbach
2 Replies

8. Programming

[C++] File I/O (Reading from a Random-Access File)

INFO: The program should enter a circle radius and Id for that circle to a file, then it should search for that id and print the radius for that circle. PROBLEM: This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream>... (0 Replies)
Discussion started by: VersEtreOuNe
0 Replies

9. UNIX for Dummies Questions & Answers

Iterator help needed

I have a question about iterators. Here is the code I have template <typename element> void merge_sort(vector<element> &v) { element::iterator lo; } Now thats all the code I have for the non-recursive merge sort at the moment, which obvioulsy isn't a lot, but I need... (0 Replies)
Discussion started by: Matrix_Prime
0 Replies

10. UNIX for Advanced & Expert Users

Data Access Error

Dear Reader, My Sun Machine comes to halt with a message 'Data Access Error'. What / Where could be wrong..?? Thanks in Advance.... (5 Replies)
Discussion started by: joseph_shibu
5 Replies
Login or Register to Ask a Question