“缺少类型说明符int假定为”C ++问题

时间:2011-12-17 21:46:42

标签: c++ visual-studio-2010

我有以下问题:当我编译源代码时出现此错误:

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

2 个答案:

答案 0 :(得分:1)

我怀疑Rectangle类未在翻译单元Primitive之前定义。

答案 1 :(得分:1)

除了设计非常糟糕外,您所显示的代码中没有实际的错误。我的猜测是错误出现在你没有向我们展示的代码中。

我个人的猜测是,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(); }
};