C++ 错误 C2143:语法错误:缺少“;” 在“命名空间”之前

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10108703/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 13:38:01  来源:igfitidea点击:

Error C2143: syntax error : missing ';' before 'namespace'

c++syntaxsstream

提问by Rainbowdave

I am VERY new to C++ and Open GL and I have been trying to display 3D objects in a scene. it worked fine with one but when I tried to alter my code to add a second, my code regarding the HUD text showing the camera location started giving errors. The error above is shown and it is apparently in the sstream file (#include). I have tried searching around and asking for help but there is nothing that helps/that I understand. When I comment-out the #include line and the code that uses it, I get a similar saying "error C2143: syntax error : missing ';' before 'using'" in my main.cpp file.

我对 C++ 和 Open GL 非常陌生,我一直在尝试在场景中显示 3D 对象。它在一个上运行良好,但是当我尝试更改我的代码以添加第二个时,我的关于显示相机位置的 HUD 文本的代码开始出现错误。显示了上面的错误,它显然在 sstream 文件 (#include) 中。我试过四处寻找并寻求帮助,但没有任何帮助/我理解的东西。当我注释掉 #include 行和使用它的代码时,我得到类似的说法“错误 C2143:语法错误:缺少 ';' 在我的 main.cpp 文件中“使用”之前。

I am running Visual Studio 2010 and I have even tried turning the whole thing off and on again, and copying the code over to a new project. Help would be greatly appreciated.

我正在运行 Visual Studio 2010,我什至尝试关闭和打开整个过程,并将代码复制到新项目中。帮助将不胜感激。

#include <Windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "glut.h"
#include "SceneObject.h"
#include <fstream>
#include <sstream>
#include <iostream>
#include <string>
//#include <cmath>
//#include <limits>
//#include <cstdlib>

using namespace std;

...

...

stringstream ss;
ss << "Camera (" << cam.pos.x << ", " << cam.pos.y << ", " << cam.pos.z << ")";
glClear(GL_DEPTH_BUFFER_BIT);
outputText(-1.0, 0.5, ss.str());

...

...

#ifndef SCENEOBJECT_H
#define SCENEOBJECT_H
#include <string>
#include <iostream>
#include <fstream>

using namespace std;

struct point3D {
    float x;
    float y;
    float z;
};

struct colour{
    float r;
    float g;
    float b;
};

struct tri {
    int a;
    int b;
    int c;
};

class SceneObject {
private:
    int NUM_VERTS;
    int NUM_COL;
    int NUM_TRI;
    point3D  * vertices;
    colour * colours;
    tri  * indices;
    void drawTriangle(int a, int b, int c);
public:
    SceneObject(const string fName) {
        read_file(fName);
    }
    void drawShape()
    {
        // DO SOMETHING HERE
    }
    int read_file (const string fileName)
    {
    ifstream inFile;
    inFile.open(fileName);

    if (!inFile.good())
    {
        cerr  << "Can't open file" << endl;
        NUM_TRI = 0;
        return 1;
    }

    //inFile >> shapeID;

    inFile >> NUM_VERTS;
    vertices = new point3D[NUM_VERTS];

    for (int i=0; i < NUM_VERTS; i++)
    {   
        inFile >> vertices[i].x;
        inFile >> vertices[i].y;
        inFile >> vertices[i].z;
    }

    inFile >> NUM_COL;
    //inFile >> randomCol;
    colours = new colour[NUM_COL];
    /*if (randomCol == 'y')
    {
        for (int i=0; i < NUM_COL; i++)
        {
            colours[i].r = ((float) rand() / (RAND_MAX+1));
            colours[i].g = ((float) rand() / (RAND_MAX+1));
            colours[i].b = ((float) rand() / (RAND_MAX+1));
        }
    }
    else if (randomCol == 'n')
    {*/
        for (int i=0; i < NUM_COL; i++)
        {   
            inFile >> colours[i].r;
            inFile >> colours[i].g;
            inFile >> colours[i].b;
        }
    //}

    inFile >> NUM_TRI;
    indices = new tri[NUM_TRI];

    for (int i=0; i < NUM_TRI; i++)
    {   
        inFile >> indices[i].a;
        inFile >> indices[i].b;
        inFile >> indices[i].c;
    }

    inFile.close();
    return 0;
}
}
#endif

I haven't changed the code and as far as I am aware, there are semi-colons where there are meant to be. Even my friend who has been programming for 5 years couldn't solve this. I will include any other specific code if needed. And when I said new to C++ and OpenGL I really much VERY new. This is even my first post. I'll get there eventually.

我没有更改代码,据我所知,应该有分号。即使是我编程5年的朋友也无法解决这个问题。如果需要,我将包含任何其他特定代码。当我说 C++ 和 OpenGL 的新手时,我真的很新。这甚至是我的第一篇文章。我最终会到达那里。

回答by retinotop

you have to put a ';' after each class

你必须放一个';' 每节课后

That means

这意味着

class foo{
public:
   void bar();
};

and i think you miss the last semicolon.

我想你错过了最后一个分号。

回答by dennis

Every including/function/call etc. has a body wich executes a line of code, because C++ ignores whitespace you will need to tell the program when the line stops this would run only an action and not confuse it with others, to tell your program when an action ends you put a ";"

每个包含/函数/调用等都有一个执行一行代码的主体,因为 C++ 忽略空格你需要告诉程序当该行停止时这只会运行一个动作而不会将它与其他动作混淆,告诉你的程序当一个动作结束时,你放一个 ";"

Because you need to compile C++ apps you will run into an error when you don't do this right. wich is the error you are running into so find out where you miss the ";"and add it

因为您需要编译 C++ 应用程序,所以如果您没有正确执行此操作,就会遇到错误。这是你遇到的错误,所以找出你错过的地方";"并添加它

I hope this helps you out.

我希望这能够帮到你。

回答by Bart

Your class SceneObjectdoes not have a ;at the end. This is probably the missing ;the error message is referring to. It is most likely causing your problems once you include the header in your code.

您的课程末尾SceneObject没有 a ;。这可能是;错误消息所指的缺失。一旦您在代码中包含标头,它很可能会导致您的问题。

This is exactly what Mat and David suggested before.

这正是 Mat 和 David 之前建议的。

回答by MarcoM

You should look at all files you included in this one. Perhaps in "SceneObject.h".. seems to be a lack of ";" but also verifies the "{" "}" and "(" ")" because mistakes can sometimes be a bit tricky.

您应该查看包含在此文件中的所有文件。也许在“SceneObject.h”中……似乎缺少“;” 但也验证“{”“}”和“(”“)”,因为错误有时会有点棘手。