I have successfully compiled a C++ file allocation program on Visual Studio 2017 Enterprise with no issues. However, when I try to compile the same program on a Red Hat Linux server with GCC 4.8.5, I get errors that hound on the vector/iterator I'm using. Here is the nested struct and the type my vector is using:
struct FATPtr
{
string filename;
int fileByte;
bool operator< (const FATPtr& other) const // overloaded < operator to compare two FAT pointers;
{ // comparison is by filename
return filename < other.filename;
}
bool operator< (const string& fname) const // overloaded < operator to compare FAT pointer filename
{ // to another filename
return filename < fname;
}
};
FATPtr fatPtr; // pointer to a file allocation table entry
vector<FATPtr> fatVector; // vector to hold these pointers
And here is the code generating the errors:
// insert FAT filename and location into sorted vector
fatPtr.filename = filename;
fatPtr.fileByte = fatByte;
auto at = lower_bound(fatVector.cbegin(), fatVector.cend(), filename);
fatVector.insert(at, fatPtr);
void DiskInterface::deleteFATEntry(Disk& dsk, string filename, int entry)
{
auto at = lower_bound(fatVector.cbegin(), fatVector.cend(), filename);
fatVector.erase(at);
dsk.writeFAT(entry);
}
Specifically, the "at" iterator is throwing the error in both the vector insert and erase methods:
{cslinux1:~/CS4348/Project3} g++ -std=c++0x Project3.cpp -o Project3
Project3.cpp: In member function ‘void DiskInterface::addFAT(Disk&, std::string, int, int)’:
Project3.cpp:248:29: error: no matching function for call to ‘std::vector<DiskInterface::FATPtr>::insert(__gnu_cxx::__normal_iterator<const DiskInterface::FATPtr*, std::vector<DiskInterface::FATPtr> >&, DiskInterface::FATPtr&)’
fatVector.insert(at, fatPtr);
GCC 4.8.5 could not handle const iterators in std::vector::insert
and std::vector::erase
( The GNU C++ Library Manual - 1.1 Implementation Status - 23.3.6 ):
Change your calls to cbegin()
and cend()
to begin()
and end()
.
auto at = lower_bound(fatVector.begin(), fatVector.end(), filename);
Your code compiles fine with later versions.
Looks like defect in the language standard as @UncleBens points ( Container insert/erase and iterator constness(Revision 1)) N2350 ).
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.