2011-12-17 58 views
1

我有以下問題:在我看來這個錯誤,當我編譯源代碼:「缺少類型說明INT假設」 C++問題

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

+0

你有什麼包括? – 2011-12-17 21:50:05

+0

這是什麼編譯器? – Omnifarious 2011-12-17 21:55:04

回答

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(); } 
}; 
+2

「與預處理器玩弄技巧」 - 在失蹤中包括?:) – 2011-12-17 22:04:31

+0

@ MichaelKrelin-hacker:是的。或多或少。我想到,在所有包含處理完畢後,在結果'翻譯單元'的某個地方,有''rect''或'Rectangle'的'#define',而不是''undef'ed'原始類被定義。我的猜測是'rect',因爲否則我會期待'Rectangle'的定義引發神聖的破壞。 – Omnifarious 2011-12-17 22:33:07

+0

不,我在這裏沒有顯示的代碼沒有任何錯誤。 – 2011-12-17 22:37:01

1

我懷疑Rectangle類沒有在翻譯單元中的Primitive之前定義。

+1

不僅聲明,而且定義。 – Pubby 2011-12-17 21:51:18

+0

@Pubby,我不清楚這些條款,但通過聲明我並不是指前瞻性聲明,您在說什麼定義?我只能想到成員函數的定義(即 - 實現),這是沒有必要的。 – 2011-12-17 21:57:24

+0

是的,不知道這個詞是否正確,我的意思是你說的 - 在這種情況下,前向定義是不夠的。 – Pubby 2011-12-17 22:00:51