0

This is my first time posting, so sorry if there's any issues with my formatting or wording.

As the title says, I've just been getting started working on an assignment and found an issue when trying to create an object instance of a class. I'm trying to create the object in a .cpp file that I'll be using for testing. The error is:

incomplete type not allowed

There are three files: Process.h, Process.cpp, and test.cpp. The code for each is below.

Process.h

#include <iostream>
#include <string>

using namespace std;

class Process {

private:

    int ProcessID;
    string ProcessState;


public:

    //constuctor
    Process(int, string);

    //mutator functions
    void SetProcessID(int);
    void SetProcessState(string);

    //accessor functions
    int GetProcessID();
    string GetProcessState();

};

Process.cpp

#include <iostream>
#include "Process.h"

//constructor
Process::Process(int Id, string State)
{
    SetProcessID(Id);
    SetProcessState(State);
}

//mutator
void Process::SetProcessID(int Id)
{
    ProcessID = Id;
    return;
}

//mutator
void Process::SetProcessState(string State)
{
    ProcessState = State;
    return;
}

//accessor
int Process::GetProcessID()
{
    return ProcessID;
}

//accessor
string Process::GetProcessState()
{
    return ProcessState;
}

Test.cpp

#include <iostream>
#include "Process.h"

using namespace std;

class Process;

int main()
{
    Process Process1(1, "Ready"); //Error here on this line

    cout << "Process 1 created" << endl;

}

I have tried to replace the error line in the test.cpp with this:

Process* Process1(1, "Ready");

or

Process Process1 = new Process(1, "Ready");

and variations of these. As well as trying to switch around the includes. But to no avail.

I'm working in Visual Studio 2019, and it seems that the test.cpp is aware of the Process class, but something (the constructor, maybe?) is out of its scope for some reason.

Running the test.cpp code in the Process.cpp file works as intended, but I need to be able to call it in other files.

The exact error, as seen is Visual Studio 2019, is E0070: incomplete type is not allowed

Codiac500
  • 21
  • 4
  • Not sure if this is your problem, but you should remove `class Process;` from `Test.cpp`. #including `Process.h` should take care of that. – Paul Sanders Nov 25 '20 at 00:08
  • 2
    Why do you put a forward declaration `class Process;` in main, when you already `#include "Process.h"`? – πάντα ῥεῖ Nov 25 '20 at 00:09
  • Reproduce, I cannot. – user4581301 Nov 25 '20 at 00:15
  • Ensure that you only have one `Process.h` file, that it has been saved, and that it contains what you think it contains. – Asteroids With Wings Nov 25 '20 at 00:15
  • Side note: Are you really sure you want to be able to change the process id of a Process after construction? Seems dodgy. – user4581301 Nov 25 '20 at 00:16
  • Removing ```class Process;``` from the ```Test.cpp``` file does not work. The process is then seen as an undefined identifier in the ```Test.cpp``` file. It requires the class declaration line here so far as I understand since these are separate files. – Codiac500 Nov 25 '20 at 00:23
  • No worries about the process ID changing after production! This is just gonna be used to show some rough simulated output of process states changing like an OS's dispatcher would have them do. The process class is just for show and won't be doing anything important. – Codiac500 Nov 25 '20 at 00:25
  • 3
    @Codiac500 removing `class Process;` in `test.cpp` should work just fine, since `Process` is already defined in `Process.h` which `test.cpp` includes. However, do note that `using namespace std;` in a header file is a very bad idea (it is a [bad idea to use it - period](https://stackoverflow.com/questions/1452721/)), and `Process.h` is missing a header guard. – Remy Lebeau Nov 25 '20 at 00:36
  • In that case, consider removing `SetProcessID`. That method is just begging someone to use it and see what happens. If you don't want people to do something, don't make it easy for them to do it. In general, don't provide a setter unless users should be allowed to modify object state, and strongly consider including validity checking in any setter you do provide. If you have a `public` setter that does nothing and always allows the member's value to be set, you have a `public` member in all but name. – user4581301 Nov 25 '20 at 00:36
  • @user4581301 I understand. No one will be using this code besides me though, and it will only be for testing and getting some desired output, and I want access to all the member variables at this time. Thank you. – Codiac500 Nov 25 '20 at 00:41
  • @RemyLebeau When I attempt to remove the ```class Process;``` line, I get an error for "identifier 'Process' is undefined", even though I have the header file included. Is the missing header guard causing this issue? And I understand about ```using namespace std;``` though it is just for convenience here as this isn't meant to be especially clean code. Would it be causing the issue? – Codiac500 Nov 25 '20 at 00:45
  • 1
    There is also a chance that your build system is including the old DOS process.h header because it's earlier in the search list for includes or something insane like that. – user4581301 Nov 25 '20 at 00:45
  • Here's a quick test: See if you can call `getpid();` from test.cpp. – user4581301 Nov 25 '20 at 00:48
  • 2
    Or better, simply rename `Process.cpp`/`Process.h` to something more unique that won't conflict with other build files – Remy Lebeau Nov 25 '20 at 00:49
  • @user4581301 It threw the error ```'getpid': identifier not found```It seems like that may be in the file though, wouldn't it differentiate that from my "Process.h"? – Codiac500 Nov 25 '20 at 00:53
  • Hard to say Take Remy's advice and rename `Process.h` to something like Codiac500Process.h, and include that instead. If that solves the problem, then yup. There's another process.h lurking somewhere tripping you up. – user4581301 Nov 25 '20 at 00:57
  • 1
    If you have a reasonably advanced IDE, you might be able to look up exactly what header's really being used. – user4581301 Nov 25 '20 at 00:58
  • You are both correct. I was able to find that it is indeed going to process.h somewhere else. Thank you for your help! – Codiac500 Nov 25 '20 at 01:04

1 Answers1

1

The issue was that another header file with the same name was being used by my test.cpp file. "Process.h" was not differentiated from "process.h", which was also apparently somewhere up the file hierarchy. Changing the name of the Process class I had created solved the issue. To be precise, the file included rather than mine was located at C:\Program Files (x86)\Windows Kits\10\Include\10.0.17763.0\ucrt\process.h

In Visual Studio 2019, simply right clicking my #include "Process.h" line and then go to document "Process.h" allowed me to quickly see that the file navigated to was not the one I had created. Thanks again to all who helped me find this issue!

Codiac500
  • 21
  • 4