2017-08-07 115 views
0

希望我失去了一些東西超明顯與此:C++ ::設定值函數集內的功能,但隨後獲得功能設定值消失了

試圖與資產設置資產(類)的值屬性修飾符(類)使用一些向量作爲容器。這似乎在函數調用期間起作用,但是當使用get調用跟隨set調用進行確認時,就好像調用沒有發生。見代碼和輸出如下:

的asset.h文件:

namespace assets { 
class Asset 
{ 
    friend class AssetPropertyModifier; 
    std::vector<properties::AssetPropertyDouble> asset_property_double_; 
    enum property_id_ { ZERO, USHORT, FLOAT, INT, UINT, DOUBLE, VEC3, STRING, 
     BOOL}; 
public: 
    void AddProperty(std::string property_name, double property_value); 
};//class Asset 
}//namespace assets 

方法addProperty工作得很好,所以我省略這一點,在.cpp不相關。只需向矢量中push_back以添加值,以及一些索引跟蹤。

在資產屬性頭:

namepsace properties { 
struct AssetPropertyDouble 
{ 
private: 
    friend class AssetPropertyModifier; 
    double real_number_value_; 
public: 
    AssetPropertyDouble(double d) : real_number_value_(d) {} 
};//struct AssetPropertyDouble 
}//namespace proeprties 

這裏是APM頭:

namespace assets { 
class Asset; 
class AssetPropertyModifier 
{ 
    int LookUpVectorIndex(Asset a, int type , int index); 
    int LookUpNameIndex(Asset a, std::string s); 
public: 
    double GetPropertyValue(Asset a, std::string property_name, 
     double not_used_just_an_overloader); 
    void SetPropertyValue(Asset a, std::string property_name, 
     double new_double_value); 
};//class AssetPropertyModifier 
}//namespace assets 

在APM .cpp文件:

namespace assets { 

//LookUpNameIndex definition 
//LookUpVectorIndex defintion 

double AssetPropertyModifier::GetPropertyValue(Asset a, 
           std::string property_name, 
           double not_used_just_an_overloader) 
{ 
    //Using Verbose output to debug: 
    std::cout << "------------GET-------------------" << std::endl; 
    int name_index = LookUpNameIndex(a, property_name); 
    std::cout << "names vector index is " << name_index << std::endl; 
    int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index); 
    std::cout << "double property vector index is " << double_index << 
     std::endl; 
    double property_value = 
     a.asset_property_double_.at(double_index).real_number_value_; 
    std::cout << property_name << " are equal to " << property_value << 
     std::endl; 
    return property_value; 
} 

void AssetPropertyModifier::SetPropertyValue(Asset a, 
          std::string property_name, 
          double new_double_value) 
{ 
    //Using Verbose output to debug: 
    std::cout << "------------SET-------------------" << std::endl; 
    int name_index = LookUpNameIndex(a, property_name); 
    std::cout << "names vector index is " << name_index << std::endl; 
    int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index); 
    std::cout << "double vector index is " << double_index << std::endl; 
    a.asset_property_double_.at(double_index).real_number_value_ = 
     new_double_value; 
    std::cout << property_name << " was changed to " << 
     a.asset_property_double_.at(double_index).real_number_value_ 
     << std::endl; 
}//SetPropertyValue 

}//namespace assets 

後來,當我使用這個原型,我得到以下事情發生:

從main.cpp中例如:

std::string some_name = "Property Name"; 
double some_initial_value = 5.5; 
double some_new_value = 4.4; 
assets::AssetPropertyModifier apm; 
assets::Asset my_asset; 

main { 
    my_asset.AddProperty(some_name, some_initial_value); 
    std::cout << "Current value = " << apm.GetPropertyValue(my_asset, 
     some_name , _DOUBLE_) 
     << std::endl; 

    apm.SetPropertyValue(my_asset, some_name , some_new_value); 

    std::cout << "Current value = " << apm.GetPropertyValue(my_asset, 
     some_name , _DOUBLE_) 
     << std::endl; 
} 

這是所得到的輸出:

------------GET------------------- 
names vector index is 0 
double property vector index is 0 
Property Name are equal to 5.5 
Current value = 5.5 
------------SET------------------- 
names vector index is 0 
double vector index is 0 
Property Name was changed to 4.4 
------------GET------------------- 
names vector index is 0 
double property vector index is 0 
Property Name are equal to 5.5 
Current value = 5.5 

正如可以看到GET上面返回5.5和一組權利要求它更新爲4.4但隨後以下GET節目5.5仍然。

還有一件事我可以提供幫助排除這是,如果我設置的屬性是公共的,這樣我可以從主直接訪問資產屬性測試起見,可以正確地更新:

//hard SET with public access to the vector and the property struct value 
my_asset.asset_property_double_.at(0).real_number_value_ = 3.3; 
//hard GET with public access: 
std::cout << my_asset.asset_property_double_.at(0).real_number_value_ << 
    std::endl; 
//GET through APM but with public values still enabled: 
std::cout << "Current value = " << apm.GetPropertyValue(my_asset, some_name, 
    _DOUBLE_) 
    << std::endl; 

即輸出爲:

3.3 
------------GET------------------- 
names vector index is 0 
double property vector index is 0 
Property Name are equal to 3.3 
Current value = 3.3 

有誰知道爲什麼SET功能上面沒有貼?預先感謝任何幫助!

+3

也許我誤解了它,但它看起來像是在設置新值時傳遞資產副本而不是實際資產。你有沒有嘗試過參考? –

+0

@EastonBornemeier - 是的,這是完全正確的。非常感謝你爲我抓到它。看着它太長,知道我錯過了一些明顯的東西。欣賞你的時間。 – Necromon

+0

沒問題!我將其添加爲答案。如果你能標記正確的話,我會非常感激! –

回答

1

您傳遞的是資產的副本,而不是對資產的引用,所以實際上只是更新副本,而不是實際副本。您需要通過引用來代替:

void AssetPropertyModifier::SetPropertyValue(Asset& a,  //<----added '&' 
          std::string property_name, 
          double new_double_value) 
{ 
    //Using Verbose output to debug: 
    std::cout << "------------SET-------------------" << std::endl; 
    int name_index = LookUpNameIndex(a, property_name); 
    std::cout << "names vector index is " << name_index << std::endl; 
    int double_index = LookUpVectorIndex(a, a.DOUBLE, name_index); 
    std::cout << "double vector index is " << double_index << std::endl; 
    a.asset_property_double_.at(double_index).real_number_value_ = 
     new_double_value; 
    std::cout << property_name << " was changed to " << 
     a.asset_property_double_.at(double_index).real_number_value_ 
     << std::endl; 
}//SetPropertyValue