错误:"class name"不命名类型
error: "class name" does not name a type
在运行我的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
的存在,并有望消除您面临的错误。
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- C++错误:"class name"之前的预期类型说明符
- typeid.name 返回派生类类型之前的数字
- 返回别名标识符,而不是typeid(SOME_TYPE).name()中的类型
- 错误:"class name"不命名类型
- 什么是类型*volatile*name=value
- 编译错误C++与上面定义的发生错误的类型"does not name a type"?
- 模板错误:非类型 ".. [with T=T] is not a type name"
- “cname”和“name.h”中的类型可以是不同的类型吗?
- "&"不是原子类型"xs:Name"的有效值
- 在bison中,是否有一种方法可以返回令牌的Name而不是其类型
- 非类-name c++ 11类型-name是一个simple-template-id
- 错误:"dependent name is not a type" 。当在类中使用 typedef 类型作为返回值时,带有模板
- inject -class-name表示不完整和完整类型
- 错误:'class name'重新声明为不同类型的符号?
- 伪destructor-name对非类和非枚举类型有意义吗?
- 获取"does not name a type error",但我正在命名一个类型
- "'SpaceShip' does not name a type"即使宇宙飞船肯定是一种类型
- 模板类 "does not name a type" 中定义的类型
- C++ 'struct name' 中的结构不命名类型