5

I've set up my programs and header files as what I think is the correct way, but I keep getting the error mentioned in the title.

I have tried searching for fixes for this issue, which most of them were simply adding ';' after the class definition in a header file. I've tried most all the fixes I could find with the same results.

Here is the main program where the error is flagging:

#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include "computeGross.h"
#include "computeInsurance.h"
#include "Employee.h" /*<-----------------This is where the error flags*/
using namespace std;

int main()
{ }

And here is the header file that the error is flagging:

#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using namespace std;

struct Employee
{
    string name;
    double rate;
    double hours;
    double insurance;
    double social;
    double stateTax;
    double fedTax;
    double netPay;
    double grossPay;
};

#endif
drescherjm
  • 10,365
  • 5
  • 44
  • 64
Stanky-kun
  • 71
  • 1
  • 1
  • 6
  • 2
    I assume you are on some version of Visual Studio. Never include `pch.h` in a header. Always put `#include "pch.h"` as the first line of every one of your .cpp files – drescherjm Jul 15 '19 at 19:56
  • 3
    Unrelated to your problem, but never use `using namespace std;` in a header file. Because what if the user of your header *has conflicting definitions*!? – Zan Lynx Jul 15 '19 at 19:57
  • 1
    Also your error could be in one of your "compute" headers and you only see the error message in employee because it is the last one. – Zan Lynx Jul 15 '19 at 19:58
  • Other than the error message I don't see any usage of precompiled headers in the presented files. – drescherjm Jul 15 '19 at 19:59
  • @drescherjm It might be in the Makefile or whatever he is using. I've seen some for GCC / clang which use `-include pch.hpp` and have rules to automatically collect header file dependencies into `pch.hpp` – Zan Lynx Jul 15 '19 at 20:01
  • @drescherjm I am using VS Community 2019, and have added the VS tag. – Stanky-kun Jul 15 '19 at 20:10
  • My guess is that `computeGross.h` or `computeInsurance.h` improperly includes `pch.h` – drescherjm Jul 15 '19 at 20:19
  • `using namespace std;` in a header is not illegal but a very bad practice. However I don't see anything at all wrong with the code that is in the question. – drescherjm Jul 15 '19 at 20:21
  • Your endif in your real file does not have those backticks on it, does it? It should not. – Zan Lynx Jul 15 '19 at 20:26
  • @ZanLynx no, that was a mistake while formatting my question. – Stanky-kun Jul 15 '19 at 20:31
  • 1
    @drescherjm I have not included pch.h anywhere in my files, also, I'm only using namespace std due to errors thrown when I tried 'std::string' in my 'Employee' class. – Stanky-kun Jul 15 '19 at 20:31
  • So precompiled headers are not enabled in your project at all? – drescherjm Jul 15 '19 at 20:32
  • To form a code block in the editor copy the text. Select it and press the {} button. I fixed the code blocks. – drescherjm Jul 15 '19 at 20:33
  • @drescherjm no, the only headers in use are the ones included in my main. – Stanky-kun Jul 15 '19 at 20:34
  • 1
    I expect that this may then be a weird false positive from the `Intellisense`. It does not always get its suggestions correct. – drescherjm Jul 15 '19 at 20:35

2 Answers2

15

FYI, I encountered this when I left a ; off of a declaration in another .h file. Since that declaration was never finished, when the #define was encountered in the next .h file it threw this error.

jshep321
  • 553
  • 5
  • 8
3

Try changing the order of your header files. Try making a new cpp file and include the headers one at a time. See which one breaks.

Reduce the possible causes of the error until you find it.

Zan Lynx
  • 53,022
  • 10
  • 79
  • 131
  • I made a new cpp and there were no issues until I added the 'Employee' header. – Stanky-kun Jul 15 '19 at 20:17
  • @Stanky-kun Keep reducing it. Make the Employee struct empty. Remove the ifndef,define,endif bits. Etc. – Zan Lynx Jul 15 '19 at 20:27
  • 2
    I included Employee first before my other headers and the problem resolved. I assume since the other headers were also including employee, it needed to be included first in my main. Thanks. – Stanky-kun Jul 15 '19 at 20:40