C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old March 21st, 2005, 07:52 PM
notsoclever notsoclever is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 1 notsoclever User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 14 m 46 sec
Reputation Power: 0
newbie clueless about iterators

There is absolutely nothing clever or difficult about this code: it is one of those student things where you implement a structure of staff which then generates a list later.

I have this so far:

(well, obviously, there's more than that, but this is the bit which doesn't work)

Code:
string Table::toString()
{
	if (table.size() < 1)
		return "There are no staff records at the moment.";
	string str;
	//iterate through
	vector<Staff*>::iterator iter;
	for (iter = table.begin();iter != table.end(); ++iter) {
		Staff * tmp = *iter;
		str += tmp->toString();
	}
	return str;
}


where each of the Staff pointers in the list points too an object derived from a base class called Staff.

All nice and simple, what could go wrong? For some reason, when it runs the

Code:
		str += tmp->toString();

line, which is a function in the base class only, it crashes out. Painstaking research suggests to me that this is caused by a memory access exception. i can't remember the exact phrase as i have been working on it for 7 hours and can't think straight. This must be some stupid newbie mistake somewhere which looks really obvious to experienced programmers. Does anyone see it yet?

Reply With Quote
  #2  
Old March 22nd, 2005, 01:55 PM
Cirus Cirus is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Mar 2005
Posts: 273 Cirus User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 11 h 35 m 2 sec
Reputation Power: 4
I think the stamt.
Quote:
str += tmp->toString();
goes wrong because 'tmp' is an object to class Staff but not its instance. In other words, you have n't allocated memory to it.
write : tmp = new Staff() preceeding the above quoted stmt. .

The quoted statement is trying to concatenate strings. Concatenation is possible if memory is allocated to strings to be concatenated.

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > newbie clueless about iterators


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway