编译器报告:error: expected unqualified-id before ' using '



我有两个文件,homework1.cchomework1.h

homework1.cc的顶部为

#include <iostream>
#include <fstream>
#include <cstring>
#include <GL/glut.h>
#include "homework1.h"
using namespace std;

homework1.h的顶部为

#ifndef _rt_H
#define _rt_H
#include <cmath>
#include <vector>
#include <list>
#include <GL/glut.h>
using namespace std;

这是我在homework1.h中使用list的地方

class surTriangle
{
    float x;
    float y;
    float z;
    list <int> surTriangles;
    bool compareVertex(Vector3f anotherVec)
    {
        if(anotherVec[0] == x && anotherVec[1] == y && anotherVec[2] == z)
            return true;
        return false;
    }
} 

当我尝试编译homework.cc时,编译器报告

homework1。Cc:8:错误:' using '

之前预期的unqualified-id

然而,当我删除#include <list>或上面的第三段代码时,它编译成功。

你能帮我找出问题是什么吗?

在类声明之后需要一个分号。(homework1.h类片段的最后一行)。否则使用将被解释为

中的标识符
class X {
} x;

您在类定义的末尾忘记了;

在调试解析错误时,考虑预处理器是如何工作的是有用的;在你的脑海中(或者用gcc -E,如果你想真正正确地看待它)想象一下#include指令被解析后的代码是什么样子。

然后,您将看到错误直接落在类定义之后,这极大地缩小了范围。错误显示using是"unexpected",这通常表示终止/结束前一个语句的问题。这会导致发现缺少分号,这是一个典型的打字错误。

相关内容

最新更新