类内的结构?

发布于 2024-10-21 02:49:50 字数 1300 浏览 1 评论 0原文

我有课。我制作了两个单独的文件,头文件和 c++ 文件。我正在使用它为我正在开发的 opengl 游戏创建一个或多或少的 Light“对象”。以下是文件: Light.h

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;
        struct ambient
        {
            float r, g, b, a;
        };
        struct diffuse
        {
            float r, g, b, a;
        };
        struct specular
        {
           float r, g, b, a;
        };
    protected:
    private:
};

#endif // LIGHT_H

和 Light.cpp

#include "../include/Light.h"

Light::Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex)
{
    index=iindex;
    type=itype;
    x=ix;
    y=iy;
    z=iz;
    ambient.r = 0.2;
    ambient.g = 0.2;
    ambient.b = 0.2;
    ambient.a = 1.0;
    specular.r = 0.8;
    specular.g = 0.8;
    specular.b = 0.8;
    specular.a = 1.0;
    diffuse.r = ir;
    diffuse.g = ig;
    diffuse.b = ib;
    diffuse.a = ia;
}

Light::~Light()
{
    //dtor
}

当我尝试编译时,它会抛出一个错误: 错误:“.”之前应有不合格的 id代币| 对于我为结构成员分配值的每一行(环境、漫反射、镜面反射) 首先,我什至无法解释这个错误。不知道这意味着什么。其次,我看不到自己做错了什么。请帮忙!

I have a class. I have made two seperate files, the header, and the c++ file. I am using it to create a more-or-less Light 'object' for an opengl game I am working on. Here are the files:
Light.h

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;
        struct ambient
        {
            float r, g, b, a;
        };
        struct diffuse
        {
            float r, g, b, a;
        };
        struct specular
        {
           float r, g, b, a;
        };
    protected:
    private:
};

#endif // LIGHT_H

and, Light.cpp

#include "../include/Light.h"

Light::Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex)
{
    index=iindex;
    type=itype;
    x=ix;
    y=iy;
    z=iz;
    ambient.r = 0.2;
    ambient.g = 0.2;
    ambient.b = 0.2;
    ambient.a = 1.0;
    specular.r = 0.8;
    specular.g = 0.8;
    specular.b = 0.8;
    specular.a = 1.0;
    diffuse.r = ir;
    diffuse.g = ig;
    diffuse.b = ib;
    diffuse.a = ia;
}

Light::~Light()
{
    //dtor
}

When I try to compile, it throws an error saying:
error: expected unqualified-id before ‘.’ token|
For every line where I assign a value to a member of the structs (ambient, diffuse, specular)
First off, I can't even interpret this error. No clue what it means. Secondly, I fail to see what I am doing wrong. Please help!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

停顿的约定 2024-10-28 02:49:50

应该这样读:

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;
        struct
        {
            float r, g, b, a;
        } ambient;
        struct
        {
            float r, g, b, a;
        } diffuse;
        struct
        {
           float r, g, b, a;
        } specular;
    protected:
    private:
};

#endif // LIGHT_H

基本问题是您声明了结构存在并给出了类型的名称,但您没有声明该类型的任何变量。因为,从您的使用情况来看,很明显这些结构的类型不需要名称(它们可以是匿名结构),我在声明之后移动了名称,因此您声明了一个变量。

正如 GMan 指出的那样,这仍然不是最佳的。这是一个更好的方法来解决这个问题:

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;

        struct Color {
            float r, g, b, a;
        };

        Color ambient, diffuse, specular;
    protected:
    private:
};

#endif // LIGHT_H

This should read so:

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;
        struct
        {
            float r, g, b, a;
        } ambient;
        struct
        {
            float r, g, b, a;
        } diffuse;
        struct
        {
           float r, g, b, a;
        } specular;
    protected:
    private:
};

#endif // LIGHT_H

The basic problem is that you were declaring that the structs existed and giving the name of the type, but you weren't declaring any variables of that type. Since, from your usage, it was clear that the type of these structs didn't need a name (they could be anonymous structs) I moved the name after the declaration so you were declaring a variable instead.

As GMan pointed out, this is still not optimal. Here is a better way to go about this:

#ifndef LIGHT_H
#define LIGHT_H


class Light
{
    public:
        Light(float ix, float iy, float iz, float ir, float ig, float ib , float ia, int itype, int iindex);
        virtual ~Light();
        float x,y,z;
        int index;
        int type;

        struct Color {
            float r, g, b, a;
        };

        Color ambient, diffuse, specular;
    protected:
    private:
};

#endif // LIGHT_H
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文