0

I have two classes using each other.

Basically, I have an helper class and an head class (I'll call it like that, head uses helper, but helper access members from head).

So it looks like that :

class CHead;

class CHelper
{
    public:
       Chelper() : m_head(0) {}; // default constructor
       CHelper(CHead *head) : m_head(head) {};

       SomeFunction(int id, int type = m_head->m_vTypes[id]); // ERROR HERE

    private:
       CHead *m_head;

       [...]
       bla bla
};


class CHead {
    friend class CHelper; // CHelper can access CHead members

    public:
         CHead(bla bla) : bla bla { bla bla };

         // Member m_helper is constructed at constructor end with smtn
         // like m_helper = CHelper(this);

    private:
         CHelper m_helper;

         [...]
         bla bla
}

Well, I am getting two errors I don't understand :

First, when trying to do m_head->m_vTypes[] I get :

A non static member reference must be relative to a specific object

Second, I get

identifier "id" is undefined

I don't get these errors. The first should pass without a problem no? Since I declared CHelper a friend of CHead. The second makes me angry. "id" is declared right before as first argument...

Anyone can help?

Yannick
  • 830
  • 7
  • 27

1 Answers1

0

The first error is because default arguments have to be known at compile-time, not at run-time. Therefore you can't have any kind of non-literal expression as default argument.

The second error is because id have no type.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • For second one, it was a retyping mistake. Yes there is a type and the compiler still doesn't figure it even with the type :( – Yannick Jul 02 '14 at 02:30
  • For the first one, do you have a solution? I'm not sure I am understanding everything. Is it just the fact it is a default arg that makes everything fail miserably? So what I'm trying to achieve is still possible to do no? (if i dont use default arguments i mean) – Yannick Jul 02 '14 at 02:31
  • 1
    You can have two declarations of `SomeFunction` one that takes two parameters (with no defaults) and one that just takes one parameter (the id). The second function can then call the first passing a second parameter of `m_head->m_vTypes[id]` as that can be calculated as runtime. – The Dark Jul 02 '14 at 02:40