2013-04-27 133 views
-1

我有一个问题。没有构造函数的实例C++

“智能感知:没有构造的实例 “树::树” 的 参数列表参数类型相匹配:(浮动[3],浮[3],浮子,浮 ,整型,双,INT, INT)”。

第三行:

float ColorS[3]={1,1,1},ColorF[3]={1,0,0}; 
for(unsigned int i=0;i<20;i++){ 
    Tree a(ColorS,  ColorF, 
      5.0f,   5.0f, 
      rand()%180+90, 0.67, 
      rand()%4+2, rand()%6+2); 
    las.push_back(a); 
    a.cordx=rand()%50-25; 
    a.cordz=rand()%50-25; 
} 

那是我的类tree.h中:

class Tree{ 
. 
. 
. 
Tree(float [3],float [3],float,float,float,int,int); 
. 
. 
. 
}; 

而这就是我的construcotor在Tree.cpp:

Tree::Tree(float fromColor[3], float toColor[3], 
      float h=5.0f,  float angle=60*rad, 
      float ratio=0.67f, int amount=4, 
      int maxLevel=5){ 
. 
. 
. 

===

编辑:现在我有这样的问题:

'树::树':没有重载函数有5个参数

下联:

for(unsigned int i=0;i<20;i++){ 
Tree a(5.0f, 1.0f, 
     0.67f, rand()%4+2, 
     rand()%6+2); 
    las.push_back(a); 
    a.cordx=rand()%50-25; 
    a.cordz=rand()%50-25; 
} 

这就是在树我的班.h:

class Tree{ 
    ... 
    Tree(float,float,float,int,int); 
    ... 
}; 

这就是我在Tree.cpp中的构造函数:

Tree::Tree(float h=5.0f,  float angle=60*rad, 
      float ratio=0.67f, int amount=4, 
      int maxLevel=5){ 
    ... 
} 
+0

我已经重新格式化了你的代码 - 你可以看到你正在调用一个带有8个参数的构造函数并且声明有7 – 2013-04-27 14:16:34

+1

我已经重新编辑了你的问题,包括原始问题和新问题。如果问题完全不同,记住答案是没有意义的。您需要为将来阅读问答的人提供一些背景信息。 – 2013-04-27 15:31:59

回答

5

你调用8个参数

Tree a(ColorS,  ColorF, 
     5.0f,   5.0f, 
     rand()%180+90, 0.67, 
     rand()%4+2, rand()%6+2); 

的构造函数,但您提供7

Tree::Tree(float fromColor[3], float toColor[3], 
      float h=5.0f,  float angle=60*rad, 
      float ratio=0.67f, int amount=4, 
      int maxLevel=5){ 

这是一个主要的原因不具备简洁的代码中声明它。你应该有很多空白,这样的事情变得明显。你也可以(我不知道你的情况是否有效)为参数h提供更好的名称。此外,我倾向于找到这样的代码更易于阅读:

const float defaultH = 5.0f; 
const float defaultAngle = 5.0f; 
const float ratio = rand() % 180 + 90f; 
const float amount = 0.67; 
const float maxLevel = 5.0f; 

Tree a(ColorS,  ColorF, 
     defaultH, defaultAngle, 
     ratio,  amount , 
     rand()%4+2, maxLevel); 

编辑:既然你已经改变了你的问题相当

现在你可以看到你传递什么,在哪里,以及因此参数类型问题更为明显,例如将金额声明为int并作为float传递。这就是为什么我提出了使用上述技术来使代码不透明的建议。

你重新编译过所有的代码吗?

+0

Thx,那是真的。但是我改变了我的代码,仍然有问题。 – Antua 2013-04-27 15:03:09

+2

阅读您的代码。你传递的是相同的参数类型吗?请注意我在最后一句中所说的话 – 2013-04-27 15:26:53

0

您已将错误数量的参数传递给您的构造函数。

对于具有很多参数的构造函数或方法,您可能想要在自己的行上键入每一个,以提高可读性并避免这样的错误。