没有默认构造函数存在 [英] no default constructor exists

查看:207
本文介绍了没有默认构造函数存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个麻烦,一个工作正常的类,现在似乎不想工作。



错误是没有适当的默认构造函数可用



我在两个地方使用这个类,然后将它们添加到列表中。



Vertice3f.h

  #pragma once 
#includeVector3f.h

// Vertice3f包含一个xyz位置的3个浮动和3个Vector3f的
//包含3个浮点数)uv,normal和color

类Vertice3f {
private:
float x,y,z;
Vector3f uv,normal,color;

public:
//如果你不想使用UV,Normal或Color
//只需传入一个Verctor3f,值为0,0,0 $ b Vertice3f(float _x,float _y,float _z,Vector3f _uv,
Vector3f _normal,Vector3f _color);
〜Vertice3f();
};

Vertice3f.cpp

  #includeVertice3f.h

Vertice3f :: Vertice3f(float _x,float _y,float _z,
Vector3f _uv,Vector3f _normal,Vector3f _color){
x = _x;
y = _y;
z = _z;
uv = _uv;
normal = _normal;
color = _color;
}

正在我的OBJModelLoader类中使用如下:

  list< Vertice3f> vert3fList; 

Vertice3f tvert = Vertice3f(
x =(float)atof(
vertList [i] .substr(
vertList [i] .find(v)+ 1,
vertList [i] .find(,vertList [i] .find(v)+ 2,10)
).c_str()
),
y =(float)atof(
vertList [i] .substr(
vertList [i] .find(,vertList [i] .find(v)+ 4,10)+ 1 ,
vertList [i] .find(,vertList [i] .find(v)+ 13,10)
).c_str()
),
z =(float)atof(
vertList [i] .substr(
vertList [i] .find(,vertList [i] .find(v)+ 13,10)
vertList [i] .find(,vertList [i] .find(v)+ 23,10)
).c_str()
) :Vector3f(0.0f,0.0f,0.0f),:: Vector3f(0.0f,0.0f,0.0f),:: Vector3f(0.0f,0.0f,0.0f)
);

vert3fList.push_back(
tvert
);

我试过自己定义一个默认的构造函数,因此在.h中我放

  Vertice3f(); 

和cpp

  Vertice3f :: Vertice3f(){
x = 0.0f;
y = 0.0f;
z = 0.0f;
uv = Vector3f(0.0f,0.0f,0.0f);
normal = Vector3f(0.0f,0.0f,0.0f);
color = Vector3f(0.0f,0.0f,0.0f);
}


$ b $ p

所以,我不知道为什么它找不到默认构造函数或如何安抚编译器。我确信它是用户错误,因为编译器可能知道它在做什么。

解决方案

我会回答您的问题。猜测缺少的默认构造函数是 Vector3f 类的默认构造函数,而不是 Vertice3f 类的默认构造函数。您的 Vertice3f 的构造函数尝试默认构造其 Vector3f 成员,导致错误。



这是为什么你试图为 Vertice3f 提供默认构造函数不改变任何东西的原因。问题出在 Vector3f



要修复它提供所有必要的默认构造函数与您的设计),或者使用初始化器列表而不是体内赋值重写 Vertice3f 的构造函数

  Vertice3f :: Vertice3f(float _x,float _y,float _z,
Vector3f _uv,Vector3f _normal,Vector3f _color):
x(_x),y (_z),uv(_uv),normal(_normal),color(_color)
{}

此版本不再尝试默认构造任何东西。在任何情况下使用初始化列表而不是体内赋值是一个好主意。


I'm having some trouble with a class that was working fine and now doesn't seem to want to work at all.

The error is "No appropriate default constructor available"

I am using the class in two places I'm making a list of them and initializing then adding them to the list.

Vertice3f.h

#pragma once 
#include "Vector3f.h"

// Vertice3f hold 3 floats for an xyz position and 3 Vector3f's 
//  (which each contain 3 floats) for uv, normal and color

class Vertice3f{
private:
    float x,y,z;
    Vector3f uv, normal, color;

public:
    // If you don't want to use a UV, Normal or Color 
    //  just pass in a Verctor3f with 0,0,0 values
    Vertice3f(float _x, float _y, float _z, Vector3f _uv, 
              Vector3f _normal, Vector3f _color);
    ~Vertice3f();
};

Vertice3f.cpp

#include "Vertice3f.h"

Vertice3f::Vertice3f(float _x, float _y, float _z, 
                     Vector3f _uv, Vector3f _normal, Vector3f _color){
    x = _x;
    y = _y;
    z = _z;
    uv = _uv;
    normal = _normal;
    color = _color;
}

It is being using in my OBJModelLoader class as follows:

list<Vertice3f> vert3fList;

Vertice3f tvert = Vertice3f(
            x = (float)atof(
            vertList[i].substr(
                vertList[i].find("v") + 1,
                vertList[i].find(" ", vertList[i].find("v") + 2, 10)
                ).c_str()
            ),
            y = (float)atof(
                vertList[i].substr(
                    vertList[i].find(" ", vertList[i].find("v") + 4, 10) + 1,
                    vertList[i].find(" ", vertList[i].find("v") + 13, 10)
                ).c_str()
            ),
            z = (float)atof(
                vertList[i].substr(
                vertList[i].find(" ", vertList[i].find("v") + 13, 10) + 1,
                vertList[i].find(" ", vertList[i].find("v") + 23, 10)
                ).c_str()
            ),
            ::Vector3f(0.0f,0.0f,0.0f),::Vector3f(0.0f,0.0f,0.0f),::Vector3f(0.0f,0.0f,0.0f)
        );

        vert3fList.push_back(
            tvert
        );

I have tried defining a default constructor myself so in the .h I put

Vertice3f();

and in the cpp

Vertice3f::Vertice3f(){
x = 0.0f;
y = 0.0f;
z = 0.0f;
uv = Vector3f(0.0f,0.0f,0.0f);
normal = Vector3f(0.0f,0.0f,0.0f);
color = Vector3f(0.0f,0.0f,0.0f);
}

So, I'm not sure why it can't find a default constructor or how to appease the compiler. I'm sure it's user error because the compiler probably knows what it's doing. Any help is greatly appreciated, I will answer any other questions you have, just ask.

解决方案

I'd guess that the missing default constructor is the default constructor of Vector3f class, not of Vertice3f class. Your constructor of Vertice3f attempts to default-construct its Vector3f members, which leads to the error.

This is why your attempts to provide default constructor for Vertice3f don't change anything. The problem lies, again, with Vector3f.

To fix it either provide all necessary default constructors (assuming it agrees with your design), or rewrite the constructor of Vertice3f by using initializer list instead of in-body assignment

Vertice3f::Vertice3f(float _x, float _y, float _z, 
                     Vector3f _uv, Vector3f _normal, Vector3f _color) :
    x(_x), y(_y), z(_z), uv(_uv), normal(_normal), color(_color)
  {}

This version no longer attempts to default-construct anything. And using initializer list instead of in-body assignment is a good idea in any case.

这篇关于没有默认构造函数存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆