referance objects cause "no match for" kinds error

hello, while I was using msvc I could use a referance and actual object interchangably however when I switch to g++ 4x, I start getting no match for ... kind of errors, here is a stuation

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
		ImageDataPtr& operator= (ImageDataPtr& p) {
			if(ptr != p.ptr) {
				dispose();
				ptr = p.getPtr();
				count = p.getCount();
				++*count;
			}
			return *this;
		}

ImageDataPtr copyImage(ImageData* bitmap, Vec4 box);



ImageDataPtr lenaImgPtr(c.decode(lena));


/* this line cause a no match error because copyImage returns ImgDataPtr
*  however, there is only one "=" operator in ImgDataPtr which takes ImgDataPtr&
*/
lenaImgPtr = c.copyImage(lenaImgPtr.getPtr(), Vec4(0,0,400,400));


this kind of usage was valid for mscv and seems correct to me as well :) saves me from writing same thing
hmmmm... try to compile with another program ...
i dont see anything wrong...
I also built project with g++ 3.4 but it caused a warning because of an un suported visibility flag, however above issue was also exist as well.
1
2
3
ImageDataPtr& operator= (ImageDataPtr& p); 
//should be
ImageDataPtr& operator= (const ImageDataPtr& p);

This way you can use temporary objects.
thanx, ImageDataPtr& operator= (const ImageDataPtr& p); this worked
but leaded to an other question

if I make p constant this line causes an error

ptr = p.getPtr();
error: passing ‘const amz::ImageDataPtr’ as ‘this’ argument of ‘T* amz::CountedPtr<T>::getPtr() [with T = amz::ImageData]’ discards qualifiers

however this one

ptr = p.ptr works quite good
If you want that your method works with constant objects, add const at the end of the definition
1
2
3
4
5
6
class A{
  void foo();            //this two methods are actually distinct
  void foo() const;  //this will be called only with constants objects
  void bar() const; //this is called by both
  void foobar();      //this can't be called by a constant object
}


However, why you need a getter if you variable is public?
Last edited on
variable is not public, I did this "ptr = p.ptr " in copy constructor.
Topic archived. No new replies allowed.