what is wrong with this simple code?

Discussion in 'C++' started by asdf, Nov 29, 2006.

  1. asdf

    asdf Guest

    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?
    asdf, Nov 29, 2006
    #1
    1. Advertising

  2. asdf

    Guest

    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?
    , Nov 29, 2006
    #2
    1. Advertising

  3. 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
    Thomas Tutone, Nov 29, 2006
    #3
  4. asdf

    David Harmon Guest

    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.
    David Harmon, Nov 29, 2006
    #4
  5. asdf

    Salt_Peter Guest

    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.
    Salt_Peter, Nov 29, 2006
    #5
  6. 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?
    James Willmott, Nov 29, 2006
    #6
  7. asdf

    benben Guest

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


    Better still, make it

    int get() const {return x;}

    > };
    >
    > Is this what you meant?
    benben, Nov 29, 2006
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Peter van der Goes

    Simple Code? What's Wrong?

    Peter van der Goes, Jul 14, 2005, in forum: Java
    Replies:
    4
    Views:
    372
    Mike Schilling
    Jul 14, 2005
  2. learningGuy
    Replies:
    1
    Views:
    588
    Mike Wahler
    Sep 26, 2003
  3. Sam
    Replies:
    20
    Views:
    749
    Martijn Lievaart
    Jan 9, 2004
  4. Robert Smith

    whats wrong with this simple code?

    Robert Smith, Apr 27, 2004, in forum: C++
    Replies:
    2
    Views:
    329
    Christopher Benson-Manica
    Apr 28, 2004
  5. RichardOnRails
    Replies:
    3
    Views:
    132
    RichardOnRails
    Jul 21, 2008
Loading...

Share This Page