no proporciona inicializador para .....? ¿Qué? [cerrado]

Pickup::Pickup(std::vector<Agent, std::allocator<Agent>> &agents)" provides no initializer for:

I'm getting this error for a number of my constructors, annoyingly the error ends abruptly just before it tells me what I'm not providing an initialiser for. Additionally, I'm pretty certain I am in fact providing initialization for everything that needs it. Can anyone shed any light on this?

#include "Pickup.h"

    Pickup::Pickup(vector<Agent>& agents)
        : GameObject(), TOLERANCE(0.1f)
    { // this brace is underlined and is where the error occurs.

        xRotation = D3DXVECTOR3(0.005f, 0.005f, 0.04f);

        count = 0;

        index = -1;

        nodeIndex = -1;

        isPresent = true;
    }

    void Pickup::Regenerate()
    {
        //when the pickup gets picked up, start a countdown
        count++;

        if (count == 300)
        {
            isPresent = true;
            count = 0;
        }
    }

    //void Pickup::addAmmo(int agentIndex) { }

    void Pickup::Update()
    {
        Rotation(Rotation() + xRotation);

        for (unsigned int i = 0; i < agents.size(); i++)
        {
            //if (D3DXVec3Length(agents[i].MainLegsPosition() - Position()) < TOLERANCE && isPresent)//right here
            //{                   
            //    addAmmo(agents[i].Index());
            //    isPresent = false;
            //}
        }

        if (isPresent == false)
        {
            Regenerate();
        }
    } 

    /*void Pickup::Draw(D3DXMATRIX matView, D3DXMATRIX matProjection, ID3D10Effect* effect)//right here
    {
        if (isPresent == true)
        {
            Draw(matView, matProjection, effect);
        }
    }*/

    // getters
    int Pickup::Index()
    {
        return index;
    }

    int Pickup::NodeIndex() 
    {
        return nodeIndex;
    }

    bool Pickup::IsPresent() 
    { 
        return isPresent;
    }

 /*       vector<Agent>& Pickup::Agents()
    {
        return agents;
    }*/

    // setters
    void Pickup::Index(int index)
    {
        this->index = index;
    }

    void Pickup::NodeIndex(int nodeIndex)
    {
        this->nodeIndex = nodeIndex;
    }

encabezamiento:

#ifndef PICKUP_H
#define PICKUP_H

#include "gameObject.h"
#include "Agent.h"
#include <vector>

using namespace std;

class Pickup : public GameObject
{
private:

    int count;

    int index;

    int nodeIndex;

    bool isPresent;

    D3DXVECTOR3 xRotation;

    const float TOLERANCE;

    void Regenerate();

protected:

    vector<Agent>& agents;

public:

    Pickup(vector<Agent>& agents);

    virtual void addAmmo(int agentIndex);

    void Update();

    void Draw();

    // getters
    int Index();

    int NodeIndex();

    bool IsPresent();

    // setters
    void Index(int index);

    void NodeIndex(int nodeIndex);
};

#endif

preguntado el 08 de noviembre de 11 a las 10:11

Are you sure the error message does end abruptly, rather than continuing onto a second line? -

¿Estás seguro de que necesitas un vector<Agent> & member? In addition, if this is in an IDE that lists error and compiler output in two windows, it may just be grabbing the first line of the error. Henrik's answer is probably correct, though. -

2 Respuestas

Miembro agents no está inicializado.

respondido 08 nov., 11:14

how incredibly stupid of me! thank you. - Dollarslice

The constructor does not initialize the attribute 'agents'. It has to be initialized in the constructor initialization list. It is also a good practice to initialize all the attributes in the initialization list (count, index, nodeIndex, ...) instead of assigning them values.

And by the way, why is the 'agents' attribute protected? It should be private. You should not allow direct modification of attributes by derived classes. It avoids messy code.

Saludos.

respondido 08 nov., 11:14

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.