what is wrong with this simple code?

A

asdf

Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?
 
S

sandy

I am a mere student so take what I say with a grain of salt.

I think get might be reserved, but if it isn't then you need to do:

int get(int x){return 1;}

and that should work.
 
T

Thomas Tutone

asdf said:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};

I think you mean:

int get(int x) { return 1; }

Best regards,

Tom
 
D

David Harmon

On 28 Nov 2006 20:01:47 -0800 in comp.lang.c++, "asdf"
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};

In the above, x would be the type of the unnamed function argument.
However, x is not a type.
 
S

Salt_Peter

asdf said:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};

The compiler in this context is unable to establish a signature for the
member function. Any parameter specified must therefore have a type in
order to construct the signature. Remember that parameters are objects
from the outside world insofar as the instance of the class is
concerned. get(...) doesn't need to pass x as a parameter since it is
already part of the object.
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?

Presumably, your goal is to use get(...) to "get" x's value all you
need to do is return the private integer. In order to protect x from
being modified through side-effects, get() should be constant.

class A
{
int x;
public:
A(int n = 1) : x(n) { } // or A() : x(1) { }
int get() const { return x; }
};

And we haven't touched copy ctors nor assignment operators.
 
J

James Willmott

asdf said:
Members defined in the private section are accessible to other class
members. So, I think the following is correct.

#include<iostream>
using namespace std;

class A{
int x;
public:
int get(x) {return 1;};
};

int main()
{
return 0;
}

However, there is a syntax error : identifier 'x'

what's the reason? the private member cannot be the parameter of the
public function?

class A{
int x;
public:
int get() { return x; };
};

Is this what you meant?
 

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

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top