The below code snippet is generating the error: expected identifier before 'nullptr'
as well as error: expected ',' or '...' before 'nullptr'
in line edge minIncoming(nullptr, NOPATH);
Any idea what is wrong? All I want to do is use the constructor to initialize minIncoming. I tried searching but couldn't find a answer. I apologize in advance if question is too basic.
#include <vector>
#include <unordered_map>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <queue>
#define MAXN 8
#define NOPATH 1000000
#define DEBUG
using namespace std;
struct vertex;
struct edge
{
vertex* node;
int weight;
edge(vertex* n, int w) : node(n), weight(w) {}
};
struct vertex
{
vector< edge > outgoing;
edge minIncoming(nullptr, NOPATH);
bool visited;
#ifdef DEBUG
int id;
#endif // DEBUG
};
CodePudding user response:
Due to C 's most vexing parse, the statement:
edge minIncoming(nullptr, NOPATH);
is treated as if you're declaring a function named minIncoming
with return type of edge
and taking two parameters. But since during declaration of a function, we specify the types of the parameters instead of specifying the arguments(as you did in your program), the program gives the mentinoed error.
To solve this replace that statement with:
edge minIncoming{ nullptr, NOPATH };
Now the above statement defines an object(data member) named minIncoming
of type edge
while passing the two initializers.