简体   繁体   English

我包括<iterator>但得到未知的类型名称“迭代器”

[英]Im including <iterator> but get unknown type name 'iterator'

I feel like this has to be an issue with namespace pollution, otherwise I dont know my compiler thinks iterator is an unknown type name我觉得这一定是命名空间污染的问题,否则我不知道我的编译器认为迭代器是一个未知的类型名称

In a previous coding project I had a class with the exact same method declaration with no issues, which is why I dont understand why it doesnt work here.在以前的编码项目中,我有一个具有完全相同方法声明的类,没有任何问题,这就是为什么我不明白为什么它在这里不起作用。

The full error is完整的错误是

In file included from AlgoContainer.cc:1:
./AlgoContainer.h:14:3: fatal error: unknown type name 'iterator'
            iterator begin() const;
            ^

1 error generated.产生了 1 个错误。

Below is the code where the error is being thrown, in my header file下面是在我的头文件中抛出错误的代码

#ifndef AlgoContainer_H_INCLUDED
#define AlgoContainer_H_INCLUDED

#include <vector>
#include "Task.h"
#include <iterator>


class AlgoContainer{
    public:
        AlgoContainer() = default;
        void addTask(const Task);
        ~AlgoContainer();
        iterator begin() const;
        iterator end() const;

    private:
        std::vector<Task> allTasks;
        void sort();
    
 };

 #endif /*AlgoContainer_H_INCLUDED*/

If it is needed I will include the Task header file I am using and the AlgoContainer.cc files如果需要,我将包含我正在使用的任务头文件和 AlgoContainer.cc 文件

AlgoContainer.cc算法容器.cc

#include "AlgoContainer.h"

using namespaces std;

AlgoContainer::addTask(const Task newTask){
    this->allTasks.push_back(newTask);
}

AlgoContainer::~AlgoContainer(){
    //TODO delete without memory leak
}

AlgoContainer::iterator AlgoContainer::begin() const{
    return this->allTasks.begin();
}

AlgoContainer::iterator AlgoContainer::end() const{
    return this->allTasks.end();
}

void AlgoContainer::sort(){
    //TODO
}

Task.h任务.h

#ifndef Task_H_INCLUDED
#define Task_H_INCLUDED

class Task{
    public:
        Task(int pid, int arrivalTime, int burstDuration, int priority = 1);
        ~Task() = default;
        void alterBurstDuration(const int);
        int getPriority();
        int getPid();
        int getArrivalTime();
        int getBurstDuration();
        void continueWaiting();
        void taskComplete(const int);
    private:
        int pid;
        int arrivalTime;
        int burstDuration;
        int priority;
        int waitingTime;
        int finishedTime;
        enum state { waiting, running, finished};
        state taskState;
};

#endif /*TASK_H_INCLUDED*/

Additionally these are the parameters in which I am compiling if that helps此外,这些是我正在编译的参数,如果有帮助的话

CXX=g++
CXXFLAGS=-Wall -Wextra -Wpedantic -Werror -Wfatal-errors -Winit-self -Wshadow -Wcomment -Wctor-dtor-privacy -Wold-style-cast -D_GLIBCXX_DEBUG -fno-diagnostics-show-option -std=c++14

Adding an std::iterator compiles with an error of添加 std::iterator 编译时出现错误

./AlgoContainer.h:14:8: fatal error: use of class template     'std::iterator' requires template
  arguments
            std::iterator begin() const;

Again weirdly in my past project I did not have the std:: before the iterator and had no issues, without using namespace std in the header再次奇怪的是,在我过去的项目中,我在迭代器之前没有 std:: 并且没有问题,没有在标题中使用命名空间 std

I was able to solve this by changing my header file to be我能够通过将头文件更改为

class AlgoContainer{
private:
    std::vector<Task> allTasks;
    void sort();
public:
    AlgoContainer() = default;
    void addTask(const Task);
    ~AlgoContainer();
    //CHANGE HERE
    using iterator = decltype(allTasks)::const_iterator;
    iterator begin() const;
    iterator end() const;

};

What is the consensus on whether this is frowned upon or not?对于这是否令人不悦,共识是什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM