0

I am just trying to create an iterator for a list of class pointers. I searched around for a while and could not find anything regarding this issue. Everything works fine until I try and create the iterator.

Here is the compilation error message I'm getting:

ProcessList.cpp:28:7: error: 'Process' does not refer to a value
            list<Process*>::iterator i;
                 ^
    ./Process.h:3:7: note: declared here
    class Process
          ^
    ProcessList.cpp:28:15: error: expected expression
            list<Process*>::iterator i;
                         ^
    ProcessList.cpp:28:18: error: cannot refer to class template 'iterator' without a template argument list
            list<Process*>::iterator i;
                          ~~^
    /Library/Developer/CommandLineTools/usr/bin/../include/c++/v1/iterator:431:30: note: template is declared here
    struct _LIBCPP_TYPE_VIS_ONLY iterator
                                 ^
    3 errors generated.

Here is my code. The error comes from the ProcessList.cpp code at the bottom:

//main.cpp
#include <iostream>
#include <list>
#include <random>
#include "ProcessList.h"
#include "Generator.h"
using namespace std;


int main()
{

    ProcessList processList(2);

    processList.getProcess(processList.getProcessList());

    return 0;
}


//Process.h
class Process
{
    public: 
        Process();

        void setPID(int PID);
        int getPID();

        int calculateNumberOfCycles();
        int getNumberOfCycles();

        int calculateMemorySize();
        int getMemorySize();

    private:
        static int number_of_processes;
        int PID;
        int number_of_cycles;
        int memory_size;


};


//Process.cpp
#include <iostream>
#include "Process.h"
using namespace std;

int Process::number_of_processes = 0;//define static variable

//Constructor
Process::Process()
{
    cout << "Process Constructor" << endl;

    PID = number_of_processes;
    number_of_cycles = number_of_processes;
    memory_size = number_of_processes;
    number_of_processes++;

    //number_of_cycles = setNumberOfCycles;
    //memory_size = setMemorySize;

}

//PID Setter
void Process::setPID(int PID)
{

}

//PID Getter
int Process::getPID()
{
    return PID;
}

//Generates a random number of cycles based od the defined variables
int Process::calculateNumberOfCycles()
{
    return 0;
}

//Returns the number of cycles of the process
int Process::getNumberOfCycles()
{
    return number_of_cycles;
}

//Generates a random amount of memory based on the defined variables
int Process::calculateMemorySize()
{
    return 0;
}

//Returns the amount of memory for the process
int Process::getMemorySize()
{
    return memory_size;
}


//ProcessList.h
#include <list>
#include "Process.h"
 using namespace std;

class ProcessList
{
    private:
        list<Process*> processList;

    public:
        ProcessList(int number_of_processes_to_create);
        list<Process*> getProcessList();
        Process* createProcess();
        void getProcess(list<Process*> list);

};


//ProcessList.cpp
 #include <iostream>
 #include "ProcessList.h"
 using namespace std;

ProcessList::ProcessList(int number_of_processes_to_create)
{
    processList.push_back(createProcess());
}

Process* ProcessList::createProcess()
{
    Process *process = new Process();

    return process;
}

list<Process*> ProcessList::getProcessList()
{
    return processList;
}

void ProcessList::getProcess(list<Process*> list)
{

    list<Process*>::iterator i;

}
Buck Wheat
  • 41
  • 5
  • 2
    Pro Tip: Never use `using namespace std;` in a header file. – NathanOliver Jun 07 '17 at 16:21
  • 1
    Can you provide us with a [MCVE] that reproduces the problem please. I'm pretty sure that not all that code is necessary to do so. Looks like a problem with circular `#include` statements. – πάντα ῥεῖ Jun 07 '17 at 16:23
  • by the way.. read this to make sure you really need to use `list` instead of `vector` https://softwareengineering.stackexchange.com/questions/185222/what-is-the-point-of-using-lists-over-vectors-in-c because it sounds that you came from JAVA and is used to using `list`. In c++ you should use `vector` by default and `list` only if you need to improve insertion and deletion (very basically speaking) – thiagoh Jun 07 '17 at 16:27
  • @NathanOliver: In this case, I don't think it matters that it's in a header file. The same thing would happen if it were only in the .cpp files. – Fred Larson Jun 07 '17 at 16:33

1 Answers1

3

Look at this function closely:

void ProcessList::getProcess(list<Process*> list)
{
   list<Process*>::iterator i;
}

What does list mean within the function body? It's not std::list here, because you've hidden that with your parameter name. That makes no sense to the compiler.

An easy way to express what you really mean is qualify it:

void ProcessList::getProcess(list<Process*> list)
{
    std::list<Process*>::iterator i;
}

But you should really just do that everywhere and forget you ever heard of using namespace std;. And it's not a good idea to use list as your parameter name, since it tends to confuse the reader if not the compiler.

Fred Larson
  • 60,987
  • 18
  • 112
  • 174
  • This worked, thank you! Can you how I've hidden std::list in the parameter name and why I needed to redefine list in the function body? I've heard that using namespace std is bad practice before, I will read over that link, thanks.*edit-spelling – Buck Wheat Jun 07 '17 at 17:06
  • You've defined a symbol called `list` for that function, and that takes precedence over the `list` in the `std::` namespace while you're in that function. So your local `list` hides the one in `std::`. – Fred Larson Jun 07 '17 at 17:09
  • Awesome, Thanks again. – Buck Wheat Jun 07 '17 at 17:16