Connecting Tech Pros Worldwide Help | Site Map

Beginner question: Pass object as class parameter

James
Guest
 
Posts: n/a
#1: Oct 13 '05
Why do I get the error C2597: illegal reference to non-static member
'ClientProxy::User' (plus a bunch of other errors)?

Obviously I'm not defining the passing of an object into a class correctly.
How do I do this correctly?


/* ClientProxy.cpp */

#include "ClientProxy.h"
Campaign RequestCampaign(User CurrentUser);
Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
{
return CurrentCampaign;
}


/* ClientProxy.h */

#ifndef __CLIENTPROXY__
#define __CLIENTPROXY__
#include "Campaign.h"
#include "User.h"

class ClientProxy
{
public:
Campaign RequestCampaign(User CurrentUser);

private:
Campaign CurrentCampaign;
User User;
};
/* END CLASS DEFINITION ClientProxy */
#endif /* __CLIENTPROXY__ */


AnonMail2005@gmail.com
Guest
 
Posts: n/a
#2: Oct 13 '05

re: Beginner question: Pass object as class parameter


> Campaign RequestCampaign(User CurrentUser);[color=blue]
> Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
> {
> return CurrentCampaign;
> }[/color]

What is that first line doing there? That may be a source of errors
since the function is not defined as a ClientProxy member function.
The second line above looks like the correct start of the function
definition.

mlimber
Guest
 
Posts: n/a
#3: Oct 13 '05

re: Beginner question: Pass object as class parameter



James wrote:[color=blue]
> Why do I get the error C2597: illegal reference to non-static member
> 'ClientProxy::User' (plus a bunch of other errors)?
>
> Obviously I'm not defining the passing of an object into a class correctly.
> How do I do this correctly?
>
>
> /* ClientProxy.cpp */
>
> #include "ClientProxy.h"
> Campaign RequestCampaign(User CurrentUser);
> Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
> {
> return CurrentCampaign;
> }
>
>
> /* ClientProxy.h */
>
> #ifndef __CLIENTPROXY__
> #define __CLIENTPROXY__
> #include "Campaign.h"
> #include "User.h"
>
> class ClientProxy
> {
> public:
> Campaign RequestCampaign(User CurrentUser);
>
> private:
> Campaign CurrentCampaign;
> User User;[/color]

This line is likely the source of the errors. The typename is used as a
variable name. Try:

User user_;

or something to that effect. Generally, people designate member
variables with some sort of tag (a prefixed or suffixed underscore or a
prefix of m_). Types are generally capitalized, and variable names
begin with a lower case letter. All this is merely stylistic, however.
[color=blue]
> };
> /* END CLASS DEFINITION ClientProxy */
> #endif /* __CLIENTPROXY__ */[/color]

Cheers! --M

Howard
Guest
 
Posts: n/a
#4: Oct 13 '05

re: Beginner question: Pass object as class parameter



"James" <james@hotmail.com> wrote in message
news:cXw3f.210$CI.45@newsfe2-gui.ntli.net...[color=blue]
> Why do I get the error C2597: illegal reference to non-static member
> 'ClientProxy::User' (plus a bunch of other errors)?
>
> Obviously I'm not defining the passing of an object into a class
> correctly. How do I do this correctly?
>
>
> /* ClientProxy.cpp */
>
> #include "ClientProxy.h"
> Campaign RequestCampaign(User CurrentUser);[/color]

That line's not needed. The function declaration is in the header, so you
don't put a prototype here.
[color=blue]
> Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
> {
> return CurrentCampaign;
> }
>
>
> /* ClientProxy.h */
>
> #ifndef __CLIENTPROXY__
> #define __CLIENTPROXY__
> #include "Campaign.h"
> #include "User.h"
>
> class ClientProxy
> {
> public:
> Campaign RequestCampaign(User CurrentUser);
>
> private:
> Campaign CurrentCampaign;
> User User;[/color]

Just guessing, but I suspect that this is the problem. You're giving the
type and the variable the same name. Change the variable name.

Personally, my variables start with small letters, and my types with capital
letters. Some people preface member variable names with "f" (for "field") or
"m_" (for "member").

-Howard


mlimber
Guest
 
Posts: n/a
#5: Oct 13 '05

re: Beginner question: Pass object as class parameter


Howard wrote:[color=blue]
> "James" <james@hotmail.com> wrote[/color]
[snip][color=blue][color=green]
> > Campaign RequestCampaign(User CurrentUser);[/color]
>
> That line's not needed. The function declaration is in the header, so you
> don't put a prototype here.[/color]
[snip]

It's not needed unless there actually is a non-member function with
that signature. Given that this is a newbie question, it could go
either way, though if such a function does exist, it probably
shouldn't.

Cheers! --M

James
Guest
 
Posts: n/a
#6: Oct 13 '05

re: Beginner question: Pass object as class parameter


"mlimber" <mlimber@gmail.com> wrote in message
news:1129227205.035420.273460@g43g2000cwa.googlegr oups.com...[color=blue]
> Howard wrote:[color=green]
>> "James" <james@hotmail.com> wrote[/color]
> [snip][color=green][color=darkred]
>> > Campaign RequestCampaign(User CurrentUser);[/color]
>>
>> That line's not needed. The function declaration is in the header, so
>> you
>> don't put a prototype here.[/color]
> [snip]
>
> It's not needed unless there actually is a non-member function with
> that signature. Given that this is a newbie question, it could go
> either way, though if such a function does exist, it probably
> shouldn't.[/color]

OK, I followed all suggestions and it is now compiling.

Thanks people.


James
Guest
 
Posts: n/a
#7: Oct 13 '05

re: Beginner question: Pass object as class parameter


<AnonMail2005@gmail.com> wrote in message
news:1129225915.806011.242170@o13g2000cwo.googlegr oups.com...[color=blue][color=green]
>> Campaign RequestCampaign(User CurrentUser);
>> Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
>> {
>> return CurrentCampaign;
>> }[/color]
>
> What is that first line doing there? That may be a source of errors
> since the function is not defined as a ClientProxy member function.
> The second line above looks like the correct start of the function
> definition.[/color]

The first line is there because Visio generated that line when it generated
the code from my class diagram. Being a beginner at C++ I guessed it had a
purpose, but obviously not.


Marcus Kwok
Guest
 
Posts: n/a
#8: Oct 13 '05

re: Beginner question: Pass object as class parameter


mlimber <mlimber@gmail.com> wrote:[color=blue]
> Generally, people designate member
> variables with some sort of tag (a prefixed or suffixed underscore or a
> prefix of m_).[/color]

You shouldn't prefix with an underscore. I believe the standard says
that names that start with an underscore followed by a capital letter,
and names with two underscores, are reserved for the implementation, so
to avoid the issue altogether, just avoid using underscores at the
beginning of a name.

--
Marcus Kwok
Closed Thread