类没有默认构造函数,但我已经声明了一个

时间:2013-03-31 00:53:50

标签: c++ default-constructor

我在代码中出现了两次, 我不确定为什么抱怨 我有一个头文件“Scene.h”:

#pragma once

#include <iostream>
#include <string>

#include "Image.h"
#include "InteractiveObject.h"

using namespace std;

    class Scene
    {
    public:
        int id;
        string title;
        Image* backgroundImage;
        InteractiveObject interactiveObjects[ 1 ];

        D3DXVECTOR3 pos;

        Scene( int id_, string title_, Image* backgroundImage_ )
            : 
            id( id_ ),
            title( title_ ),
            backgroundImage( backgroundImage_ )
        {
            this->pos.x = 0.0f;
            this->pos.y = 0.0f;
            this->pos.z = 0.0f;
        }
    };

我有另一个名为“InteractiveObject.h”的文件:

#pragma once

#include <iostream>
#include <string>

#include "Image.h"
enum { CHARACTER, OBJECT };

class InteractiveObject
{
public:
    int id;
    int type;

    float x;
    float y;
    float z;

    D3DXVECTOR3 pos;

    string title;
    Image* theImage;
    InteractiveObject( int id_, string title_, Image* theImage_, int type, float x, float y, float z )
        : 
        id( id_ ),
        title( title_ ),
        theImage( theImage_ )
    {
        this->pos.x = x;
        this->pos.y = y;
        this->pos.z = z;
    }
};

我的intellisense抱怨说:

  

错误1错误C2512:'InteractiveObject':没有合适的默认值   构造函数可用c:\ users \ james \ documents \ visual studio   2012 \ projects \ game \ game \ scene.h 26 1游戏

任何考虑两者的想法都有默认构造函数?

EDIT :: ----------

好的,谢谢大家,我可以看到,如果我这样做:;

InteractiveObject( int id_, string title_, Image* theImage_, int type_, float x_, float y_, float z_ )
        : 
        id( id_ ),
        title( title_ ),
        theImage( theImage_ ),
        type( type_ ),
        x( x_ ),
        y( y_ ),
        z( z_ )
    {

问题消失了......我不确定这是最好的方式,我肯定会在一会儿后回来哭。你能否投票给出你认为最简洁的答案?

我目前的要求是让一个场景包含在我的游戏构造函数中声明的一定数量的交互式对象,并迭代以绘制到屏幕上:

“game.cpp”:

Game::Game( HWND hWnd, Mouse &mouse  )
    :
    gfx( hWnd ),
    mouse( mouse )
{
    gfx.d3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE);

    //--------------Scene 0---------------
    scenes[ 0 ] = Scene( 0, "La barra de funk", new Image( gfx, "Images/Scene/Area/Bar/Background.jpg", 1024, 768, FALSE ) );
    scenes[ 0 ].interactiveObjects[ 0 ] = new InteractiveObject( 0, "la rockola de muse", new Image( gfx, "Images/Scene/Area/Bar/InteractiveObjects/Jukebox.png", 428, 586, TRUE ), OBJECT, 300.0f, 200.0f, 1.0f );
};

Game::~Game()
{
    // Delete scenes array

    /*for( int a = 0; a < sizeof( scenes ) / sizeof( Scene ); a++ )
    {
        scenes[ a ] = NULL;
        delete scenes[ a ];
    }*/
};

void Game::Go()
{
    gfx.Begin();
    ComposeFrame();
    gfx.End();
    gfx.Present();
};

void Game::ComposeFrame()
{
    for each ( Scene currentScene in scenes )
    {
        currentScene.backgroundImage->sprite->Begin( D3DXSPRITE_ALPHABLEND );
        currentScene.backgroundImage->sprite->Draw( currentScene.backgroundImage->gTexture, NULL, NULL, &currentScene.pos, 0xFFFFFFFF );
        currentScene.backgroundImage->sprite->End();

        for each ( InteractiveObject currentInteractiveObject in currentScene.interactiveObjects )
        {
            currentInteractiveObject.theImage->sprite->Begin( D3DXSPRITE_ALPHABLEND );
            currentInteractiveObject.theImage->sprite->Draw( currentScene.backgroundImage->gTexture, NULL, NULL, &currentInteractiveObject.pos, 0xFFFFFFFF );
            currentInteractiveObject.theImage->sprite->End();
        }       
    }
}

5 个答案:

答案 0 :(得分:3)

默认构造函数不需要传递任何参数,因为它不接受任何参数,或者所有命名参数都有默认值。从您的代码中,InteractiveObject类'构造函数需要参数,但是当您调用时,它也会在Scene类中构建

InteractiveObject interactiveObjects[ 1 ];

您可以动态创建它(使用new),或者向InteractiveObject类添加非参数构造函数。

答案 1 :(得分:2)

您的类具有非默认构造函数(即带参数的构造函数)。它们没有默认构造函数,它们采用零参数。

将对象放入C数组的要求之一是对象是默认构造的。否则,编译器无法构造它们 - 当您声明一个数组时,编译器需要默认构造数组中的所有实例(无法将构造函数参数传递给数组中的对象)。 / p>

要解决此问题,您需要确保永远不要在数组中使用这些类(例如,如果您愿意,可以通过指针数组替换所有对象数组,但随后内存管理变得更难)。或者,只需添加简单的默认构造函数,将所有类成员初始化为合理的值。

答案 2 :(得分:0)

自:

InteractiveObject interactiveObjects[ 1 ];

它将调用类default constructor的{​​{1}}。但是,您提供的构造函数不符合InteractiveObject,因为:

default constructor

因此,构造函数不能被视为1. it has arguments 2. all of its arguments does not have default values. ,因为在不提供任何参数的情况下无法调用它。因此,编译器无法为您生成一个default constructor,因此您有错误。

答案 3 :(得分:0)

InteractiveObject( int id_, string title_, Image* theImage_, int type, float x, float y, float z )

您需要将这些参数放在对象声明中。

答案 4 :(得分:-1)

下面:

InteractiveObject interactiveObjects[ 1 ];

您正在声明一个InteractiveObject数组,但InteractiveObject没有零参数默认构造函数来创建对象。

相反,您需要在运行时使用new实例化它。