我有一个函数,我将指针传递给unsigned char的向量。
有人可以告诉我如何获得函数中的一个值吗?
double CApp::GetCost(unsigned char *uBytes)
{
unsigned char iHP;
iHP=uBytes[49]; //this does not work
}
编辑: 对不起,我首先想到的是我应该简化我的代码,但我认为太多可能会出错。现在这是真正的宣言:
// ---------------------------------------
struct ByteFeature
{
unsigned char Features[52];
};
class clsByteFeatures : public CBaseStructure
{
private:
vector<ByteFeature> m_content;
protected:
virtual void ProcessTxtLine(string line);
public:
vector<ByteFeature> &Content();
void Add(ByteFeature &bf);
};
vector<ByteFeature> &clsByteFeatures::Content()
{
return m_content;
}
这就是我使用它的方式:
dblTargetCost = GetCost(m_ByteFeatures.Content()[iUnitID].Features);
另一个问题: 简单地传递这样的矢量会不好?
double CApp::GetCost(vector<unsigned char> &uBytes)
{
//...
}
答案 0 :(得分:3)
Would it be bad to simply pass the vector like this?
double CApp::GetCost(vector<unsigned char> &uBytes)
不是通过引用传递它的更好方法。但是,如果您不希望修改uBytes
,则可能需要添加const限定符。
double CApp::GetCost(const vector<unsigned char> &uBytes)
{
try
{
unsigned char iHP = uBytes.at(49);
//...
}
catch(std::exception& e)
{
// process e
}
//...
}
编辑:
在您发布新帖后,我觉得您只需要返回对m_content元素的引用,然后将引用传递给GetCost
函数
ByteFeature& clsByteFeatures::operator[](int i) { return m_content.at(i); }
double GetCost(const ByteFeature& bf)
{
std::cout << bf.Features[49]; << std::endl;
return 0.0;
}
然后你打电话:
GetCost(m_ByteFeatures[iUnitID]);