c ++迭代器错误“不引用值”

时间:2017-06-07 16:18:07

标签: c++ list stl iterator

我只是想为类指针列表创建一个迭代器。我搜索了一会儿,找不到任何关于这个问题的内容。一切正常,直到我尝试创建迭代器。

以下是我收到的编译错误消息:

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.

这是我的代码。错误来自底部的ProcessList.cpp代码:

//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;

}

1 个答案:

答案 0 :(得分:3)

仔细看看这个功能:

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

list在函数体中的含义是什么?这里不是std::list,因为您已使用参数名称隐藏了它。这对编译器没有意义。

表达真实含义的简单方法是限定它:

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

但你应该到处都这样做forget you ever heard of using namespace std;。使用list作为参数名称并不是一个好主意,因为如果不是编译器,它会使读者感到困惑。