I'm working on a project about Data structures. In the first , I wrote everythin开发者_Python百科g 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
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.
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.
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)
}
First off, it might help if line 5 and 6 are changed.
/* CODE : inside main
1 BT T;
2 T.add(3);
3 T.add(5);
4 STACK S;
5 BT* p
6 S=T.MyFunct(p); // error C2664 cannot convert parametre 1
*/
Line 5:
BT *p; -> BT *p = &T;
Line 6:
S=T.MyFunct(p) -> S=ARB::MyFunct(p);
Now assuming that MyFunct does what its suppose to do, it should work. The main problem with the program was that the pointer p wasn't initialized in effect reducing the parameter to void. Also, I'm assuming that MyFunct has no object relation, so it doesn't make a lot of sense to reference T in this case. That is why I would suggest instead the later version.
精彩评论