Call by reference get function

Cannot convert 'this' pointer from 'const class' to &#

Page 1 of 1

2 Replies - 610 Views - Last Post: 08 June 2009 - 07:46 PM Rate Topic: -----

#1 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Call by reference get function

Posted 08 June 2009 - 06:14 PM

void extPersonType::printAddress() const
{
	string a,c,s,z;
	getAddress(a,c,s,z);//<------'cannot convert 'this' pointer from 'const extPersonType' to 'extPersonType &'
	cout<<"Street: "<<a<<endl
		<<"City :"<<c<<endl
		<<"State: "<<s<<endl
		<<"Zip: "<<z<<endl;

}//end print


void addressType::getAddress(string& sAddress, string& c, 
					string& s, string& z)
{	
	sAddress = streetAddress;
	c = city;
	s = state;
	z = zip;
}


Ok, so these two functions are from two different class implementation files. The second one, getAddress, is given to us by the Prof. The first one we get to define ourselves. Is there a way to make the error go away without taking the 'const' out of the definition? I commented the definition in there.

I know how to do this the "long way" where you just do the get functions one by one, but they seem to want to do it this way and I figure I better learn these small little programming tricks to save space eventually.

Is This A Good Question/Topic? 0
  • +

Replies To: Call by reference get function

#2 skaoth  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 91
  • View blog
  • Posts: 601
  • Joined: 07-November 07

Re: Call by reference get function

Posted 08 June 2009 - 06:57 PM

I think the problem is related to the constness of the functions
the printAddress() function is declared const.
However, the getAddress() function is not.


const provides a gurantee that the extPersonType will not change during the
call of printAddress(). However, since getAddress()
makes no such gurantees, the compiler assumes that this function can change the object.

So either make getAddress() const or make printAddress() non-const
Was This Post Helpful? 1
  • +
  • -

#3 agentkirb  Icon User is offline

  • D.I.C Head

Reputation: 2
  • View blog
  • Posts: 219
  • Joined: 28-December 08

Re: Call by reference get function

Posted 08 June 2009 - 07:46 PM

View Postskaoth, on 8 Jun, 2009 - 05:57 PM, said:

I think the problem is related to the constness of the functions
the printAddress() function is declared const.
However, the getAddress() function is not.


const provides a gurantee that the extPersonType will not change during the
call of printAddress(). However, since getAddress()
makes no such gurantees, the compiler assumes that this function can change the object.

So either make getAddress() const or make printAddress() non-const


So other than the const, its completely fine to do it that way?

It actually worked out that there was this hidden print address function in another class. But I'm having this same problem with other functions so now I know that they way I did it worked. Thanks.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1