Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > what is wrong with this simple code?

Reply
Thread Tools

what is wrong with this simple code?

 
 
asdf
Guest
Posts: n/a
 
      11-29-2006
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?

 
Reply With Quote
 
 
 
 
sandy@murdocks.on.ca
Guest
Posts: n/a
 
      11-29-2006
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.

asdf wrote:
> 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?


 
Reply With Quote
 
 
 
 
Thomas Tutone
Guest
Posts: n/a
 
      11-29-2006
asdf wrote:
> 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

 
Reply With Quote
 
David Harmon
Guest
Posts: n/a
 
      11-29-2006
On 28 Nov 2006 20:01:47 -0800 in comp.lang.c++, "asdf"
<> wrote,
>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.


 
Reply With Quote
 
Salt_Peter
Guest
Posts: n/a
 
      11-29-2006

asdf wrote:
> 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.

 
Reply With Quote
 
James Willmott
Guest
Posts: n/a
 
      11-29-2006
asdf wrote:

> 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?
 
Reply With Quote
 
benben
Guest
Posts: n/a
 
      11-29-2006
>
> class A{
> int x;
> public:
> int get() { return x; };


Better still, make it

int get() const {return x;}

> };
>
> Is this what you meant?

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Simple program with a simple(?) error -- what's wrong? RichardOnRails Ruby 3 07-21-2008 01:26 PM
Simple Code? What's Wrong? Peter van der Goes Java 4 07-14-2005 07:15 PM
simple iframe, calling with innerHTML - what am i doing wrong here? Ragnorack67@hotmail.com HTML 9 03-04-2005 07:27 AM
Is XML Doc wrong or is Schema wrong? (or both) Matthew XML 7 01-07-2005 10:05 PM
Simple SQL question. Can you check what am i doing wrong? Thank You. Miguel Dias Moura ASP .Net 1 06-18-2004 09:33 AM



Advertisments