|
From: David Pol on 18 Apr 2008 08:15 On 18 abr, 03:33, t.wa...(a)nci-sw.com wrote: > I'm trying to slowly convert some C code to C++ and have encounter > what's probably a trivial problem. > > The easiest way to describe the problem is with an example: > > class FOO > { > public: > int Version(void){return m_ver;}; > FOO(){m_ver = 1;}; > private: > int m_ver; > }; > > void PrintVer(const FOO * fp) > { > int v = fp->Version(); /* error here */ > > cout << "The version is " << v << endl; > } > > When I try to compile, I get the error > 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO > &' > > on the line containing fp->Version(). > > If I remove the const from the routine declaration, it works fine, but > I'd rather not do that. I guess I don't understand what the compiler > is trying to tell me, and have no idea of how to work around this > without removing the const. Hello, FOO::Version() is not a const member function, so it cannot be invoked by a const FOO object, or via a pointer (or a reference) to a const FOO object. It makes sense to mark FOO::Version() as a const member function, because it does not mutate its object (it merely inspects the m_ver member variable of its object): #include <iostream> class FOO { public: int Version() const { return m_ver; } FOO() : m_ver(1) {} private: int m_ver; }; void PrintVer(const FOO& fp) { int v = fp.Version(); std::cout << "The version is " << v << std::endl; } int main() { FOO f; PrintVer(f); return 0; } Note the use of an initialization list in the default constructor of FOO [1] and the change of the parameter type of PrintVer() to a reference to a const FOO (it is generally preferable to pass an argument via a reference instead of passing it via a pointer unless the argument can be legitimately null). [1] See: http://www.parashift.com/c++-faq-lite/ctors.html#faq-10.6 Regards, David -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: rwf_20 on 18 Apr 2008 08:17 On Apr 17, 9:33 pm, t.wa...(a)nci-sw.com wrote: > When I try to compile, I get the error > 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO > &' > > on the line containing fp->Version(). fp is a pointer to const FOO, and you are calling a non-const method Version(). Declare Version as a const member function: int Version(void) const { return m_ver; } Ryan -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Mathias Gaunard on 18 Apr 2008 08:18 On 18 avr, 03:33, t.wa...(a)nci-sw.com wrote: > The easiest way to describe the problem is with an example: > > class FOO > { > public: > int Version(void){return m_ver;}; > FOO(){m_ver = 1;}; > private: > int m_ver; > }; > > void PrintVer(const FOO * fp) > { > int v = fp->Version(); /* error here */ > > cout << "The version is " << v << endl; > } class Foo { public: Foo() : m_ver(1) { } int Version() const { return m_ver; } private: int m_ver; }; void PrintVer(const Foo& f) { std::cout << "The version is " << f.Version() << std::endl; } -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Todd Mars on 18 Apr 2008 08:18 > class FOO > { > public: > int Version(void){return m_ver;}; > FOO(){m_ver = 1;}; > private: > int m_ver; > }; > > void PrintVer(const FOO * fp) > { > int v = fp->Version(); /* error here */ > > cout << "The version is " << v << endl; > } > > When I try to compile, I get the error > 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO > &' The 'Version' member function needs to be declared as const. int Version(void)const {return m_ver;}; -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: SeanW on 18 Apr 2008 08:17 > class FOO > { > public: > int Version(void){return m_ver;}; > FOO(){m_ver = 1;}; > private: > int m_ver; > }; > > void PrintVer(const FOO * fp) > { > int v = fp->Version(); /* error here */ > > cout << "The version is " << v << endl; > } > > When I try to compile, I get the error > 'FOO::Version' cannot convert 'this' pointer from 'const FOO' to 'FOO > &' > > on the line containing fp->Version(). > > If I remove the const from the routine declaration, it works fine, but > I'd rather not do that. I guess I don't understand what the compiler > is trying to tell me, and have no idea of how to work around this > without removing the const. It *is* a bad error message, but it's trying to tell you that you're not respecting the promise you made with by qualifying the pointer with "const" in the parameter list: not to change the object either by direct access to its data or by calling a method on it that hasn't made that promise, such as Version(). The way to tell the compiler that Version() promises not to change the object it's called on is with this unfortunate syntax: int Version(void) const {return m_ver;} That "const" after the parameters means that the implicit "this" pointer will be treated as if declared "const FOO*" rather than just "FOO*", which is the mismatch that the compiler is ineptly trying to tell you about. It just throws in the ampersand to throw you off. Sean -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Next
|
Last
Pages: 1 2 3 4 5 Prev: auto_ptr for array of built-ins Next: assignment operator using a constructor |