473,324 Members | 1,856 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

sort / vector

Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:

template<class T> class Compare
{
public:
Compare(){};
virtual ~Compare(){};

bool operator()(const T& T1, const T& T2) const
{return T1.GetNumber() < T2.GetNumber();}
};

class Element
{
public:
Element(Model* model, unsigned int number);
virtual ~Element();

unsigned int GetNumber() const { return _number;}
}

std::vector<Element*>* elements = new std::vector<Element*>(0);

elements->push_back(new Element()); // n-times
std::vector<Element*>::const_iterator iterFirst = elements->begin() ;
std::vector<Element*>::const_iterator iterLast = elements->end() ;

std::sort(iterFirst, iterLast, Compare<Element>());

My Compiler tells me(sorry my compiler speaks german, but it is
readable):
error C2664: '()' : Konvertierung des Parameters 1 von 'class
fesolv::Element *' in 'const class fesolv::Element &' nicht moeglich
Ursache: Konvertierung von 'class fesolv::Element *' in 'const
class fesolv::Element' nicht moeglich

I don't know where to start.
Do sort also works for vector<T*>* or only for vector<T*> ????

Thanks,
eiji

Nov 27 '05 #1
10 4031

eiji wrote:
Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:

template<class T> class Compare
{
bool operator()(const T& T1, const T& T2) const std::vector<Element*>* elements = new std::vector<Element*>(0); std::sort(iterFirst, iterLast, Compare<Element>());


Notice that your compare function expects references but your vector
contains pointers. The "T" for your compare would want to be Element*
and then of course the body of that operator would need
changing....therefore a seperate ComparePtr functor may be in order.

Keeping pointers in a vector is *generally* a bad move. There are of
course always exceptions to any rule but when you are going to be
keeping pointers in a vector try to find a different way. One way to
do so without all the pita that goes with it is to create a smart
pointer that can be contained in a vector (there are certain rules
about vector contents). I'm not even sure pointers meet those reqs.

Nov 27 '05 #2
On 27 Nov 2005 12:39:10 -0800, "eiji" <Sa*****@gmx.de> wrote:
Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:

template<class T> class Compare
{
public:
Compare(){};
virtual ~Compare(){};
The destructor doesn't have to be virtual if you do not inherit any
other types from Compare<T>.
bool operator()(const T& T1, const T& T2) const
{return T1.GetNumber() < T2.GetNumber();}
};
This won't work for pointers; either you'll need to change this to
accommodate T*, or you will need a second type to deal with them,
e.g.:

bool operator()(const T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber();}
class Element
{
public:
Element(Model* model, unsigned int number);
virtual ~Element();

unsigned int GetNumber() const { return _number;}
}
Some questions/remarks:

(1) Where is _number declared?
(2) You should never use names beginning with an underscore for
anything (see section 17.4.3.1.2 of the C++ for the reason).
(3) If you never want to construct an Element with no arguments, you
should declare a default constructor, but make it private and do not
implement it. This will prevent the compiler from giving you a default
constructor that you don't want.
std::vector<Element*>* elements = new std::vector<Element*>(0);
The default constructor will work fine, no need to pass an argument of
zero:

std::vector<Element*>* elements = new std::vector<Element*>;
elements->push_back(new Element()); // n-times
Hmmm ... I see we have a problem here if you want to hide the default
constructor ... but if you provide initializers/arguments, it's OK.
std::vector<Element*>::const_iterator iterFirst = elements->begin() ;
std::vector<Element*>::const_iterator iterLast = elements->end() ;

std::sort(iterFirst, iterLast, Compare<Element>());
This should work if you (a) pass an object and (b) change the
implementation and signature of Compare<>::operator() to accept T*
instead of T&:

Compare<Element> cmp_obj;
std::sort(iterFirst, iterLast, cmp_obj);

It might also work with Compare<Element>(), but you are creating a
temporary. I don't know why, but I always seem to have trouble when
passing a temporary object to std::sort()...
My Compiler tells me(sorry my compiler speaks german, but it is
readable):
error C2664: '()' : Konvertierung des Parameters 1 von 'class
fesolv::Element *' in 'const class fesolv::Element &' nicht moeglich
Ursache: Konvertierung von 'class fesolv::Element *' in 'const
class fesolv::Element' nicht moeglich

I don't know where to start.
Do sort also works for vector<T*>* or only for vector<T*> ????

Thanks,
eiji


FYI -- übrigens gibt es auch eine deutsche NG für C++-Fragen,
de.comp.lang.iso-c++ ... aber hier bist Du mit Deinen Fragen natürlich
genauso willkommen, wie drüben! :)

--
Bob Hairgrove
No**********@Home.com
Nov 27 '05 #3
>there are certain rules about vector contents

Nice to know? Any idea where to find them easily?

Nov 27 '05 #4
eiji wrote:
Hi folks,
I have a problem compiling this under VC6!
Maybe someone has some minutes to look at this:

template<class T> class Compare
{
public:
Compare(){};
virtual ~Compare(){};

bool operator()(const T& T1, const T& T2) const
{return T1.GetNumber() < T2.GetNumber();}
};

class Element
{
public:
Element(Model* model, unsigned int number);
virtual ~Element();

unsigned int GetNumber() const { return _number;}
}

std::vector<Element*>* elements = new std::vector<Element*>(0);

elements->push_back(new Element()); // n-times
std::vector<Element*>::const_iterator iterFirst = elements->begin() ;
std::vector<Element*>::const_iterator iterLast = elements->end() ;

std::sort(iterFirst, iterLast, Compare<Element>());

My Compiler tells me(sorry my compiler speaks german, but it is
readable):
error C2664: '()' : Konvertierung des Parameters 1 von 'class
fesolv::Element *' in 'const class fesolv::Element &' nicht moeglich
Ursache: Konvertierung von 'class fesolv::Element *' in 'const
class fesolv::Element' nicht moeglich

I don't know where to start.
Do sort also works for vector<T*>* or only for vector<T*> ????


Since your vector has T* elements, your comparison function needs to take T*
as arguments.
Best

Kai-Uwe Bux

Nov 27 '05 #5

"eiji" <Sa*****@gmx.de> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
there are certain rules about vector contents


Nice to know? Any idea where to find them easily?


Objects to be stored in standard library containers
(e.g. vectors) must be copyable and assignable.

-Mike
Nov 27 '05 #6
On 27 Nov 2005 13:05:10 -0800, ro**********@gmail.com wrote:
Keeping pointers in a vector is *generally* a bad move.
Why? People do it all the time. As long as the lifetime of the objects
pointed to is well-managed, there shouldn't be a problem, and it is
the only way to have a container of polymorphic types.
There are of
course always exceptions to any rule but when you are going to be
keeping pointers in a vector try to find a different way. One way to
do so without all the pita that goes with it is to create a smart
pointer that can be contained in a vector (there are certain rules
about vector contents). I'm not even sure pointers meet those reqs.


No need to create a smart pointer that works in containers, it's
already been done by the Boost folks (look for boost::shared_ptr<>):

http://www.boost.org

Beware of std::auto_ptr, however -- that one doesn't work in STL
containers.

--
Bob Hairgrove
No**********@Home.com
Nov 27 '05 #7
Thanks a lot.

bool operator()(const T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber();}
and
std::vector<Element*>::iterator iterFirst = elements->begin() ;
std::vector<Element*>::iterator iterLast = elements->end() ;
(no const iterators)
and
Compare<Element> cmp_obj;

works fine!

I didn't put in all code, because the classes are quit big!
(2) You should never use names beginning with an underscore for
anything (see section 17.4.3.1.2 of the C++ for the reason).

Nice to know!

Nov 27 '05 #8
On 27 Nov 2005 13:22:30 -0800, "eiji" <Sa*****@gmx.de> wrote:
Thanks a lot.

bool operator()(const T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber();}
and
std::vector<Element*>::iterator iterFirst = elements->begin() ;
std::vector<Element*>::iterator iterLast = elements->end() ;
(no const iterators)
It should also work with const_iterator, I believe, although Scott
Meyers (among others) recommends to "prefer iterator over
const_iterator" (from an old "C++ Users Journal" article).
and
Compare<Element> cmp_obj;

works fine!

I didn't put in all code, because the classes are quit big!
(2) You should never use names beginning with an underscore for
anything (see section 17.4.3.1.2 of the C++ for the reason).

Nice to know!


--
Bob Hairgrove
No**********@Home.com
Nov 27 '05 #9

Bob Hairgrove wrote:
On 27 Nov 2005 13:05:10 -0800, ro**********@gmail.com wrote:
Keeping pointers in a vector is *generally* a bad move.
Why? People do it all the time. As long as the lifetime of the objects
pointed to is well-managed, there shouldn't be a problem,


That IS the problem.

and it is the only way to have a container of polymorphic types.


Better to wrap in a smart pointer. There are exceptions when you don't
need/want a smart pointer but imo most of the time you should.
Overhead is minimal compared to the time it can save in ownership
issues.

Nov 27 '05 #10
On 27 Nov 2005 13:22:30 -0800, "eiji" <Sa*****@gmx.de> wrote:
Thanks a lot.

bool operator()(const T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber();}
and
std::vector<Element*>::iterator iterFirst = elements->begin() ;
std::vector<Element*>::iterator iterLast = elements->end() ;
(no const iterators)
and
Compare<Element> cmp_obj;

works fine!

I didn't put in all code, because the classes are quit big!
(2) You should never use names beginning with an underscore for
anything (see section 17.4.3.1.2 of the C++ for the reason).

Nice to know!


Duh ... I just realized why you cannot use a const_iterator:
std::sort() needs to reassign the vector elements in order to sort
them!

(Guess I shouldn't post so late at night!)

--
Bob Hairgrove
No**********@Home.com
Nov 27 '05 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Mike | last post by:
Hi there, I'm very new to the STL and am struggling a bit with a sort, and would love some help if possible! I have a simple array V of objects of class OBJ that I want to rank in order of...
8
by: b83503104 | last post by:
In matlab, the sort function returns two things: =sort() a = 5 7 8 b = 1 3 2 where a is the sorted result, and b is the corresponding index. Is there C++ code available to...
2
by: bill | last post by:
Hi, I just spent a loooooooong day to debug a problem, it ended up that sort() does not work properly on my vector!!! In the program I have a vector and the program dynamically add some new...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
12
by: Eva | last post by:
Hi, I try to implement quick sort. I sort vectors by their first value. 10 2 3 4 9 3 5 6 10 4 5 6 must be 9 3 5 6 10 2 3 4 10 4 5 6 The prog works great on maybe 500 vectors, but I have an...
5
by: Dr. Ann Huxtable | last post by:
Hello All, I am reading a CSV (comma seperated value) file into a 2D array. I want to be able to sort multiple columns (ala Excel), so I know for starters, I cant be using the array, I need...
4
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
8
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
5
by: Sarahger9 | last post by:
I am trying to generate a random vector of integers and sort them using merge sort. I am having trouble with the code. It is long, but I would appreciate if someone could take a look at it and see...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.