没有构造函数C++的实例

Ant*_*tua -1 c++ constructor class

我有个问题.

"IntelliSense:没有构造函数的实例"Tree :: Tree"匹配参数列表的参数类型是:(float [3],float [3],float,float,int,double,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;
}
Run Code Online (Sandbox Code Playgroud)

这是我在Tree.h的课程:

class Tree{
.
.
.
Tree(float [3],float [3],float,float,float,int,int);
.
.
.
};
Run Code Online (Sandbox Code Playgroud)

这就是我在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){
.
.
.
Run Code Online (Sandbox Code Playgroud)

===

编辑:现在我有这个问题:

'Tree :: Tree':没有重载函数需要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;
}
Run Code Online (Sandbox Code Playgroud)

这是我在Tree.h中的课程:

class Tree{
    ...
    Tree(float,float,float,int,int);
    ...
};
Run Code Online (Sandbox Code Playgroud)

这就是我在Tree.cpp中的建设者:

Tree::Tree(float h=5.0f,       float angle=60*rad,
           float ratio=0.67f,  int amount=4, 
           int maxLevel=5){
    ...
}
Run Code Online (Sandbox Code Playgroud)

Pre*_*gha 5

您正在使用8个参数调用构造函数

Tree a(ColorS,        ColorF,
       5.0f,          5.0f,
       rand()%180+90, 0.67,
       rand()%4+2,    rand()%6+2);
Run Code Online (Sandbox Code Playgroud)

但你用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){
Run Code Online (Sandbox Code Playgroud)

这是没有简洁代码的主要原因.你应该有很多空白,以便这样的事情变得明显.您可能(我不知道它是否有效,但在您的情况下)有更好的名称参数,如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);
Run Code Online (Sandbox Code Playgroud)

编辑:因为你已经大大改变了你的问题

现在你可以看到你传递的内容和位置,因此参数类型问题更加明显,例如将数量声明为a int并作为a传递float.这就是为什么我提出使用上述技术使你的代码不那么不透明的原因.

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

  • 阅读你的代码.你传递相同的参数类型?请注意我在上一句中所说的内容 (2认同)