2

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
};
Ryuzaki
  • 41
  • 6
  • 3
    "most vexing parse" fix is `edge minIncoming{ nullptr, NOPATH };` - live - https://godbolt.org/z/zn7qGEKvx – Richard Critten Mar 04 '22 at 09:47
  • Thanks for the info. I didn't know what MVP was but now I understand. Could you add this comment as an answer so I could approve it or nah? – Ryuzaki Mar 04 '22 at 09:56
  • Related: [What is the purpose of the Most Vexing Parse?](https://stackoverflow.com/questions/14077608/what-is-the-purpose-of-the-most-vexing-parse) or [Default constructor with empty brackets](https://stackoverflow.com/questions/180172/default-constructor-with-empty-brackets/181463#181463) – Ted Lyngmo Mar 04 '22 at 10:00

1 Answers1

1

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.

Jason
  • 36,170
  • 5
  • 26
  • 60