views:

82

answers:

4

Hi I'm working on a project about Data structures. In the first , I wrote everything in main but it sounds like C . But as I learned, I tried to thinkk OOP and do as little as possible in my main() methods.

I've implemented some opertation in my class like add,delet,find.it's too easy to implement its .

class ARB
{
        private:
                struct BT
                {
                        int data;
                        BT *l;
                        BT *r;
                };
                struct BT *p;
       public
                ARB();
                ~ARB();
                void del(int n);
                void add(int n); 
};

    void ARB::del(int num)
{
//The code ,don't care about it 

  };  

main()
{
// 
    BTR T;
    T.add(3);
    T.add(5);

};

But I arrived to the big program How can I define a methode which have to use a binary tree and to get a stack

STACK ARB::MyFunct(BT* p)
{
// The code don't care about it 
}

How can I apply it in the main programme

main()
{
// 
    BT T;
    T.add(3);
    T.add(5);
    STACK S;
    BT* p
    S=T.MyFunct(p); // error C2664 cannot convert parametre 1

};

**mention :I implement STACK class

A: 

That looks like it could be the STACK classes 'copy assign operator' is not correctly defined, eg:

class STACK {
public:
    ...
    STACK& operator=(STACK& right); // copy assign
    ...
};

in this case, the copy constructor is requiring that it can modify right, but the STACK returned from ARB::MyFunct() is a temporary, and cannot be modified. Try changing the copy constructor to STACK(STACK const& right), so C++ knows you will not modify it. If you are using a compiler supporting R-value references (eg, Visual Studio 2010), then you can define a 'move constructor' as well as the copy constructor:

class STACK {
public:
    STACK& operator=(STACK const& right); // copy assign: note 'const'
    STACK& operator=(STACK&& right); // move assign: note no 'const', double '&'.
    ...
};

This will only be called with temporary values, which it is allowed to modify.

The same rules apply to constructors:

class STACK {
public:
    STACK();
    STACK(STACK const& right); // copy construct
    STACK(STACK&& right); // move construct
    STACK& operator=(STACK const& right); // copy assign
    STACK& operator=(STACK&& right); // move assign
    ...
};

int main() {
    STACK S = T.MyFunct(p); // move construct S (right side is temporary)
    S = T.MyFunct(p); // move assign S (right side is temporary)

    STACK K = S; // copy construct K (right side is not temporary)
    K = S; // copy assign K (right side is not temporary)
}
Simon Buchan
A: 

I assume you are trying to traverse the tree and build a stack out of it? I don't understand why you're passing in p. From your example, it looks like you've simply created a pointer to a BT object and then you're trying to pass it in.

It seems like this would make more sense:

S=T.MyFunct();

Couldn't you use this to build your tree? I guess I'm not sure what it is you're trying to do.

Assuming that you're trying to implement a function for the BT object that converts the BT into a stack, then you don't really need to pass anything in (because MyFunct is a member function of BT). You already have access to the tree within your member function, so all you need to do is traverse the tree and build a stack.

Note: My C++ is pretty rusty.

Vivin Paliath
Absolutly , yes I'd like to do something like that .I'm trying to implement what you said,Should I use This to convert Look to this function http://i41.tinypic.com/25qrzbs.png what should I modify !My C++ is pretty rusty;Idon't think that
T_Geek
A: 
why do you assume `ARB::MyFunct()` is static? Also, while he should be initializing `p`, it would not cause a compile error.
Simon Buchan
A: 

There's a few issues here. For one thing, add() is a member function of ARB, not BT. And, BT is a private subclass of ARB, so it can't be accessed from main(). p is a private member of ARB (as it should be), but it should really be a direct variable, not a pointer, so it will be automatically created and destroyed with ARB. As is, p is never initialized and there's no way to do so from outside ARB.

I'm guessing here that ARB uses its internal BT p for internal storage, so the implementations of add() and del() both operate on p, and that MyFunct() is supposed to take that BT and generate a stack from it. If so, MyFunct() should take no parameters and simply reference p directly.

So main() would look something like:

ARB arb;
arb.add(3)
arb.add(5)
STACK s = arb.myFunct(); // which should maybe be makeStack() or such

This is all assuming I've correctly deduced your intentions here.

ceo
Thank you for your answer Absolutly , yes I'd like to do something like that . I'm trying to implement what you said,Should I use This to convert Look to this function i41.tinypic.com/25qrzbs.png what should I modify !
T_Geek
Should I use "This"
T_Geek
You don't need to use "this". Simply take out myfunction()'s p parameter, so that the declaration looks like LLC ARB::myfunction(). p is already a member variable, so you can simply use it in member functions without passing it in.
ceo