Mojie11
Mojie11

Reputation: 105

How can I solve the error 'expected expression'?

The C++ code that gets an error is below. My g++ version is clang version 12.0.0 (clang-1200.0.32.27)

(The code is written by others many years ago, and may because of the version updates of the g++, I can not run it successfully now. )

typedef struct Cond{
  int offset1; 
  bool (*comparator) (void * , void *, AttrType, int); 
  bool isValue;
  void* data; 
  int offset2; 
  int length; 
  int length2; 
  AttrType type; 
} Cond;

Cond *condList;

// Malloc the list of conditions to be met
condList = (Cond *)malloc(numConds * sizeof(Cond));
for(int i= 0; i < numConds; i++){
  condList[i] = {0, NULL, true, NULL, 0, 0, INT};
}

Compiler return an error in the line condList[i] = {0, NULL, true, NULL, 0, 0, INT} ,

ql_nodejoin.cc:78:19: error: expected expression
    condList[i] = {0, NULL, true, NULL, 0, 0, INT};
                  ^

How can I solve this?

Upvotes: 0

Views: 33503

Answers (2)

Mojie11
Mojie11

Reputation: 105

I solved this error by change the line

condList[i] = {0, NULL, true, NULL, 0, 0, INT};

to

Cond c = {0, NULL, true, NULL, 0, 0, INT};
condList[i] = c;

A small change. I think the type declaration is required.

Upvotes: 3

tadman
tadman

Reputation: 211580

The quick fix is to add -std=c++17 to support this C++ feature.

The actual fix is to use C++ more effectively, like employing a std::vector plus using emplace_back to create entries as necessary:

// Malloc the list of conditions to be met
std::vector<Cond> condList;

for (int i= 0; i < numConds; ++i) {
  condList.emplace_back(
    0, // int offset1
    nullptr, // bool (*comparator) (void * , void *, AttrType, int); 
    true, // bool isValue;
    nullptr, // void* data; 
    0, // int offset2; 
    0, // int length; 
    0, // int length2; 
    INT // AttrType type; 
  );
}

It behaves a lot like a regular array, you can still condList[i] and whatever.

This would be a lot easier with a default constructor:

struct Cond {
  Cond() : offset1(0), comparator(nullptr), offset2(0), length(0), length2(0), type(INT) { };

  // ... (properties) ...
}

Where now you can just emplace_back() with nothing which sets defaults, or even easier, just pre-size the vector:

std::vector<Cond> condList(numConds);

Note: typedef isn't necessary in C++ as it is in C, as struct is not required.

Upvotes: 5

Related Questions