- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我已经使用这个网站一段时间了,到目前为止,我从来不需要提出新问题(找到了到目前为止我需要的所有答案)。
我需要将多个对象 push_back 到一个 vector 中,但 VS 抛出一个错误(这可能是由于堆损坏,这表明 PVSS00DataGate.exe 或它加载的任何 DLL 中存在错误)我似乎无法为自己锻炼。
这就是我正在尝试做的,它可以将第一个对象推回 vector 中,但是当我尝试将第二个对象推回时发生错误。
class HWObject{}
void DataLogger::WriteNewMessages()
{
unsigned int iBattery = 0;
unsigned int iSignal = 0;
TimeVar tTimeStamp;
// I want to store all HWObjects in a temporary vector (loggerData)
std::vector<HWObject> loggerData;
CharString strElement;
strElement.format( "batteryCondition.value" );
SendOneValuePVSS( (const char *)strElement, iBattery, tTimeStamp );
strElement.format( "signalStrength.value" );
SendOneValuePVSS( (const char *)strElement, iSignal, tTimeStamp );
}
void DataLogger::SendOneValuePVSS(const char *szElementName, double dValue, TimeVar, &tValue)
{
HWObject obj;
obj.setOrgTime(tValue); // Current time
obj.setTimeOfPeriphFlag();
CharString address;
address = strID;
address += ".";
address += szElementName;
obj.setAddress(address);
loggerData.reserve( loggerData.size() + 1 );
loggerData.push_back( obj );
obj.cutData();
}
dataLogger 在
中声明class DataLogger
{
public:
std::vector<HWObject> loggerData;
...
}
这是 HWObject 类,我不想用代码让您不知所措。
public:
/** Default constructor*/
HWObject();
/** Constructor, which sets the periphAddr and transformationType.
* @param addressString address of the HW object
* @param trans type of transformation
*/
HWObject(const char* addressString, TransformationType trans);
/** Destructor. If the date pointer is not NULL, it is deleted.
*/
virtual ~HWObject();
/** Creates a new HWObject
* This function returns an empty HWObject, no properties are duplicated or copied!
* @classification public use, overload, call base
*/
virtual HWObject * clone() const;
/** Reset all pvss2 relevant parts of the HWObject. when overloading this member
* don't forget to call the basic function!
* @classification public use, overload, call base
*/
virtual void clear();
/** Gets pointer to data
* @return pointer to data
*/
const PVSSchar * getDataPtr() const { return dataPtr; }
/** Gets the data buffer pointer
* @return data buffer pointer
*/
PVSSchar * getData() { return dataPtr; }
/** Cut the data buffer out of the HWObject.
* This function is used to avoid the deletion
* of the data buffer, when a new pointer is set using
* setData() or the HWObject is deleted.
* @return pointer to the data of the HWObject
*/
PVSSchar * cutData();
/** Get the data buffer lenght
* @return length of the data buffer
*/
PVSSushort getDlen() const { return dataLen; }
/** Set ptr to the data buffer, pointer is captured.
* The actual data pointer in the HWObject is deleted,
* if it is not NULL. To avoid the deletion use cutData()
* in order to cut out the pointer.
* @param ptr pointer to new data
*/
void setData(PVSSchar *ptr);
/** Set the data length
* @param len length to be set
*/
void setDlen(const PVSSushort len) { dataLen = len; }
/** Get the periph address
* @return periph address string
*/
const CharString & getAddress() const { return address; }
/** Get the transformation type
* @return type of transformation
*/
TransformationType getType() const { return transType; }
/** Set the transformation type
* @param typ type of transformation for setting
*/
void setType(const TransformationType typ) { transType = typ; }
/** Get the subindex
* @return subindex
*/
PVSSushort getSubindex() const { return subindex; }
/** Set the subindex
* @param sx subindex to be set
*/
void setSubindex( const PVSSushort sx) { subindex = sx; }
/** Get the origin time
* @return origin time
*/
const TimeVar& getOrgTime() const { return originTime; }
/** Get the origin time
* @return oriin time
*/
TimeVar& getOrgTime() { return originTime; }
/** Set the origin time
* @param tm origin time to be set
*/
void setOrgTime(const TimeVar& tm) { originTime = tm; }
/** Get HWObject purpose
* @return objSrcType
*/
ObjectSourceType getObjSrcType() const { return objSrcType; }
/** Set HWObject purpose
* @param tp objSrcType
*/
void setObjSrcType(const ObjectSourceType tp) { objSrcType = tp; }
/** Get number of elements in data buffer
* @return number of elements in data buffer
*/
PVSSushort getNumberOfElements() const { return number_of_elements; }
/** Set number of elements in data buffer
* @param var number of elements in data buffer
*/
void setNumberOfElements(const PVSSushort var) { number_of_elements = var; }
/** Prints the basic HWObject information on stderr.
* in case of overloading don't forget to call the base function!
* @classification public use, overload, call base
*/
virtual void debugPrint() const;
/** Prints th basic HWObject info in one CharString for internal debug DP.
* in case of overloading call base function first, then append specific info
* @classification public use, overload, call base
*/
virtual CharString getInfo() const;
/** Set the periph address
* @param adrStr pointer to address string
*/
virtual void setAddress(const char *adrStr);
/** Set the additional data (flag, orig time, valid user byte,etc)
* @param data aditional flags that be set
* @param subix subindex, use subix 0 for setting values by default
*/
virtual void setAdditionalData(const RecVar &data, PVSSushort subix);
/** Set the 'origin time comes from periph' flag
*/
void setTimeOfPeriphFlag()
{
setSbit(DRV_TIME_OF_PERIPH);
}
/** Check whether time comes from periph
* @return PVSS_TRUE if the time is from perip
*/
PVSSboolean isTimeFromPeriph() const
{
// If isTimeOfPeriph is set, it must be valid
return getSbit(DRV_TIME_OF_PERIPH);
}
/** Set the flag if you want to receive callback if event has answered the value change
*/
void setWantAnswerFlag()
{
setSbit(DRV_WANT_ANSWER);
}
/** Get the status of the 'want answer, flag
*/
PVSSboolean getWantAnswerFlag() const
{
// If isTimeOfPeriph is set, it must be valid
return getSbit(DRV_WANT_ANSWER);
}
/** Set the user bit given by input parameter.
* Status bits defined by the enum DriverBits
* @param bitno bit number
* @return PVSS_TRUE if bit was set
*/
PVSSboolean setSbit(PVSSushort bitno)
{
return (status.set(bitno) && status.set(bitno + (PVSSushort)DRV_VALID_INVALID - (PVSSushort)DRV_INVALID));
}
/** Clear the user bit given by input parameter
* @param bitno bit number
* @return PVSS_TRUE if bit was cleared
*/
PVSSboolean clearSbit(PVSSushort bitno)
{
return (status.clear(bitno) && status.set(bitno + (PVSSushort)DRV_VALID_INVALID - (PVSSushort)DRV_INVALID));
}
PVSSboolean isValidSbit(PVSSushort bitno) const
{
return status.get(bitno + (PVSSushort)DRV_VALID_INVALID - (PVSSushort)DRV_INVALID);
}
/** Check any bit
* @param bitno bit number
* @return status of the bit on bitno position
*/
PVSSboolean getSbit(PVSSushort bitno) const {return status.get(bitno);}
/** Clear all status bits
* return status of clear all
*/
PVSSboolean clearStatus() {return status.clearAll();}
/** Get the status of this object
* @return bit vector status
*/
const BitVec & getStatus() const {return status;}
/** Set status of the bit vector
* @param bv deference to bit vector to be set as status
*/
void setStatus(const BitVec &bv) {status = bv;}
/** Set a user byte in the status.
* @param userByteNo number of user byte range 0..3
* @param val value to set
* @return PVSS_TRUE execution OK
* PVSS_FALSE in case of error
*/
PVSSboolean setUserByte (PVSSushort userByteNo, PVSSuchar val);
/** Reads a user byte from the status.
* @param userByteNo number of user byte range 0..3
* @return the requested user byte
*/
PVSSuchar getUserByte (PVSSushort userByteNo) const;
/** Check validity of user byte.
* @param userByteNo number of user byte range 0..3
* @return PVSS_TRUE user byte is valid
* PVSS_FALSE user byte is not valid
*/
PVSSboolean isValidUserByte(PVSSushort userByteNo) const;
/** Format status bits into a string
* @param str status bits converted to string
*/
void formatStatus (CharString & str) const ;
// ------------------------------------------------------------------
// internal ones
/** Read data from bin file
* @param fp file handle
*/
virtual int inFromBinFile( FILE *fp );
/** Write data to bin file
* @param fp file handle
*/
virtual int outToBinFile( FILE *fp );
/** Set data length
* @param dlen data length
*/
void setDlenLlc (PVSSushort dlen) {dataLenLlc = dlen;}
virtual void updateBufferLlc (HWObject * hwo, int offset1 = 0);
virtual int compareLlc (HWObject * hwo, int offset1 = 0, int offset2 = 0, int len = -1);
/** Get dataLenLlc
* @return dataLenLlc
*/
PVSSushort getDlenLlc () const {return dataLenLlc;}
/** Function to delete the data buffer; overload if special deletion must be done
*/
virtual void deleteData ();
/** Set HW identifier
* @param id hw identifier to be set
*/
void setHwoId(PVSSulong id) {hwoId = id;}
/** Get HW identifier
* @return hw identifier
*/
PVSSulong getHwoId() const {return hwoId;}
protected:
/// the dynamic data buffer
PVSSchar* dataPtr;
/// the data buffer len
PVSSushort dataLen;
/// the pvss2 periph address string
CharString address;
/// the start subix for the data buffer
PVSSushort subindex;
/// the datatype of the data in the buffer (i.e. transformationtype)
TransformationType transType;
/// the time of income, normally set by the constructor
TimeVar originTime;
/// the purpose of this HWObject
ObjectSourceType objSrcType;
/// the number of elements in the data buffer, used for arrays and records
PVSSushort number_of_elements; // fuer array!!!
/// the user bits of the original config
BitVec status;
private:
PVSSushort dataLenLlc;
PVSSulong hwoId;
};
最佳答案
您没有显示重要部分。我猜 HWObject
有动态分配的内存,并没有实现三的规则(复制构造函数、赋值运算符和析构函数)。但这只是一个猜测。 (除非你使用像引用计数这样的特殊技术或智能指针,复制必须做深复制,赋值应该可能使用交换习语。)
此外,在之前保留 size() + 1
没有意义推回
。
关于带有类对象的 C++ vector push_back,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7513595/
这个问题在这里已经有了答案: Passing by value vs const & and && overloads (3 个答案) 关闭 8 年前。 为什么push_back的函数签名如下? v
代码如下: std::vector s; s.push_back(~Dword(0)); 什么是~Dword?它是如何工作的? 最佳答案 Dword 这里是一个数字类型(可能是 DWORD 的类型别名
我正在测试 C++ 中推回对象与推回对象指针到 Vector 之间的性能差异。 我在 Stackoverflow 和其他文章中读到,您应该避免向后推指针,除非您必须这样做... 但是,我意识到推回指针
关闭。这个问题是not reproducible or was caused by typos .它目前不接受答案。 这个问题是由于错别字或无法再重现的问题引起的。虽然类似的问题可能是on-topi
我有一个很大的 .txt 文件,需要加载并存储在 vector 中。该文件大小约为 5MB,500 000 行,每行约 10-20 个字符,以 '\n' 分隔。我正在使用以下示例代码对读取整个文件所需
这个问题在这里已经有了答案: What is object slicing? (18 个答案) 关闭 9 年前。 我有这两个类: class A { public: A(); vir
在我当前的代码中,我想将新的 DrawObjects 插入到我创建的 vector 中, std::vector 对象; 有什么区别: objects.push_back(DrawObject(nam
我将 Cygwin 与 GCC 一起使用,最终我想将字 rune 件读入字符 vector ,并使用此代码 #include #include #include using namespace
以下代码创建一个临时对象 A 并将其推送到一个 vector 中。 在 push_back 期间删除复制构造函数并调用移动构造函数。我不确定这段代码的设计是否正确,肯定存在内存泄漏。 #include
我知道push_back可以抛出bad_alloc异常,并且如果没有try catch block ,则调用析构函数是不正确的。如果有任何push_back抛出并且它不在try catch block
正如 Scott Meyers 所指出的(http://channel9.msdn.com/Events/GoingNative/2013/An-Effective-Cpp11-14-Sampler
我正在写一个 push_back将临时容器添加到另一个容器的函数。 并且它应该在使用 push_back 之前调整或保留容器(如果两者都可用,它应该更喜欢保留而不是调整大小) 当前代码是: names
这个问题在这里已经有了答案: Create an array when the size is a variable not a constant (2 个答案) 关闭 3 年前。 我认为我在代码中
代码: // test2.cpp #include #include struct test_class { test_class() = default; test_class(
我试图在 forloop 的 vector 中推回一个 const char*。 char_temp 是一个名为 segment 的结构,而 chars_temp 是一个结构 vector 。请参阅下
在 C++ 入门书第 (3) 章中,有以下 for 循环将 vector 中的元素重置为零。 vector ivec; //UPDATE: vector declaration for (vector
class A { public: A():a(0) {} A(int x):a(x) { coutve
我正在尝试使用 Visual Leak Detector 查找内存泄漏。它告诉我 m_neighbors.push_back(ent);导致泄漏。 (简短调用堆栈 = NeighborCalculat
我们正在制作一个包含棋盘游戏信息(名称、年份、分数)的列表。我们从 .csv 文件中扫描信息,根据该信息创建一个结构,然后将该结构添加到列表中。我们一直这样做,直到文档阅读完毕。问题是列表的 push
以下代码是将“非重叠”的 TablePath 从 vector v 移动到 vector u。我在“u.push_back(*it1);”行遇到段错误。我没有复制对象(而是只复制对象的指针)所以我相信
我是一名优秀的程序员,十分优秀!