S
S
Sergey Chistyakov2014-11-09 01:30:06
Qt
Sergey Chistyakov, 2014-11-09 01:30:06

Default parameters in a friend function. Why is he cursing?

I am writing a "graph" class and a "breadth-first search" friend function. In the function, in the "your graph" parameter, I pass a default constructor that creates a graph from 1 vertex. The following code will tell for me:

/**********
 * граф содержит:
 * количество точек
 * вектор связности
 **********/
class Graph {
    int vs;
    std::vector<int> **relations;
public:
    Graph(int _vs = 1){
        vs = _vs;
        relations = new std::vector<int>*[_vs];
        for(int i = 0; i < _vs; i++) relations[i] = new std::vector<int>;
    }
    Graph &bind(int from, int to);
    // печать в стандартный поток
    friend std::ostream & operator <<(std::ostream &out, Graph &g);
    // поиск в ширину
    friend int **bfs(Graph g = Graph::Graph());
};

Question: why does the compiler swear at my default parameter? After all, I can shove the function call into the default parameters, why then is the trouble with the constructor?
UPD: answer 1 , answer 2

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2014-11-09
@jcmvbkbc

why does the compiler swear at my default parameter?

From the fact that you do not need to call the constructor as a function. Just
write friend int **bfs(Graph g = Graph());

M
Misha Krinkin, 2014-11-09
@kmu1990

I would venture to suggest that he wants to see just Graph() instead of Graph::Graph(). And a little offtopic, I may not understand something, but why the graph is passed as a default parameter to the width traversal, it's a little strange to bypass the width of the default graph (I assume an empty graph).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question