2011-11-08 25 views
4
Pickup::Pickup(std::vector<Agent, std::allocator<Agent>> &agents)" provides no initializer for: 

我得到了一些我的構造函數的錯誤,令人討厭的是錯誤在它告訴我什麼我沒有提供初始化之前突然結束。另外,我非常確定我實際上爲所有需要它的東西提供初始化。任何人都可以對此有所瞭解嗎?不提供用於.....的初始化程序?什麼?

#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; 
    } 

頭:

#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 
+0

您確定錯誤信息確實突然結束,而不是繼續到第二行嗎? –

+0

你確定你需要一個'vector &'會員嗎?另外,如果這是在一個IDE中列出兩個窗口中的錯誤和編譯器輸出,它可能只是抓住錯誤的第一行。但Henrik的回答可能是正確的。 – ssube

回答

4

會員agents未初始化。

+0

我真是愚蠢至極!謝謝。 – SirYakalot

0

構造函數不初始化屬性'agents'。它必須在構造函數初始化列表中進行初始化。初始化初始化列表中的所有屬性(count,index,nodeIndex,...)而不是分配值也是一種很好的做法。

順便說一下,爲什麼'agents'屬性受到保護?它應該是私人的。您不應允許派生類直接修改屬性。它避免了混亂的代碼。

乾杯。

相關問題