Home > error c2662 > error c2662 volatile

Error C2662 Volatile

Contents

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 this site About Us Learn more about Stack Overflow error c2662 conversion loses qualifiers the company Business Learn more about hiring developers or posting ads with us Stack Overflow

Error C2662 Cannot Convert 'this' Pointer From

Questions Jobs Documentation Tags Users Badges Ask Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of

C++ Error C2662

4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: Sign up Defining volatile class object up vote 5 down vote favorite Can the volatile be used for class objects?

C2662 Cannot Convert This Pointer From Const

Like: volatile Myclass className; The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &' What is the problem here and how to solve it? EDIT: class Queue { private: struct Data *data; int amount; int size; public: Queue (); ~Queue (); bool volatile push(struct Data element); bool volatile pop(struct Data *element); void volatile cannot convert this pointer from const to & cleanUp(); }; ..... volatile Queue dataIn; ..... EnterCriticalSection(&CriticalSection); dataIn.push(element); LeaveCriticalSection(&CriticalSection); c++ volatile share|improve this question edited Jun 20 '10 at 5:19 asked Jun 20 '10 at 4:38 maximus 1,41753574 Note that in C++, volatile does not guarantee thread-safety. That's different from, for example, .NET, where it does. –sbi Jun 20 '10 at 7:49 add a comment| 4 Answers 4 active oldest votes up vote 9 down vote accepted Yes, you can, but then you can only call member functions that are declared volatile (just like the const keyword). For example: struct foo { void a() volatile; void b(); }; volatile foo f; f.a(); // ok f.b(); // not ok Edit based on your code: bool volatile push(struct Data element); declares a non-volatile member function that returns a bool volatile (= volatile bool). You want bool push(struct Data element) volatile; share|improve this answer edited Jun 20 '10 at 5:29 answered Jun 20 '10 at 4:40 Jesse Beder 18.3k1372117 1 What about constructors and destructors and variables(properties) declared in class? –maximus Jun 20 '10 at 4:44 I did as you said, but anyway the same error messages.. –maximus Jun 20 '10 at 4:49 1 @maximus, constructors and destructors can't be overloaded with volatile (or const), and member variables inherit these prop

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 this site About Us Learn more about Stack Overflow the company Business Learn more about hiring c++ const method developers or posting ads with us Stack Overflow Questions Jobs Documentation Tags Users Badges Ask c++ const function Question x Dismiss Join the Stack Overflow Community Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join const_cast them; it only takes a minute: Sign up Problems with STL as a static volatile member up vote 0 down vote favorite I'm trying to compile something like this: #include class thread { public: static volatile std::list http://stackoverflow.com/questions/3078237/defining-volatile-class-object threadList; //This is also protected by a mutex }; volatile std::list thread::threadList; void main() { thread A; thread::threadList.push_back(&A); thread::threadList.remove(&A); } I get these errors when trying to compile: test.cpp(14): error C2663: 'std::list::push_back' : 2 overloads have no legal conversion for 'this' pointer with [ _Ty=thread * ] test.cpp(15): error C2662: 'std::list::remove' : cannot convert 'this' pointer from 'volatile std::list' to 'std::list &' with [ _Ty=thread * ] Conversion loses qualifiers What am I doing wrong? EDIT: fixed some http://stackoverflow.com/questions/4812256/problems-with-stl-as-a-static-volatile-member formatting errors with < and > EDIT2: threadList is protected by a mutex, I just didn't put it here for simplicity c++ share|improve this question edited Jan 27 '11 at 3:30 asked Jan 27 '11 at 2:51 Mark 848925 1 Why are you using volatile? –Sam Miller Jan 27 '11 at 3:08 I'm betting he wants a list of all c++0x or boost thread s he has and he wants the container holding them to be synchronized –KitsuneYMG Jan 27 '11 at 3:12 add a comment| 4 Answers 4 active oldest votes up vote 4 down vote accepted Remove volatile and remove the problem. So for the real answer, it might help if you explain why you need to volatile, and what it is intended to mean here. It won't make the whole list be treated as volatile. See also previous answer share|improve this answer answered Jan 27 '11 at 3:06 Keith 5,922821 I'm using volatile because the list will be used by multiple threads. Also this code is simplified; my actual code has the list protected by a mutex. Now that I think about it, would volatile even make a difference in that case? –Mark Jan 27 '11 at 3:12 1 @Mark: No. If you have a lock around it, volatile is not needed for simple access protection. –JaredC Jan 27 '11 at 3:21 1 @Mark- Vo

コンパイラ先生は優秀で、NRVOという最適化をしてくれることがあります。この最適化を施してくれると次のようなコードにおいて、 #include class Hoge { public: Hoge() : val_(0) { std::cout << "コンストラクト" << std::endl; } Hoge(const Hoge &rhs) : val_(rhs.val_) { std::cout << "コピーコンストラクト" << std::endl; } http://d.hatena.ne.jp/nagoya313/20100509/1273371889 ~Hoge() { std::cout << "デストラクト" << std::endl; } void setVal(int val) { val_ = val; } int val() const { return http://kechengpuzi.com/q/s3078237/a/1 val_; } private: int val_; }; Hoge createHoge() { Hoge hoge; hoge.setVal(256); return hoge; } int main() { Hoge hoge(createHoge()); error c2662 std::cout << "val = " << hoge.val() << std::endl; return 0; } 出力: コンストラクト val = 256 デストラクト となって、コピーコンストラクタの呼び出しが消されてしまいます。普段はこの最適化はうれしいことが多いのですが、時にはコピーコンストラクタをちゃんと呼んでほしい場合もあるでしょう。 そこで思い出されるのがvolatile修飾子の存在です。こいつを変数に付けると、最適化を抑止してくれます。そこでcreateHoge関数とmain関数内を次のように修正してみると、 Hoge createHoge() { Hoge hoge; hoge.setVal(256); return hoge; } int main() { volatile Hoge hoge(createHoge()); std::cout << "val = " c2662 cannot convert << hoge.val() << std::endl; return 0; } エラー: error C2662: 'Hoge::setVal' : 'volatile Hoge' から 'Hoge &' へ 'this' ポインターを変換できません。変換で修飾子が失われます。 error C2558: class 'Hoge' : コピー コンストラクターが使用できないか、'explicit' として宣言されています。 'Hoge::val' : 'volatile Hoge' から 'const Hoge &' へ 'this' ポインターを変換できません。変換で修飾子が失われます。 という二つのエラーが出てきます。要するに変数がvolatile宣言されてるのに、volatileじゃないメンバ関数とかvolatileじゃない参照をとるコピーコンストラクタが呼ばれたよと怒られるわけです。そこで、Hogeクラスの方を次のように修正します。 class Hoge { public: Hoge() : val_(0) { std::cout << "コンストラクト" << std::endl; } Hoge(const volatile Hoge &rhs) : val_(rhs.val_) { std::cout << "最適化阻止コピーコンストラクト" << std::endl; } ~Hoge() { std::cout << "デストラクト" << std::endl; } Hoge &operator =(const Hoge &rhs) { val_ = rhs.val_; std::cout << "コピー" << std::endl; } void setVal(int val) volatile { val_ = val; } int val() const volatile { return val_; } private: int val_; }; まず、setValメソッドをvolatileメンバ関数にして、コピーコンストラクタに、const volatile参照を受けるものを追加しました。ところがこの修正だけだと error C2558: class 'Hoge' : コピー コンス

used for class objects? Like: volatile Myclass className; The problem is that it doesn't compile, everywhere when some method is invoked, the error says: error C2662: 'function' : cannot convert 'this' pointer from 'volatile MyClass' to 'MyCLass &' What is the problem here and how to solve it? EDIT: class Queue { private: struct Data *data; int amount; int size; public: Queue (); ~Queue (); bool volatile push(struct Data element); bool volatile pop(struct Data *element); void volatile cleanUp(); }; ..... volatile Queue dataIn; ..... EnterCriticalSection(&CriticalSection); dataIn.push(element); LeaveCriticalSection(&CriticalSection);

Game | 2010-06-20 I think he meant to say bool push(struct Data element) volatile; instead of bool volatile push(struct Data element); Also have a look here http://www.devx.com/tips/Tip/13671 SUPPORT US 筝恰絵緇篆≦篌 莚丞阪, 醇筝贋篏莚丞膣√ 息 2016 莚丞阪 kechengpuzi@gmail.com 莚丞阪筝膾283紊 莚丞阪鐚罸鎀篏菴罩ヤ ICP紊16011938-2

 

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 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 copy constructor

Error C Copy Constructor table id toc tbody tr td div id toctitle Contents div ul li a href Error C Cannot Convert this Pointer From a li ul td tr tbody table p Programming Boards C Programming Copy constructor error 'this' pointer conversion Getting started with C or C C relatedl Tutorial C Tutorial C and C FAQ error c conversion loses qualifiers Get a compiler Fixes for common problems Thread Copy constructor error 'this' p h id Error C Cannot Convert this Pointer From p pointer conversion Thread Tools Show Printable Version Email this Page hellip Subscribe to

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