473,770 Members | 6,133 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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()(cons t 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<Ele ment*>* elements = new std::vector<Ele ment*>(0);

elements->push_back(ne w Element()); // n-times
std::vector<Ele ment*>::const_i terator iterFirst = elements->begin() ;
std::vector<Ele ment*>::const_i terator iterLast = elements->end() ;

std::sort(iterF irst, 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 4088

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()(cons t T& T1, const T& T2) const std::vector<Ele ment*>* elements = new std::vector<Ele ment*>(0); std::sort(iterF irst, 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....the refore 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<cla ss 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()(cons t 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()(cons t 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<El ement*>* elements = new std::vector<Ele ment*>(0);
The default constructor will work fine, no need to pass an argument of
zero:

std::vector<Ele ment*>* elements = new std::vector<Ele ment*>;
elements->push_back(ne w 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<El ement*>::const_ iterator iterFirst = elements->begin() ;
std::vector<El ement*>::const_ iterator iterLast = elements->end() ;

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

Compare<Element > cmp_obj;
std::sort(iterF irst, 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::Elemen t *' 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.is o-c++ ... aber hier bist Du mit Deinen Fragen natürlich
genauso willkommen, wie drüben! :)

--
Bob Hairgrove
No**********@Ho me.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()(cons t 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<Ele ment*>* elements = new std::vector<Ele ment*>(0);

elements->push_back(ne w Element()); // n-times
std::vector<Ele ment*>::const_i terator iterFirst = elements->begin() ;
std::vector<Ele ment*>::const_i terator iterLast = elements->end() ;

std::sort(iterF irst, 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.goo glegroups.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**********@gm ail.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_p tr<>):

http://www.boost.org

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

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

bool operator()(cons t T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber(); }
and
std::vector<Ele ment*>::iterato r iterFirst = elements->begin() ;
std::vector<Ele ment*>::iterato r 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()(cons t T* T1, const T* T2) const
{return T1->GetNumber() < T2->GetNumber(); }
and
std::vector<El ement*>::iterat or iterFirst = elements->begin() ;
std::vector<El ement*>::iterat or 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<Elemen t> 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**********@Ho me.com
Nov 27 '05 #9

Bob Hairgrove wrote:
On 27 Nov 2005 13:05:10 -0800, ro**********@gm ail.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

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

Similar topics

6
2611
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 the float OBJ.t and put the ranks into a list of ints, call it RANK, so I can then access the OBJ with the i-th smallest value of t by V].
8
7599
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 achieve this? Better compatible with STL vector.
2
1686
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 elements to the vector, but after each time an element is added, I call sort() against this vector, and start over again. Then there are some strange probelms, in debugging them, I found that sort() falls into some wild state, its pointers of vector...
7
3548
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 there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
12
2926
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 "Aborted(core
5
8984
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 something more sophistictated like a vector of row objects. The data is of this format (same number of columns for each row): A, x, y, z, ...
4
1916
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
4195
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
3909
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 no path included (no C:\...) eg: my_file2.jpg, my_file1.bmp, etc... and m_path stores the path, eg: C:\folder1 I want to sort this vector according to different criterion, such as
5
4494
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 if the can help me, I've been working on it for days and am completely stuck. When I run the program, it just stops at the point where I call the mergesort. I believe the problem may be in the merge split, because when I cout the values for low,...
0
9592
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9425
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10059
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10005
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9871
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8887
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7416
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5452
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3972
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.