copy constructor

Started by
3 comments, last by Storyyeller 15 years, 1 month ago
ok I was getting a bug in my reference counting system(C++ btw), and I believe it was because I was using the default copy constructor, so when an object was copied, the original object's destructor decrements the reference count, but the copy constructor wasn't incrementing it. The problem is, after I made the copy constructor, my code won't compile, here is the error 1>c:\program files\microsoft visual studio 9.0\vc\include\vector(1211) : error C2558: class 'PVertex' : no copy constructor available or copy constructor is declared 'explicit' 1> c:\program files\microsoft visual studio 9.0\vc\include\vector(1153) : while compiling class template member function 'void std::vector<_Ty>::_Insert_n(std::_Vector_const_iterator<_Ty,_Alloc>,unsigned int,const _Ty &)' 1> with 1> [ 1> _Ty=PVertex, 1> _Alloc=std::allocator<PVertex> 1> ] 1> c:\users\hugh\documents\programming\c++\games\3d\d3d\terrain\terrain.h(18) : see reference to class template instantiation 'std::vector<_Ty>' being compiled 1> with 1> [ 1> _Ty=PVertex 1> ] _Ty _Tmp = _Val; // in case _Val is in sequence 1>c:\program files\microsoft visual studio 9.0\vc\include\vector(1235) : error C2558: class 'PVertex' : no copy constructor available or copy constructor is declared 'explicit' I googled it, and everything says that i had to have made the copy constructor private, or explicit, but I didn't here is the code //public interface public: PVertex(); PVertex(LPDIRECT3DDEVICE9 device); PVertex(float x,float y,float z,LPDIRECT3DDEVICE9 device); PVertex(PVertex & ref); ~PVertex(void); //copy constructor PVertex::PVertex(PVertex & ref) { PVertex::referenceCounter++; this->x = ref.x; this->y = ref.y; this->z = ref.z; } I can't figure out what I'm doing wrong(I've never played with copy constructors before, since I've never had the need for one)
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Advertisement
Try:
PVertex(const PVertex& ref);
haha, thanks a lot for the help, the about.com tutorial i browsed for a second didn't have the const in there
--------------------------------------Not All Martyrs See Divinity, But At Least You Tried
Quote:Original post by godsenddeath
ok I was getting a bug in my reference counting system(C++ btw)

Just curious, what are you using this ref counting system for?

As for copy constructors, you do need them as soon as you're storing objects inside std containers. For plain data objects, the default copy constructor et al will suffice, but for anything more complex (for example, an object that dynamically allocates resources), you'll want to give careful thought to what should happen with those resources when you make a copy of the object. Do you need a deep or a shallow copy? For a shallow copy, who will own those resources (e.g. who has to clean them up)?
Create-ivity - a game development blog Mouseover for more information.
Would storing pointers to objects inside of the container instead of the objects themselves work?
I trust exceptions about as far as I can throw them.

This topic is closed to new replies.

Advertisement