C++ 错误:"<"标记之前应为";"

c++ error: expected ‘;’ before ‘<’ token

本文关键字:错误 lt C++      更新时间:2023-10-16

所以我在写光线追踪器的同时试图学习c++,当我尝试使用我的makefile(首先生成.o文件,然后链接它们)编译时,我有点纠结于这个错误。完整的错误如下:

touch include/ray.h
touch include/material.h
touch include/hit.h
touch include/object.h
touch include/light.h
touch include/scene.h
touch include/sphere.h
touch include/directional_light.h
g++ -c -O -I. raytrace.cpp -o raytrace.o -std=c++0x
g++ -c -O -I. base/scene.cpp -o base/scene.o -std=c++0x
g++ -c -O -I. base/vector.cpp -o base/vector.o -std=c++0x
g++ -c -O -I. base/vertex.cpp -o base/vertex.o -std=c++0x
g++ -c -O -I. base/colour.cpp -o base/colour.o -std=c++0x
g++ -c -O -I. objects/object.cpp -o objects/object.o -std=c++0x
g++ -c -O -I. objects/sphere.cpp -o objects/sphere.o -std=c++0x
g++ -c -O -I. base/material.cpp -o base/material.o -std=c++0x
g++ -c -O -I. base/ray.cpp -o base/ray.o -std=c++0x
g++ -c -O -I. lights/light.cpp -o lights/light.o -std=c++0x
g++ -c -O -I. lights/directional_light.cpp -o lights/directional_light.o -std=c++0x
g++ -c -O -I. base/hit.cpp -o base/hit.o -std=c++0x
g++ -c -O -I. base/matrix.cpp -o base/matrix.o -std=c++0x
touch include/t_stack.h
g++ -c -O -I. t_stack/matrix_w.cpp -o t_stack/matrix_w.o -std=c++0x
touch include/t_stack.h
g++ -c -O -I. t_stack/t_stack.cpp -o t_stack/t_stack.o -std=c++0x
In file included from t_stack/t_stack.cpp:1:
./include/t_stack.h:12: error: expected ‘;’ before ‘<’ token
./include/t_stack.h:13: error: expected ‘;’ before ‘<’ token

导致问题的代码是t_stack.h文件:

1 #ifndef _STACK_H_
2 #define _STACK_H_
3
4 #include <stack>
5 #include <list>
6 #include <iostream>
7 #include "include/matrix.h"
8 #include "include/matrix_w.h"
9 
10 class T_stack {
11    private:
12        stack<Matrix_w*, list<Matrix_w* > >* m_stack;
13        list<Matrix<double>* >* t_list;
14    public:
15        T_stack();
16        void pop();
17        void push_translation(double tx, double ty, double tz);
18        void push_scaling(double sx, double sy, double sz);
19        int size() const;
20        ~T_stack();
21 };
22
23 #endif

我已经检查了包含在这个如matrix.h和matrix_w.h的其他文件,我很确定他们没有丢失任何分号或任何的东西。我甚至尝试编译一切,但t_stack.h文件,它正确构建…知道是什么问题吗?这有点奇怪,因为在我尝试将这些文件与其他代码合并之前,我用更简单的程序做了几次测试,结果都很好……非常感谢你的帮助。谢谢!

stacklist,就像标准库中的所有东西一样,在std命名空间中声明。为了使用它们,您需要将它们限定为std::stackstd::list

同时,不要使用_STACK_H_这样的保留名称。

您的stacklist应该是std::stackstd::list。顺便说一下,不要试图在头文件中执行using namespace std—这是错误的方法。您需要在头文件中包含名称空间。