mardi 24 novembre 2020

Getting "Incomplete Type" error when trying to create a new object in cpp file

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

Aucun commentaire:

Enregistrer un commentaire