2

我的代码中出现了两次这种情况,我不确定为什么它抱怨我有一个头文件“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;
    }
};

我的智能感知抱怨有:

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

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

编辑:: - - - - -

好的,感谢这里的每个人,如果我这样做,我可以看到:;

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_ )
    {

问题消失了。我不确定这是不是最好的方法,我肯定会在片刻后哭着回来。您能否投票选出您认为最简洁的答案?

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

“游戏.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();
        }       
    }
}
4

5 回答 5

3

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

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

To fix this, you either need to make sure you never use those classes in arrays (e.g. you can replace all object arrays by arrays of pointers, if you wish, but then memory management becomes harder). Alternatively, just add simple default constructors which initialize all of the class members to reasonable values.

于 2013-03-31T00:58:23.450 回答
3

A default constructor doesn't require any arguments passed to it, either because it doesn't take any arguments, or there are default values for all of the named parameters. From your code, the InteractiveObject class' constructor required parameters, but it's also constructed in your Scene class when you call

InteractiveObject interactiveObjects[ 1 ];

You could either dynamically create this (using new), or add a non-argument constructor to the InteractiveObject class.

于 2013-03-31T01:00:14.120 回答
0

自从:

InteractiveObject interactiveObjects[ 1 ];

它将调用default constructorInteractiveObject。但是,您提供的构造函数不符合以下条件default constructor

1. it has arguments
2. all of its arguments does not have default values.

因此构造函数不能被视为default constructor,从某种意义上说,它不能在不提供任何参数的情况下被调用。出于这个原因,编译器无法default constructor为你生成一个,所以你有那个错误。

于 2013-03-31T00:58:18.007 回答
0
InteractiveObject( int id_, string title_, Image* theImage_, int type, float x, float y, float z )

You need to put these arguments on object declaration.

于 2013-03-31T00:59:29.640 回答
-1

这里:

InteractiveObject interactiveObjects[ 1 ];

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

相反,您需要在运行时使用new.

于 2013-03-31T00:58:03.520 回答