Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 05-25-2005, 02:38 AM   #1 (permalink)
tis99
Registered User
 
Join Date: May 2005
Posts: 1
tis99 is on a distinguished road
vector of own objects

Hello,

Im trying to create a vector of objects of a class that I defined:

std::vector <element*> elementList;

within the element class I have a member function called checkNames which I try to use thus:

std::vector <element*>::iterator listIter;

for (listIter = this->elementList.begin( ); listIter < this->elementList.end( ); listIter++)
if (listIter->checkName (elementName))
..

The problem I am getting is that the compiler gives back the following message for the last line:

error C2839: invalid return type 'std::vector<_Ty>::_Tptr' for overloaded 'operator ->'

Does anybody know what I am doing wrong please?

Mark
tis99 is offline   Reply With Quote
Old 05-25-2005, 06:49 AM   #2 (permalink)
Valmont
[code][/code] enforcer
 
Valmont's Avatar
 
Join Date: Mar 2003
Location: Netherlands
Posts: 1,544
Valmont is on a distinguished road
Code:
listiter->
is not defined by the standard. Do...
Code:
(*listiter).
offtopic tip

Don't do...
Code:
listIter < this->elementList.end( )
...in the for-loop. At every iteration, end() needs to be recalculated.
Do:
Code:
std::vector<element*>::iterator ItEnd;
ItEnd = elementList.end( );
for (listIter = elementList.begin( ); listiter != ItEnd; ++listIter)
{ }
- Observe !=.
Any iterator it < vector::end() is undefined since end() is a pointer *past* the last element. So this comparison makes technically no sense.
- Observe ++listiter
Your element object *may* create an internal temporary object and therefore *may* add runtime overhead due to copy constructor calls and destructor calls. Using the pre-increment operator prevents this.
__________________
Valmont is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
TIP (5 minute read): remainder on const objects Valmont Standard C, C++ 1 05-18-2005 08:22 PM
Sorting an array of any Comparable objects plague Java 1 03-14-2005 06:24 PM
sorting objects arrays by object properties sde Java 28 08-05-2004 05:51 AM


All times are GMT -8. The time now is 11:22 PM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting