Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C++ Moderated > Re: const point...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 22 of 24 Topic 9518 of 10096
Post > Topic >>

Re: const pointer to class problem

by "Jeff Baker" <algorthjb@[EMAIL PROTECTED] > Apr 20, 2008 at 03:51 PM

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! ]
 




 24 Posts in Topic:
const pointer to class problem
t.wanat@[EMAIL PROTECTED]  2008-04-17 19:33:58 
Re: const pointer to class problem
Ulrich Eckhardt <dooms  2008-04-18 05:05:12 
Re: const pointer to class problem
paradox <ksjetski@[EMA  2008-04-18 06:13:16 
Re: const pointer to class problem
Friedhelm Hoerner <f.h  2008-04-18 06:13:16 
Re: const pointer to class problem
kasthurirangan.balaji@[EM  2008-04-18 06:13:17 
Re: const pointer to class problem
Tony Delroy <tony_in_d  2008-04-18 06:13:15 
Re: const pointer to class problem
Chris Uzdavinis <cuzda  2008-04-18 06:13:17 
Re: const pointer to class problem
Christopher <cpisz@[EM  2008-04-18 06:13:18 
Re: const pointer to class problem
Oncaphillis <oncaphill  2008-04-18 06:13:15 
Re: const pointer to class problem
red floyd <no.spam@[EM  2008-04-18 06:13:16 
Re: const pointer to class problem
=?ISO-8859-1?Q?Daniel_Kr=  2008-04-18 06:13:19 
Re: const pointer to class problem
"Thomas J. Gritzan&q  2008-04-18 06:13:17 
Re: const pointer to class problem
Lionel B <me@[EMAIL PR  2008-04-18 06:13:18 
Re: const pointer to class problem
Gerhard Menzl <clcppm-  2008-04-18 06:13:17 
Re: const pointer to class problem
Thomas Lehmann <t.lehm  2008-04-18 17:16:36 
Re: const pointer to class problem
David Pol <david@[EMAI  2008-04-18 17:15:18 
Re: const pointer to class problem
rwf_20 <rfrenz@[EMAIL   2008-04-18 17:17:39 
Re: const pointer to class problem
Mathias Gaunard <loufo  2008-04-18 17:18:25 
Re: const pointer to class problem
Todd Mars <tamnt54@[EM  2008-04-18 17:18:24 
Re: const pointer to class problem
SeanW <sean_woolcock@[  2008-04-18 17:17:37 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 15:47:59 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 15:51:21 
Re: const pointer to class problem
"Jeff Baker" &l  2008-04-20 21:01:04 
Re: const pointer to class problem
Ulrich Eckhardt <dooms  2008-04-21 10:25:11 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Tue Oct 14 8:50:07 CDT 2008.