Click to See Complete Forum and Search --> : Sorting a vector of objects
cax
December 2nd, 2005, 06:15 AM
Ok this shouldn't be nearly this hard, but i've checked EVERYWHERE from msdn to scouring google & I can't figure it out.
I basically have a vector of pointers to "aaWord" objects, each of which contain titles (as strings) and I want to order the vector alphabetically by those strings. I'm using __gc and pointers and all that. Visual C++ .NET
vector<gcroot<aaWord __gc*> > *VectorofWords;
Since obviously ordinary sort wouldn't work, I tried making my own predicate function looking like
static bool op_LessThan( aaWord* a, aaWord* b ){...}
but it throws a Fatal Error C1001 INTERNAL COMPILER ERROR and there's no way i'm getting through that.
Am I just making a stupid syntax error?:(
NoHero
December 2nd, 2005, 06:58 AM
Are you using the new Managed C++ syntax? If so the asterisk indiciator for managed types got replaced by the ^ (xor operator):
vector<gcroot<aaWord^> > VectorofWords^ = gcnew vector<gcroot<aaWord^> >();
The code above is written from scratch, so it might contain some errors too.
cilu
December 2nd, 2005, 08:37 AM
Try this:
#include <vcclr.h>
#include <algorithm>
#include <vector>
#include <iostream>
using namespace System;
bool mycompare(gcroot<String*> s1, gcroot<String*> s2)
{
int res = String::Compare(s1, s2);
return res <= 0;
}
int main()
{
std::vector< gcroot<String*> > *list = new std::vector< gcroot<String*> >();
list->push_back(S"one");
list->push_back(S"two");
list->push_back(S"three");
list->push_back(S"four");
list->push_back(S"five");
std::sort(list->begin(), list->end(), mycompare);
for(std::vector< gcroot<String*> >::const_iterator it = list->begin(); it != list->end(); ++it)
{
Console::WriteLine(*it);
}
return 0;
}
cax
December 2nd, 2005, 04:01 PM
Try this:
bool mycompare(gcroot<String*> s1, gcroot<String*> s2)
{
int res = String::Compare(s1, s2);
return res <= 0;
}
...
std::sort(list->begin(), list->end(), mycompare);
I tried that and it still gave the same error. I think the problem is elsewhere, seeing as it doesn't seem to matter how the operator is implemented.
My code is changed a bit, since the vector is of "aaWord" objects & i have to extract the strings from that
bool mycompare(aaWord *x, aaWord *y){
gcroot<String*> s1 = x->gettitle();
gcroot<String*> s2 = y->gettitle();
int res = String::Compare(s1, s2);
return res <= 0;
}
...
std::sort(Collection->getwordvector()->begin(), Collection->getwordvector()->end(), mycompare);I get the feeling i'm making a really basic mistake.
Are you using the new Managed C++ syntax?no, I don't believe so
cilu
December 2nd, 2005, 06:08 PM
I tried that and it still gave the same error. I think the problem is elsewhere, seeing as it doesn't seem to matter how the operator is implemented.
Well, I tested that piece of code before posting, so I'm sure it works ok.
Now, what is aaWord?
If the error your talking about is still "C1001 INTERNAL COMPILER ERROR", I don't see how that has anything to do with this piece of code. It must be triggered by something else...
cax
December 2nd, 2005, 06:28 PM
Well, I tested that piece of code before posting, so I'm sure it works ok.
Now, what is aaWord?
If the error your talking about is still "C1001 INTERNAL COMPILER ERROR", I don't see how that has anything to do with this piece of code. It must be triggered by something else...aaWord is a gc'ed class with constructors, virtual functions etc. It's kept in header/cpp files elsewhere.
__gc class aaWord{
public:
....//functions here
protected:
String * title;
};
I'm also working with Windows Forms so iostream isn't that relevant, but I got the gist of your code.
I'm trying to sort the vector of (pointers to) aaWords by that title string. Could it have anything to do with dereferencing?
cilu
December 3rd, 2005, 04:43 AM
Your comparison predicate is wrong. Here it a tested piece of code. I think this is what you need:
__gc class aaWord
{
public:
aaWord(String* title_): title(title_){}
String* get_Title() {return title;}
void set_Title(String* value) {title = value;}
protected:
String * title;
};
bool mycompare(gcroot<aaWord*> s1, gcroot<aaWord*> s2)
{
int res = String::Compare(s1->get_Title(), s2->get_Title());
return res <= 0;
}
int main()
{
std::vector< gcroot<aaWord*> > *list = new std::vector< gcroot<aaWord*> >();
list->push_back(new aaWord(S"one"));
list->push_back(new aaWord(S"two"));
list->push_back(new aaWord(S"three"));
list->push_back(new aaWord(S"four"));
list->push_back(new aaWord(S"five"));
std::sort(list->begin(), list->end(), mycompare);
for(std::vector< gcroot<aaWord*> >::const_iterator it = list->begin(); it != list->end(); ++it)
{
Console::WriteLine((*it)->get_Title());
}
return 0;
}
cax
December 3rd, 2005, 06:59 PM
Your comparison predicate is wrong. Here it a tested piece of code. I think this is what you need:I made a new project with that exact code & it didn't compile.
http://rapidshare.de/files/8572755/testrr.zip.html
The only changes I made were to rearrange the code in a Windows Forms layout & to disable precompiled headers(tried it both ways).
This is frustrating. I really appreciate your help though.
Edit: Project added as attachment.
cilu
December 4th, 2005, 05:23 AM
cax, your project compiles and runs with no problem. I am using VS 2003.
BTW, when you zip projects, make sure you REMOVE the Debug and Release directories (which are regenerated when building) as well as the .NCB file (which too are regenerated). That way, from a 2.7MB zip (as your testrr is) you got a 10KB zip you can yeasily attack to your post, without the need of using a file sharing server. If I was using a dial-up connection, I would have never downloaded that zip.
cax
December 4th, 2005, 05:33 PM
cax, your project compiles and runs with no problem. I am using VS 2003.As am I...
So.. I have to get a new compiler? Any ideas on what the problem could be?
http://img204.imageshack.us/img204/8450/ice5wj.jpg
BTW, when you zip projects, make sure you REMOVE the Debug and Release directories (which are regenerated when building) as well as the .NCB file (which too are regenerated). That way, from a 2.7MB zip (as your testrr is) you got a 10KB zip you can yeasily attack to your post, without the need of using a file sharing server. If I was using a dial-up connection, I would have never downloaded that zip.acknowledged
cilu
December 5th, 2005, 12:49 PM
As I told you, internal compiler error doesn't have to do with this piece of code you have. I suggest you to delete the .ncb file and the Debug\Release folders and go for a Rebuild.
codeguru.com
Copyright WebMediaBrands Inc., All Rights Reserved.