错误:"class name"不命名类型

error: "class name" does not name a type

本文关键字:类型 name class 错误      更新时间:2023-10-16

在运行我的main.cpp时遇到这个问题。 有一堆 .h 类和 .cpp 类文件,由于某种原因我只是收到此错误。

In file included from /home/ubuntu/workspace/cs1440-hw2/Analyzer.h:8:0,
                 from /home/ubuntu/workspace/cs1440-hw2/Station.h:7,
                 from /home/ubuntu/workspace/cs1440-hw2/Day.h:6,
                 from /home/ubuntu/workspace/cs1440-hw2/main.cpp:7:
/home/ubuntu/workspace/cs1440-hw2/Region.h:12:3: error: ‘Station’ does not name a type
   Station*        _stations[MAX_STATION_COUNT];

这不是我编写的代码,但我试图将其重组为单独的文件,但是当我完成并尝试运行它时,我不断收到此错误。 不知道为什么。 任何帮助表示赞赏。

#ifndef Region_H
#define Region_H
#include "Stat.h"
#include "Day.h"
#include "Station.h"
#include "Analyzer.h"
#include "Region.h"
class Region {
    private:
        Station*        _stations[MAX_STATION_COUNT];
        int             _stationCount=0;
        int             _currentStation;
    public:
        Region();
        void load(std::ifstream &input);
        void resetStationIteration();
        Station* getNextStation();
        Station* findStation(std::string& id);
    private:
        Station* addStation(std::string& id, std::string& name);
};
#endif

已经有另一个叫做 Station 的类,所以这应该可以工作,不知道为什么它不会。 请告诉我是否需要添加更多代码或细节 我是 C++ 的新手,所以需要很多帮助。

这是车站类 .h 文件

#ifndef Station_H
#define Station_H
#include "Stat.h"
#include "Day.h"
#include "Station.h"
#include "Analyzer.h"
#include "Region.h"
class Station {
    private:
        std::string         _id;
        std::string         _name;
        Day*               _days[MAX_DAYS];
        int                 _dayCount = 0;
        int                 _currentDay = -1;
    public:
        Station(std::string& id, std::string& name);
        void load(std::string& datetime, std::string& qgag, std::string& qpcp);
        void resetDayIteration();
        Day* getDayNext();
        std::string getId();
        std::string getName();
    private:
        Day* findDay(std::string& date);
        Day* addDay(std::string& date);
};
#endif

好的,感谢您到目前为止回答的所有问题,我已经使用了您的所有建议并修复了一些错误,但是,在尝试运行我的 main 函数时,我收到此错误:

Running /home/ubuntu/workspace/cs1440-hw2/main.cpp
/tmp/ccxaejI8.o: In function `main':
main.cpp:(.text+0x1e0): undefined reference to `Region::Region()'
main.cpp:(.text+0x1f9): undefined reference to `Region::load(std::basic_ifstream<char, std::char_traits<char> >&)'
main.cpp:(.text+0x264): undefined reference to `Region::findStation(std::string&)'
main.cpp:(.text+0x28e): undefined reference to `Analyzer::analyze(Station*)'
main.cpp:(.text+0x2ae): undefined reference to `Region::resetStationIteration()'
main.cpp:(.text+0x2c9): undefined reference to `Analyzer::analyze(Station*)'
main.cpp:(.text+0x2d8): undefined reference to `Region::getNextStation()'
collect2: error: ld returned 1 exit status

Process exited with code: 1

这是主文件,以便你们可以获得更好的感知:

#include <fstream>
#include <iostream>
#include <sstream>
#include <string>
#include "Analyzer.h"
#include "Region.h"
#define MAX_DAYS          365
#define MAX_STATS         100
#define MAX_STATION_COUNT 20
bool split(const std::string& s, char delimiter, std::string elements[], int expectedNumberOfElements) {
    std::stringstream ss;
    ss.str(s);
    std::string item;
    int i=0;
    while (std::getline(ss, item, delimiter) && i<expectedNumberOfElements) {
        elements[i++] = item;
    }
    return (i==expectedNumberOfElements);
}
int main(int argc, char* argv[]) {
    if (argc>1) {
        std::ifstream inputStream(argv[1], std::ios_base::in);
        Region region;
        region.load(inputStream);
        Analyzer analyzer;
        if (argc>2) {
            std::string stationId(argv[2]);
            Station* station = region.findStation(stationId);
            if (station!= nullptr)
                analyzer.analyze(station);
        }
        else {
            region.resetStationIteration();
            Station *station;
            while ((station = region.getNextStation()) != nullptr)
                analyzer.analyze(station);
        }
    }
    return 0;
}

问题是你的循环#include

Station.h的第一部分是

#include "Region.h"
class Station {

然后Region.h被包含在那里,然后再次包括Station.h。在该嵌套#include,宏条件#ifndef Station_H失败,因此没有可用的Station声明。

我建议你从两个头文件中删除彼此的包含,如果确实需要,并为类提供一个前向声明,正如Daniel H在评论中指出的那样:

class Station; // Put in Region.h
class Region;  // Put in Station.h

这声明了类Station的存在,并有望消除您面临的错误。