Έχω μια κλάση iterator. Ας το ονομάσουμε PIterator
εδώ. Α MessageBuffer
είναι επαναλαμβάνεται και γίνεται εξάγεται σωστά, εκτός αν το nSizeOfMessage
συν όπου η iterator επισημαίνει επί του παρόντος να είναι ίσο με το μέγεθος του ολόκληρο το μήνυμα (θέση σωστή, δείκτης μία πολύ μεγάλο).
Αν μπορώ να ελέγξω για το τελευταίο στοιχείο και μείωση κατά ένα, θα πρέπει να λειτουργεί. Αν και φαίνεται να είναι ένα «λάθος δρόμο» για μένα. Ναι, δεν είμαι βέβαιος για αυτό, έτσι το πρόβλημά μου φαίνεται σε αυτό το απόσπασμα κώδικα, ίσως κάποιος ξέρει μια καλή λύση, προσπάθησε να το καταλάβω για αρκετό διάστημα.
Ναι, ξέρω πώς να χρησιμοποιήσετε ένα πρόγραμμα εντοπισμού σφαλμάτων, ξέρω πού έγκειται το πρόβλημα και να εξηγείται μια χαρά. Δεν ξέρω πώς να το διορθώσω αυτό, εκτός αν χρησιμοποιηθεί με τον τρόπο που ανέφερα.
Αυτό συγκεντρώνει προστίμου σύμφωνα με το Visual Studio 2015.
Παρακαλείστε επίσης να δείτε τα σχόλια στην κύρια λειτουργία.
#include <iostream>
#include <vector>
class MessageBuffer
{
public:
MessageBuffer(const std::string &s)
{
_msgBuffer.assign(s.begin(), s.end());
}
char &operator[](std::size_t nIndex)
{
return _msgBuffer[nIndex];
}
//more functions...
private:
std::vector<char> _msgBuffer;
};
class PIterator
{
public:
PIterator(MessageBuffer &b)
: m_Ref(b)
, m_Where(0)
{ }
PIterator &operator=(PIterator &other)
{
if (this == &other)
return *this;
this->m_Ref = other.m_Ref;
this->m_Where = other.m_Where;
return *this;
}
//more functions...
PIterator operator+(unsigned int nValue) const
{
PIterator copy(*this);
copy.m_Where += nValue;
return copy;
}
PIterator &operator+=(unsigned int nValue)
{
m_Where += nValue;
return *this;
}
char &operator*()
{
return m_Ref[m_Where];
}
private:
MessageBuffer &m_Ref;
std::size_t m_Where;
};
int wmain(int argv, wchar_t **args)
{
std::string msg = 123MyMessage; //Length 12
// ^ Index 3, Position 4
MessageBuffer mb(msg);
PIterator itr(mb);
//Calculations - here the results hardcoded
std::size_t nSizeOfMessage = 9; //The size of the message without the numbers
//itr.m_Where is 3 - That's where the non-numeric part of the message starts
itr += 3;
std::string needThis;
PIterator cpy = itr + nSizeOfMessage; //itr points to the first element of the message
//cpy is now out of bounds - position is correct, but index is 1 too large
needThis.assign(&*itr, &*cpy); //boom
return 0;
}