gpt4 book ai didi

c++ - 当 Visual Studio 运行正常时, vector 迭代器上的 LInux g++ 编译器错误

转载 作者:行者123 更新时间:2023-11-28 04:54:00 25 4
gpt4 key购买 nike

我已经在 Visual Studio 2017 Enterprise 上成功编译了一个 C++ 文件分配程序,没有任何问题。但是,当我尝试在使用 GCC 4.8.5 的 Red Hat Linux 服务器上编译相同的程序时,我在使用的 vector/迭代器上遇到错误。这是嵌套结构和我的 vector 使用的类型:

    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

这里是产生错误的代码:

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

具体来说,“at”迭代器在 vector 插入和删除方法中都抛出了错误:

{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 无法处理 std::vector::insertstd::vector::erase 中的常量迭代器(The GNU C++ Library Manual - 1.1 Implementation Status - 23.3.6):

将对 cbegin()cend() 的调用更改为 begin()end() .

auto at = lower_bound(fatVector.begin(), fatVector.end(), filename);

您的代码可以在更高版本中正常编译。

看起来像defect in the language standard作为@UncleBens 点(Container insert/erase and iterator constness(Revision 1)) N2350)。

关于c++ - 当 Visual Studio 运行正常时, vector 迭代器上的 LInux g++ 编译器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47569813/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com