Beginner question: Pass object as class parameter

J

James

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__ */
 
A

AnonMail2005

Campaign RequestCampaign(User CurrentUser);
Campaign ClientProxy::RequestCampaign(User CurrentUser) /* ERROR HERE */
{
return CurrentCampaign;
}

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.
 
M

mlimber

James said:
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;

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.
};
/* END CLASS DEFINITION ClientProxy */
#endif /* __CLIENTPROXY__ */

Cheers! --M
 
H

Howard

James said:
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);

That line's not needed. The function declaration is in the header, so you
don't put a prototype here.
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;

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
 
M

mlimber

Howard said:
That line's not needed. The function declaration is in the header, so you
don't put a prototype here.
[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
 
J

James

mlimber said:
Howard said:
That line's not needed. The function declaration is in the header, so
you
don't put a prototype here.
[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.

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

Thanks people.
 
J

James

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.

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.
 
M

Marcus Kwok

mlimber said:
Generally, people designate member
variables with some sort of tag (a prefixed or suffixed underscore or a
prefix of m_).

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top