Using method in friend operator

Hello, I have problem with code like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class X;

class Y{
    ...
    char type; //type of 'node', may contains data or may be a 'node'
    void asd(X& object);
    ...
}

void Y::asd(X& object){
    ++object.x;
}

friend QDataStream & operator >>(QDataStream & out, Y & s){
    ...
    Y cache;
    out >> cache;
    if( cache.type == 'node' )
        s.asd( ? ); //It's possible to use it here? How?
    ...
}

class X{
    friend class Y;
public:
    ...
    int x;
    std::vector<Y> wektor;
    ...
}


I try to read some binary file, which contains blocks of data with the same structure (class Y), but some of them are something like 'nodes'. Whole structure is similar to tree, but i don't know how many elements is (not specified in file).
I try to get information about type of 'node' while reading to cache and it's working, but i don't know how to use asd() method here.
It's possible to use it here?

Thanks for any help (and sorry for my english).
Last edited on
Yes it's possible. You'd do it exactly like you are:

 
s.asd( your_x_object_here );


The only thing is you need an X object with which to call it.
Ok, i have this: (propably a lot of errors)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <vector>

class X {
    friend class Y;
public:

    X() {
        wsk = &X();
    }
    X *wsk;
    int x;
    std::vector<Y(*wsk) > wektor;

};

class Y {
public:

    Y(X &ref) {
        wsk = &ref;
    }
    X *wsk;
    char type;
    void asd(X& object);

    friend std::istream & operator >>(std::istream & out, Y & s) {
        Y cache(*s.wsk);
        out >> cache;
        if (cache.type == 0x01)
            s.asd(*s.wsk);
    }
};

friend std::istream & operator >>(std::istream & out, Y & s) {

    Y cache(*s.wsk);
    out >> cache;
    if (cache.type == 0x01)
        s.asd(*s.wsk);

}

void Y::asd(X& object) {
    ++object.x;
};

int main() {
    X obj;
    return 0;
}


and errors:
main.cpp:13: error: ‘X::wsk’ cannot appear in a constant-expression
main.cpp:13: error: ‘*’ cannot appear in a constant-expression
main.cpp:13: error: a function call cannot appear in a constant-expression
main.cpp:13: error: template argument 1 is invalid
main.cpp:13: error: template argument 2 is invalid

What is wrong in this line?
Last edited on
Um, what do you want the vector to be of? Simply put that type in the <>s. For example:
vector<int> for a vector of ints
vector<Y> for a vector of your Y class
This is bad for many reasons:
1
2
3
    X() {
        wsk = &X();
    }


This is infinite recursion. It will make your program collapse.

If X() calls X(), then X() will call X() which calls X() which calls X(), etc, etc, etc. forever and ever until you run out of stack space and the program dies.

But even if that worked, X() returns a temporary object. wsk may point to it, but as soon as the next line of code runs, the temporary object is destroyed and wsk becomes a bad pointer.

Why would you need to do this anyway?



 
std::vector<Y(*wsk) > wektor;


You're trying to mix a constructor with a type definition. This doesn't work. You have to make this as std::vector<Y>, then when you add Ys to the vector, you construct them appropriately.



Your classes are somewhat of a giant mess. You should really rethink this design.

What exactly are you trying to accomplish, here?
Topic archived. No new replies allowed.