gpt4 book ai didi

c++ - 错误 : expected class-name before '{' token - can't seem to find any circular includes

转载 作者:行者123 更新时间:2023-11-28 02:00:35 29 4
gpt4 key购买 nike

我知道这个问题已经被问过几次了,通常的情况是因为循环包含,但在过去的几个小时里,我一直在努力找出我可能有循环包含的地方。我认为我也没有可以转发声明任何内容的观点。我可能错了,所以我需要一些帮助

我要完成的作业使用两个 ADT,一个是 Stack,另一个是 Queue。这些文件被模板化以接受任何数据类型,但在本实验中我们将使用字符串。我们的 Queue.h/.hpp 应该实现一个 QueueInterface.h,而 Stack.h/.hpp 应该实现一个 StackInterface.h

最后,我们必须定义我们自己的名为 PreconditionViolationException 的错误。h 是 std::runtime_error 的子类。

Maybe this link will help if that's not a good explanation

为了可能节省一些时间,我将从我正在使用的文件列表开始,然后是我的 Makefile。

Executive.h/.cpp, main.cpp, Node.h/.hpp PreconditionViolationException.h/.cpp Queue.h/.hpp QueueInterface.h Stack.h/.hpp StackInterface.h

BuildingExecutive: main.o Executive.o PreconditionViolationException.o
g++ -std=c++11 -g -Wall main.o PreconditionViolationException.o Executive.o -o BuildingExecutive

main.o: main.cpp
g++ -std=c++11 -g -Wall -c main.cpp

PreconditionViolationException.o: PreconditionViolationException.cpp PreconditionViolationException.h
g++ -std=c++11 -g -Wall -c PreconditionViolationException.cpp

Executive.o: Executive.cpp Executive.h Queue.hpp Queue.h Stack.hpp Stack.h Node.hpp Node.h StackInterface.h QueueInterface.h
g++ -std=c++11 -g -Wall -c Executive.cpp

这是关于我的 Makefile 的一个可能离题的问题。我的问题是我是否应该将 PreconditionViolationException.o 编译为它自己的目标文件?我明白为什么我不显式编译 Stack 和 Queue 文件,因为它们是模板化的,但是由于唯一依赖于 PreconditionViolationException 的文件是模板化文件,这有什么不同吗?我的 Executive(这是只输出和运行程序的文件)不依赖于 PreconditionViolationException,它只捕获任何 std::exception,它应该捕获 PreconditionViolationException,因为 std::runtime_error 是 std::exception

好吧,如果我的 Makefile 没有明显的问题,这基本上就是我尝试跟踪它是否有任何循环包含的方法。

我从我的 main.cpp 开始,它看起来像这样。

#include "Executive.h"

int main(int argc, char** argv) {
Executive exec(argv[1]);
exec.Run();
return 0;
}

这只包括 Executive.h,所以这里是

#ifndef EXECUTIVE_H
#define EXECUTIVE_H

#include "Queue.h"
#include "Stack.h"

class Executive {
.
. will cutout whatever isn't necessary
.
private:
Queue<std::string> Line;
Stack<std::string> Elevator;
};
#endif

这个文件依赖于 Queue.h 和 Stack.h,所以这里是那些

#ifndef QUEUE_H
#define QUEUE_H

#include "Node.h"
#include "QueueInterface.h"

template <typename T>
class Queue : public QueueInterface {
.
.
.
};
#endif

然后

#ifndef STACK_H
#define STACK_H

#include "Node.h"
#include "StackInterface.h"

template <typename T>
class Stack : public StackInterface {
.
.
.
};

我认为 Node 不会在这里引起问题,所以这里是接口(interface)

#ifndef STACKINTERFACE_H
#define STACKINTERFACE_H

#include "PreconditionViolationException.h"

template <typename T>
class StackInterface {
.
.
.
};
#endif

#ifndef QUEUEINTERFACE_H
#define QUEUEINTERFACE_H

#include "PreconditionViolationException.h"

template <typename T>
class QueueInterface {
.
.
.
}
#endif

其中每一个都包含 PreconditionViolationException,因为它们的方法可以抛出该异常。

#ifndef PVE_H
#define PVE_H

#include <stdexcept>
#include <string>

class PreconditionViolationException : public std::runtime_error {
.
.
.
}
#endif

如果我错了请纠正我,但在阅读之后 this我认为除了在我声明我的节点时我可以转发声明任何东西之外,没有其他地方。由于我对如何将所有内容编译在一起的理解并不是最好的,所以我唯一能想到的是我的 makefile 不适合这项任务,或者我有我无法识别的循环包含。

我花了很多时间试图追踪和理解正在发生的事情,所以我希望有一些东西可以帮助我更好地理解正在发生的事情。

抱歉,如果这真的很长!感谢您的帮助!

最佳答案

template<class T>
class Stack : public StackInterface {

StackInterface不是一个类(它是一个类模板),所以你不能从它继承。

您可能打算继承自 StackInterface<T> ,同样适用于 QueueInterface .

关于c++ - 错误 : expected class-name before '{' token - can't seem to find any circular includes,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39780306/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com