Home c++ Error: expected initializer before 'PointType'

Error: expected initializer before ‘PointType’

Author

Date

Category

In this piece of code:

PointType * createPoint (int x, int y, int value) {
  PointType * vertex = (PointType *) malloc (sizeof (PointType));
  vertex - & gt; x = x;
  vertex - & gt; y = y;
  vertex - & gt; value = value;
  return vertex;
}

An error occurs:

main.cpp: 29: 1: error: expected initializer before 'PointType'
PointType * createPoint (int x, int y, int value) {
^

The structure itself looks like this:

typedef struct Point PointType;
struct Point {
  int x;
  int y;
  int value;
};

What could be the problem?


Answer 1, authority 100%

This error is usually caused by a missing semicolon.

Look at the piece of code just before the problem line. If it is preceded by #include , look at the end of the header (#include stupidly includes the header text at the point where it is mentioned).

This piece of code is a class or structure definition, do not forget that in C++ it must also end with a semicolon. The same goes for pre-declarations of functions and globals, but in such places, mistakes are usually less common.

Example:

(x.h)

# ifndef X_H
#define X_H
double variable // & lt; - missing semicolon here
#endif

(x.cpp)

# include "x.h"
PointType * createPoint (int x, int y, int value) {// & lt; - error will be reported here
  // ...

Proof: http://ideone.com/KHT4pz

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions