1

Sorry for asking such a beginner question, but I'm stuck writing header files. For information, there aren't StackOverflow articles relating to the problem I have -- it seems like I've got a very simple setting wrong but couldn't find it on the internet.

Error message:

insertion/insertion.h:16:24: error: expected unqualified-id before ')' token   
   16 |         void insertion();

insertion.cpp:

#include <insertion.h>
// more imports

void insertion() {
    // implementation not shown
}

int main() {
    insertion();
    return 0;
}

insertion.h:

#include <iostream>
// exactly the same imports as the file above but didn't import itself

#ifndef insertion
#define insertion

class Insertion {
    public:
        void insertion();
};

#endif 

execute.cpp:

#include "insertion/insertion.h"
using namespace std;

int main() {
    // still writing
}

The file structure looks like this:

|- execute.cpp
|- insertion\
|---- insertion.cpp
|---- insertion.h

Any help is greatly appreciated.

crimsonpython24
  • 2,223
  • 2
  • 11
  • 27
  • 6
    `#define insertion` - what do you think that does to `void insertion();` in your class decl ? Result: `void ();` . Pick a proper include-guard name (and unrelated, headers included in headers should be fenced into the same include guards). – WhozCraig Mar 26 '21 at 12:49
  • 1
    ot: the includes should be inside the header guards, `void insertion() {...}` is not a definition for `void Insertion::insertion()` and there can be only one `main`, though none of this is directly related to the error message – 463035818_is_not_an_ai Mar 26 '21 at 12:49
  • If you mean for `insertion()` to be a class constructor, then note it needs the same capitization as the class name. – acraig5075 Mar 26 '21 at 12:51

3 Answers3

4

#define insertion colides with void insertion();.

Change

#ifndef insertion
#define insertion

to

#ifndef _insertion_h_inc_
#define _insertion_h_inc_

Or better: change

#include <iostream>
// exactly the same imports as the file above but didn't import itself

#ifndef insertion
#define insertion

class Insertion {
    public:
        void insertion();
};

#endif 

to

#ifndef _insertion_h_inc_
#define _insertion_h_inc_

#include <iostream>
// exactly the same imports as the file above but didn't import itself

class Insertion {
    public:
        void insertion();
};

#endif 
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Jabberwocky
  • 48,281
  • 17
  • 65
  • 115
2

The line

#define insertion

is the cause of your problems.

insertion is replaced by an empty token in all the places after that.


class Insertion {
    public:
        void insertion();
};

becomes

class Insertion {
    public:
        void ();
};

and

void insertion() {
    // implementation not shown
}

int main() {
    insertion();
    return 0;
}

becomes

void () {
    // implementation not shown
}

int main() {
    ();
    return 0;
}

Now you can see why your program fails to compile.

Change

#ifndef insertion
#define insertion

to

#ifndef insertion_h
#define insertion_h

That should resolve your problems.


Better still, use #pragma once. Most modern compilers support it.

#pragma once

#include <iostream>

class Insertion {
    public:
        void insertion();
};
R Sahu
  • 204,454
  • 14
  • 159
  • 270
1

I always do this on my codes

insertion.h

#pragma once
#ifndef insertion_h
#define insertion_h
//some codes goes here
#endif

in my main.cpp file

#include "insertion.h"

and you're good to go, hope this helps.

Regards,

Joey

EDIT: if you want to learn more about pragma you may refer to this link here more on pragma's

Joey
  • 53
  • 1
  • 9