Variable is undeclared, but why?

Discussion in 'C++' started by mailforpr@googlemail.com, Nov 27, 2006.

  1. Guest

    Here's an examble:

    template<class C> struct S
    {
    C c;
    };

    template<class C> struct D
    :public S<C>
    {

    void f(C temp)
    {
    c=temp;
    }
    };

    int main()
    {
    D<int> i;
    i.f(2);

    return 0;
    }


    Error message (compiled with g++ 3.4):
    In member function `void D<C>::f(C)':
    error: `c' undeclared (first use this function)

    D is a S, so D should have public access to c. But it hasn't. Does
    someone know why that is so?
    , Nov 27, 2006
    #1
    1. Advertising

  2. Guest

    Okay, it does have access to c. I modified the code a bit:

    void f(C temp)
    {
    S<C>::c=temp;
    }

    Is there any other way to access the member of a base class/struct?
    Writing S<C>:: before each member is annoying.
    , Nov 27, 2006
    #2
    1. Advertising

  3. flopbucket Guest

    wrote:
    > Okay, it does have access to c. I modified the code a bit:
    >
    > void f(C temp)
    > {
    > S<C>::c=temp;
    > }
    >
    > Is there any other way to access the member of a base class/struct?
    > Writing S<C>:: before each member is annoying.


    Well, you can use this->
    flopbucket, Nov 27, 2006
    #3
  4. flopbucket wrote:
    > wrote:
    >> Okay, it does have access to c. I modified the code a bit:
    >>
    >> void f(C temp)
    >> {
    >> S<C>::c=temp;
    >> }
    >>
    >> Is there any other way to access the member of a base class/struct?
    >> Writing S<C>:: before each member is annoying.

    >
    > Well, you can use this->


    You could also use 'using':

    template<class C> class B {
    protected:
    C c;
    };

    template<class C> class D : public B<C> {
    using B<C>::c; /// AHA!
    public:
    void foo(C cc) {
    c = cc;
    }
    };

    int main() {
    D<int> di;
    di.foo(42);
    }

    V
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Nov 27, 2006
    #4
  5. Noah Roberts Guest

    Noah Roberts, Nov 27, 2006
    #5
    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. Brad Smallridge

    undeclared loop variable

    Brad Smallridge, Mar 23, 2005, in forum: VHDL
    Replies:
    7
    Views:
    681
    Michael
    Mar 25, 2005
  2. W. Van Hooste

    FILE undeclared - but its not, according to me.

    W. Van Hooste, Sep 16, 2003, in forum: C Programming
    Replies:
    9
    Views:
    4,971
    Dave Thompson
    Sep 22, 2003
  3. yttrium
    Replies:
    12
    Views:
    1,368
    Jonathan Bromley
    Jan 5, 2007
  4. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,804
    Smokey Grindel
    Dec 2, 2006
  5. Kensai
    Replies:
    6
    Views:
    740
    Victor Bazarov
    Dec 18, 2009
Loading...

Share This Page