|
From: Jeff Baker on 20 Apr 2008 06:47 > 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; > } Why do you what to create extra objects and construct for each? Here is a quicker way, #ifndef H1_H #define H1_H # include <iostream> class FOO { public: FOO():m_ver(1){PrintVer();}; int Version(){return m_ver;}; void PrintVer(); private: int m_ver; }; #endif ///////////////////////////// #include "h1.h" void FOO::PrintVer( ) // no contructor created in Printver() { int v = Version(); //no extra objects to construct std::cout << "The version is " << v <<std::endl; } ///////////////////////////// #include "h1.h" int main() { FOO a; return 0; } Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Baker on 20 Apr 2008 06:51 I am having trouble with this one. The const the way it is written will not function. I rewrote it and find a link error that is listed with the code below. > 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). > Regards, > David I get Link errors - I think a constructor should pass an object and int v = fp.Version() should be a object This would change how I wrote too. I think the problem is in the main() function. # include <iostream> class FOO { public: FOO():m_ver(1){/*PrintVer();*/}; int Version(){return m_ver;}/*const*/; // with or without the const void PrintVer(/*const*/ FOO&); // with or without the const private: int m_ver; }; void FOO::PrintVer(/*const*/FOO& fp) // with or without the const { fp.m_ver = fp.Version(); // or int v = fp.Version() std::cout << "The version is " << v << std::endl; } int main() { FOO a; PrintVer(a); return 0; } scribble fatal error LNK1120: 1 unresolved externals scribble error LNK2019: unresolved external symbol _main referenced in function _mainCRTStartup Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Baker on 20 Apr 2008 12:01 <t.wanat(a)nci-sw.com> wrote in message news:a2e098d9-a270-4a3f-ad0f-e0a16ddbdabc(a)24g2000hsh.googlegroups.com... > > 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. I found a way to have all const. The return value as already specified is obsolete. If using the copy constructor, which isn't really necessary the code will go as this, #include <iostream> class FOO { public: FOO(){/*PrintVer();*/}; FOO(int c):m_ver(c){} int Version()const {return m_ver;} // with or without the const void PrintVer(const FOO&)const; // with or without the const private: int m_ver; }; void FOO::PrintVer(const FOO& fp)const // with or without the const { //fp.m_ver = fp.Version(); std::cout << "The version is " << fp.Version() << std::endl; } int main() { FOO a(1); a.PrintVer(a); return 0; } Best, Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Ulrich Eckhardt on 21 Apr 2008 01:25 Jeff Baker wrote: > I am having trouble with this one. The const the way it is written will > not function. Could you describe where you see an error? I couldn't spot any error in the code, though I didn't actually try to compile it. > I rewrote it and find a link error that is listed with the > code below. > scribble fatal error LNK1120: 1 unresolved externals > scribble error LNK2019: unresolved external symbol _main referenced in > function _mainCRTStartup This is not an error in your program. Rather, it's an artifact from programming under win32, where you have two different entry points to programs, not just main(). You need to switch your program to target 'console' as subsystem. Note that since this is not C++ specific, you should direct further discussion of that topic elsewhere. Uli -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
First
|
Prev
|
Pages: 1 2 3 4 5 Prev: auto_ptr for array of built-ins Next: assignment operator using a constructor |