Home > error c2662 > error c2662 copy constructor

Error C2662 Copy Constructor

Contents

Programming Boards C++ Programming Copy constructor error, 'this' pointer conversion Getting started with C or C++ | C Tutorial | C++ Tutorial | C and C++ FAQ error c2662 conversion loses qualifiers | Get a compiler | Fixes for common problems Thread: Copy constructor error, 'this'

Error C2662 Cannot Convert 'this' Pointer From

pointer conversion Thread Tools Show Printable Version Email this Page… Subscribe to this Thread… Display Linear Mode Switch to Hybrid Mode c++ error c2662 Switch to Threaded Mode 01-08-2004 #1 bennyandthejets View Profile View Forum Posts mustang benny Join Date Jul 2002 Posts 1,401 Copy constructor error, 'this' pointer conversion Making a copy constructor, I am getting a problem. Code: CPost::CPost(const CPost& rhs) { rhs.GetMessageLength(); //error on this line } error C2662: 'GetMessageLength' : cannot convert 'this' pointer from 'const class CPost' to 'class CPost &' Some discussions I found about this recommended taking away the const, but when I do that, MS VC++ 6.0 wont recognize it as a copy constructor. Is anyone aware of a solution to this problem? benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials 01-08-2004 #2 Shakti View Profile View Forum Posts Registered User Join Date Aug 2003 Posts 1,218 Try to make the function const aswell, like Code: class c { public: c(const c& rhs); int GetSomething() const { return something; } private: int something; }; c::c(const c& rhs) { rhs.GetSomething(); } Not sure if that is it but according to msdn this should solve your problem. 01-08-2004 #3 bennyandthejets View Profile View Forum Posts mustang benny Join Date Jul 2002 Posts 1,401 That worked, thanks. But what if the function I want to call CANNOT be const, ie, it modifies the object? benforbes@optusnet.com.au Microsoft Visual Studio .NET 2003 Enterprise Architect Windows XP Pro Code Tags Programming FAQ Tutorials 01-08-2004 #4 Shakti View Profile View Forum Posts Registered User Join Date Aug 2003 Posts 1,218 Then you cant send the object as a const. At least this is how I think it is. 01-08-2004 #5 Prelude View Profile View Forum Posts Visit Homepage Code Goddess Join Date Sep 2001 Posts 9,880 >But what if the function I want to call CANNOT be const, ie, it modifies the object? Look up the mutable keyword. I can't guarante

Visual Studio Languages , Windows Desktop Development > C++ Standards, Extensions, and Interop Question 0 Sign in to vote error C2662: 'PivotTransformable::Pivot::get' : cannot convert 'this' pointer from 'const Vertex' to 'PivotTransformable %' Hello! Such an error I'm getting, when I tried to write a copy constructor for the Vertex ref class. It looks like this: Vertex::Vertex(const Vertex% vertex) : PivotTransformable(vertex.Pivot) { Coords = vertex.coords; } Vertex derives from PivotTransformable ref class. PivotTransformable contains a handle to an instance of PivotPoint ref class. It's private, so I have a property named Pivot looking like this: //this in header file: property PivotPoint^ Pivot { PivotPoint^ http://cboard.cprogramming.com/cplusplus-programming/49140-copy-constructor-error-pointer-conversion.html get(); protected: void set(PivotPoint^ value); } //and this in source file: PivotPoint^ PivotTransformable::Pivot::get() { return pivot; } void PivotTransformable::Pivot::set(PivotPoint^ value) { pivot = value; } (This handle is named pivot.) The property has protected set, so Vertex could set it. The only way I could solve this problem, is to make this field (handle 'pivot') protected in PivotTransformable class. Then I could write Vertex::Vertex(const Vertex% vertex) : PivotTransformable(*(vertex.pivot)) { Coords = https://social.msdn.microsoft.com/Forums/vstudio/en-US/b06b4aea-b2f0-4c0f-9bf5-98c3f86ce732/cannot-convert-this-pointer-from-problem-with-copy-constructor?forum=vclanguage vertex.coords; } and it worked perfectly fine. But I don't like this, cause it's not as encapsulated, as I'd like to. I think it may be something wrong with my property, but I'm not exactly sure what. Any ideas? Monday, May 30, 2011 8:22 PM Reply | Quote Answers 0 Sign in to vote Idiomatic copy-construction for value semantics in C++/CLI would use the constructor signature ClassName(ClassName% rhs); Idiomatic copy-assignment for value semantics in C++/CLI would use the operator signature ClassName% operator =(ClassName% rhs); I.e., both are what one would expect, minus the concept of `const'-ness. Idiomatic copy-construction for reference semantics .NET-wide is to implement the ICloneable interface. Marked as answer by Rob Pan Monday, June 06, 2011 1:52 AM Tuesday, May 31, 2011 7:54 PM Reply | Quote All replies 0 Sign in to vote Sushi271 wrote: error C2662: 'PivotTransformable::Pivot::get' : cannot convert 'this' pointer from 'const Vertex' to 'PivotTransformable %' The getter is not const, the compiler has to assume that it may modify the object it's called on. Thus, you cannot call it on const Vertex. [code] //this in header file: property PivotPoint^ Pivot { PivotPoint^ get(); I'm not very familiar with C++/CLI syntax, but I expect it to look something like this: PivotPoint^ get() const; Igor Tandetnik Mo

"this' pointer from const string1030 to string 1030& FILE I CAN CHANGE: 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include #include "String1030.h" String1030::String1030(const char *buf):mysize(0) { error c2662 //setString(0); } String1030::String1030(const String1030& oldstring): mysize(0) { if(oldstring.getSize()<=0) { setSize(0); } else { setSize(oldstring.getSize()); for(int i = 0; i < (mysize+1); i++) { buffer[i] = oldstring.buffer[i]; //.buffer[i]; } } } String1030::~String1030() error c2662 copy { delete [] buffer; } String1030& String1030::operator=(const String1030& right) { if((&right == this)) //change made { if(right.getSize()<=0) { setSize(0); } else { //setSize(getSize()); for(int i = 0; i < (mysize+1); i++) { buffer[i] = right.buffer[i]; } } } return *this; } char& String1030::operator[](int index) { if(!(index < 0 || index >= mysize)) //recheck { return buffer[index]; } else return buffer[0]; } int String1030::getSize(void) { return mysize; } void String1030::setSize(int newsize) { if(!(newsize < 0)) { if(&buffer != NULL) //reeck { mysize = newsize; //buffer = new char[mysize+1]; } /*else {

class ArrayClass' to 'class ArrayClass &' Conversion loses qualifiersand it points to this line of code... Asize =OtherArray.AsizeIs();I'm not even using the this pointer in my copy constructor or in the program for that matter... in the help it said to get rid of the const on your object that you are passing or make the function call a const... when i take the const out of my parameter list i get linker errors... any clue as to what to do? Follow Ups: Nevermind... i got it Susan 22:56:07 03/26/01 (0) Post a Followup Name: E-Mail: Subject: Comments: : I keep getting this error....error C2662: 'AsizeIs' : cannot convert 'this' pointer from 'const class ArrayClass' to 'class ArrayClass &' : Conversion loses qualifiers : and it points to this line of code... : Asize =OtherArray.AsizeIs(); : I'm not even using the this pointer in my copy constructor or in the program for that matter... in the help it said to get rid of the const on your object that you are passing or make the function call a const... when i take the const out of my parameter list i get linker errors... any clue as to what to do? Optional Link URL: Link Title: Optional Image URL: [ Follow Ups ] [ Post Followup ] [ CS1704 Web Discussion Board ] [ FAQ ]

 

Related content

c2662 error

C Error table id toc tbody tr td div id toctitle Contents div ul li a href Error C a li li a href Error C a li li a href Error C a li li a href Error C Conversion Loses Qualifiers a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies of relatedl this site About Us Learn more about Stack Overflow the p h id Error C p company Business Learn more about hiring developers

error 1 error c2662

Error Error C table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert This Pointer From Const To Conversion Loses Qualifiers a li li a href Const Method a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards Events Community relatedl Magazine Forums Blogs Channel Documentation APIs and reference error c conversion loses qualifiers Dev centers Retired content Samples We re sorry The content you requested has error c cannot convert this pointer from

error c2662 msdn

Error C Msdn table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href C a li li a href C Const Function a li ul td tr tbody table p XXX to XXX Visual Studio Languages Windows Desktop Development C Standards Extensions and Interop Question relatedl Sign in to vote Please look at my p h id Error C Conversion Loses Qualifiers p code pragma once class CBox protected double m length double m width double m high error c cannot convert this pointer from

error c2662 volatile

Error C Volatile table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li li a href C Error C a li li a href C Cannot Convert This Pointer From Const a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies relatedl of this site About Us Learn more about Stack Overflow error c conversion loses qualifiers the company Business Learn more about hiring

error c2662 cannot convert this

Error C Cannot Convert This table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href Error C Cannot Convert This Pointer From Const To Conversion Loses Qualifiers a li li a href Const Function a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss relatedl the workings and policies of this site About Us Learn error c cannot convert this pointer from const to more about Stack

error c2662

Error C table id toc tbody tr td div id toctitle Contents div ul li a href Error C a li li a href Error C a li li a href Error C a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups relatedl TechRewards Events Community Magazine Forums Blogs Channel Documentation error c conversion loses qualifiers APIs and reference Dev centers Retired content Samples We re sorry The error c cannot convert this pointer from content you requested has been removed You ll

error c2662 this const

Error C This Const table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href Const Method a li li a href Const cast Example a li li a href Const Function a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups TechRewards relatedl Events Community Magazine Forums Blogs Channel Documentation APIs c error c cannot convert this pointer from const to and reference Dev centers Retired content Samples We

error c2662 visual studio

Error C Visual Studio table id toc tbody tr td div id toctitle Contents div ul li a href Error C Conversion Loses Qualifiers a li li a href C Cannot Convert This Pointer From Const a li li a href Cannot Convert This Pointer From Const To a li li a href C Const Function a li ul td tr tbody table p resources Windows Server resources Programs MSDN subscriptions Overview Benefits Administrators Students Microsoft Imagine Microsoft Student Partners ISV Startups relatedl TechRewards Events Community Magazine Forums Blogs Channel p h id Error C Conversion Loses Qualifiers p Documentation

error c2662 cannot

Error C Cannot table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li li a href Cannot Convert This Pointer From Const To a li li a href Const cast Example a li li a href Const Correctness a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss the workings and policies relatedl of this site About Us Learn more about Stack Overflow the p h id Error

error c2662 iterator

Error C Iterator p here for a quick overview of the site Help Center Detailed answers to any questions you might have Meta Discuss relatedl the workings and policies of this site About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of million programmers just like you helping each other Join them it only takes a minute Sign up Visual Studio error C - calling const methods

error c2662 const

Error C Const table id toc tbody tr td div id toctitle Contents div ul li a href Cannot Convert This Pointer From Const To a li li a href Const cast Example a li li a href Const Function a li ul td tr tbody table p here for a quick overview of the site relatedl Help Center Detailed answers to any questions you c c might have Meta Discuss the workings and policies of this site error c conversion loses qualifiers About Us Learn more about Stack Overflow the company Business Learn more about hiring developers or error

error c2662 cannot convert

Error C Cannot Convert table id toc tbody tr td div id toctitle Contents div ul li a href Visual Studio Error C a li li a href C Error C a li li a href Const Method a li ul td tr tbody table p here for a quick overview of the site Help Center Detailed answers relatedl to any questions you might have Meta Discuss the error c cannot convert this pointer from workings and policies of this site About Us Learn more about p h id Visual Studio Error C p Stack Overflow the company Business Learn