Jump to content


 


Register a free account to unlock additional features at BleepingComputer.com
Welcome to BleepingComputer, a free community where people like yourself come together to discuss and learn how to use their computers. Using the site is easy and fun. As a guest, you can browse and view the various discussions in the forums, but can not create a new topic or reply to an existing one unless you are logged in. Other benefits of registering an account are subscribing to topics and forums, creating a blog, and having no ads shown anywhere on the site.


Click here to Register a free account now! or read our Welcome Guide to learn how to use this site.

Photo

C++ Accessing Member Functions of Elements During Iteration


  • Please log in to reply
5 replies to this topic

#1 PropagandaPanda

PropagandaPanda


  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:03 PM

Posted 27 June 2010 - 01:33 PM

Hello.

I've made a class and put members of it it into a set. While iterating through it though, I can't access the member functions. For example:
#include <iostream>
#include <set>

using namespace std;

class Rect{
	public:
		Rect(int X1,int Y1,int X2,int Y2);
		int x1,y1,x2,y2;

		bool IsValid();

};

Rect::Rect(int X1,int Y1,int X2,int Y2){
	//Left
	x1 = X1;//Bot x
	y1 = Y1;//Bot y

	//Right
	x2 = X2;//Top x
	y2 = Y2;//Top y
}


bool Rect::IsValid(){
	return (
		x1 < x2 &&
		y1 < y2
		);
}

typedef set<Rect,RectComp> RectSet;
RectSet Rects;

int main() {
	Rects.insert(Rect(0,0,9,9));
	for (RectSet::iterator R=Rects.begin();R!=Rects.end();++R){
		(*R).IsValid(); // <----- 'Rect::IsValid' : cannot convert 'this' pointer from 'const Rect' to 'Rect &'
	}
	return 0;
}

How would I change the function IsValid to make it work with this code? Or would I have to just create an outside function to do the job?

Thanks for any help.

With Regards,
The Panda

BC AdBot (Login to Remove)

 


#2 comet@earth

comet@earth

  • Members
  • 170 posts
  • OFFLINE
  •  
  • Local time:03:33 AM

Posted 28 June 2010 - 09:56 AM

which compiler are you using

#3 PropagandaPanda

PropagandaPanda

  • Topic Starter

  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:03 PM

Posted 28 June 2010 - 11:50 AM

Microsoft Visual C++2010 Express.

Thanks,
The Panda

#4 Synetech

Synetech

  • Members
  • 149 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:06:03 PM

Posted 11 August 2010 - 05:18 PM

Hmm, I did not have a problem compiling that line with VC++ 2003. Perhaps C99 changed something?
****** *** ****** * ****; * ***** **** ** *** **** ******* *** ****** ************ ****.

-- Synetech

#5 PropagandaPanda

PropagandaPanda

  • Topic Starter

  • Malware Response Team
  • 10,433 posts
  • OFFLINE
  •  
  • Gender:Male
  • Local time:06:03 PM

Posted 15 August 2010 - 12:38 PM

Hello.

I think I got it working using the member selection.
R->IsValid();

Thanks anyway.

With Regards,
The Panda

#6 Synetech

Synetech

  • Members
  • 149 posts
  • OFFLINE
  •  
  • Gender:Male
  • Location:Canada
  • Local time:06:03 PM

Posted 15 August 2010 - 02:12 PM

Well yes, of course that would work, but you should also have been able to dereference the pointer.
****** *** ****** * ****; * ***** **** ** *** **** ******* *** ****** ************ ****.

-- Synetech




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users