"Missing type specifier int assumed" C++问题

"Missing type specifier int assumed" C++ Issue

本文关键字:C++ 问题 assumed specifier Missing type int      更新时间:2023-10-16

我有以下问题:当我编译源代码时,出现了这个错误:

error C4430: missing type specifier - int assumed

当变量没有使用任何类型(如整数、浮点数或任何自定义类型的对象)初始化时,会发生此错误。但是,我有一个初始化的类型,它是不可识别的,尽管我有更多的类,这些类的构建类似于麻烦的那个,它们运行得很好。

那么,我的问题类是下面这个:

class Rectangle
{
    float x1, y1, x2, y2;
    bool created;
public:
    Rectangle() { created = false;}
    Rectangle(float x1, float y1, float x2, float y2);
    ~Rectangle() {}
    bool isCreated() { return created;}
    void setCreated( bool c) { created = c;}
    float getX1() { return x1;}
    float getX2() { return x2;}
    float getY1() { return y1;}
    float getY2() { return y2;}
};

一个工人阶级的例子:

class Triangle
{
private:
    vector<float> x, y, z;
    bool created;
public:
    Triangle() { created = false;}
    Triangle(vector<float> x, vector<float> y, vector<float> z);
    ~Triangle() {}
    bool isCreated() { return created;}
    void setCreated( bool c) { created = c;}
    vector<float> getX() { return x;}
    vector<float> getY() { return y;}
    vector<float> getZ() { return z;}
};

最后,出现错误的代码片段:

class Primitive
{
private:
    string index;
    Material mat;
    Texture text;
    int count;
    Rectangle rect; //error detected in this line
    Triangle tri;
    Cylinder cyl;
    Sphere sph;
    Torus tor;
public:
    Primitive() {count = 0;}
    Primitive(string id, Material mt, Texture tx);
    ~Primitive(void);
    string getIndex() { return index;}
    Material getMaterial() { return mat;}
    Texture getTexture() { return text;}
    void addRectangle(float x1, float y1, float x2, float y2);     //error detected in this line
    Rectangle getRectangle() { if(rect.isCreated()) return rect; else return Rectangle(); }   //error detected in this line
    void addTriangle(float x1, float x2, float x3, float y1, float y2, float y3, float z1, float z2, float z3);
    Triangle getTriangle() { if(tri.isCreated()) return tri; else return Triangle(); }
    void addCylinder(float bs, float tp, float hei, int sli, int stac);
    Cylinder getCylinder() { if(cyl.isCreated()) return cyl; else return Cylinder(); }
    void addSphere(float rad, int sli, int stac);
    Sphere getSphere() { if(sph.isCreated()) return sph; else return Sphere(); }
    void addTorus(float in, float out, int sli, int loo);
    Torus getTorus() { if(tor.isCreated()) return tor; else return Torus(); }
};

有什么好主意吗?

编辑:我的包括:

#include "Material.h" //custom class
#include "Texture.h"  //custom class
#include <iostream>

编译器是Visual C++ 2010

除了非常糟糕的设计之外,您所展示的代码中并没有实际的错误。我猜错误出在你没有给我们看的代码里。

我个人的猜测是,Rectanglerect在某种程度上并不意味着你在使用它们时期望它们的意思。也许有人在预处理器上耍花招。

我绝对肯定你在这里展示的代码中没有任何错误。我稍微编辑了一下你的代码,它可以完美地编译:

#include <string>
#include <vector>
class Rectangle
{
    float x1, y1, x2, y2;
    bool created;
public:
    Rectangle() { created = false;}
    Rectangle(float x1, float y1, float x2, float y2);
    ~Rectangle() {}
    bool isCreated() { return created;}
    void setCreated( bool c) { created = c;}
    float getX1() { return x1;}
    float getX2() { return x2;}
    float getY1() { return y1;}
    float getY2() { return y2;}
};
class Triangle
{
private:
    ::std::vector<float> x, y, z;
    bool created;
public:
    Triangle() { created = false;}
    Triangle(::std::vector<float> x, ::std::vector<float> y, ::std::vector<float> z);
    ~Triangle() {}
    bool isCreated() { return created;}
    void setCreated( bool c) { created = c;}
    ::std::vector<float> getX() { return x;}
    ::std::vector<float> getY() { return y;}
    ::std::vector<float> getZ() { return z;}
};
class Primitive
{
private:
    ::std::string index;
//    Material mat;
//    Texture text;
    int count;
    Rectangle rect; //error detected in this line
    Triangle tri;
//    Cylinder cyl;
//    Sphere sph;
//    Torus tor;
public:
    Primitive() {count = 0;}
//    Primitive(::std::string id, Material mt, Texture tx);
    ~Primitive(void);
    ::std::string getIndex() { return index;}
//    Material getMaterial() { return mat;}
//    Texture getTexture() { return text;}
    void addRectangle(float x1, float y1, float x2, float y2);     //error detected in this line
    Rectangle getRectangle() { if(rect.isCreated()) return rect; else return Rectangle(); }   //error detected in this line
    void addTriangle(float x1, float x2, float x3, float y1, float y2, float y3, float z1, float z2, float z3);
    Triangle getTriangle() { if(tri.isCreated()) return tri; else return Triangle(); }
//    void addCylinder(float bs, float tp, float hei, int sli, int stac);
//    Cylinder getCylinder() { if(cyl.isCreated()) return cyl; else return Cylinder(); }
//    void addSphere(float rad, int sli, int stac);
//    Sphere getSphere() { if(sph.isCreated()) return sph; else return Sphere(); }
//    void addTorus(float in, float out, int sli, int loo);
//    Torus getTorus() { if(tor.isCreated()) return tor; else return Torus(); }
};

我怀疑在翻译单元中Primitive之前没有定义Rectangle