C++ C++;::Set value函数在函数内部设置,但在Get函数之后,设置值消失

C++ C++;::Set value函数在函数内部设置,但在Get函数之后,设置值消失,c++,debugging,stdvector,C++,Debugging,Stdvector,希望我错过了一些非常明显的东西: 尝试使用一些向量作为容器,使用资产属性修饰符(类)设置资产(类)的值。这似乎在函数调用期间起作用,但在set调用之后进行get调用以确认时,就好像set调用没有发生一样。请参阅下面的代码和输出: asset.h文件: namespace assets { class Asset { friend class AssetPropertyModifier; std::vector<properties::AssetPropertyDouble>

希望我错过了一些非常明显的东西:

尝试使用一些向量作为容器,使用资产属性修饰符(类)设置资产(类)的值。这似乎在函数调用期间起作用,但在set调用之后进行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
以下是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
正如您在上面看到的,GET返回了5.5,SET声明它更新到了4.4,但是下面的GET仍然显示了5.5

我可以提供的另一个帮助解决此问题的方法是,如果我将属性设置为public,以便可以直接从main访问资产属性以进行测试,那么它将正确更新:

//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;

有人知道为什么SET函数不在上面吗?提前感谢您的帮助

您传递的是资产的副本,而不是对资产的引用,因此您实际上只是更新副本,而不是实际的副本。您需要改为通过引用传递:

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

void AssetPropertyModifier::SetPropertyValue(Asset&a,//也许我看错了,但是当你设置新的值时,看起来你传递的是你的资产的副本,而不是实际的资产。你试过通过引用传递吗?@EastonBornemeier-是的,完全正确。非常感谢你帮我捕捉到它。看了太久,我知道我错过了一些明显的东西。a感谢您的时间。没问题!我把它作为一个答案加上了。如果您能把它标记正确,我将非常感激!完成了。再次感谢
------------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
//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
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