C++ vector 操作错误

标签 c++ vector

我目前在我的程序中使用 vector ,但出现了一些奇怪的错误,这些错误是在我开始使用该类后才出现的。

错误是:

1>MyCloth.obj : error LNK2019: unresolved external symbol __CrtDbgReportW referenced in function "public: unsigned int & __thiscall std::vector<unsigned int,class std::allocator<unsigned int> >::operator[](unsigned int)" (??A?$vector@IV?$allocator@I@std@@@std@@QAEAAII@Z)
1>libcpmtd.lib(stdthrow.obj) : error LNK2001: unresolved external symbol __CrtDbgReportW
1>D:\Licenta\Project\IOPBTS\Debug\IOPBTS.exe : fatal error LNK1120: 1 unresolved externals

我的代码是:

在头文件中:

#undef vector
#include <vector>

void findPieceVertices(NxU32 selectedVertex); 
bool checkVertexExistsInClothPieceElements(int vertex);
void findVertexTriangles(NxU32 vertex);
std::vector<NxU32> clothPieceElements;

在cpp文件中:

bool MyCloth::checkVertexExistsInClothPieceElements(int vertex)
{
for(int i=0;i<clothPieceElements.size();i++)
    if(clothPieceElements[i]==vertex)
        return true;
return false;
}

void MyCloth::findVertexTriangles(NxU32 vertex)
{
NxMeshData data = mCloth->getMeshData();
NxU32* vertices = (NxU32*)data.indicesBegin;
NxU32 aux = 0;

for(int i=0;i<(mInitNumVertices-1)*3;i+=3)
{
    if(*vertices == vertex || *(vertices+1) == vertex || *(vertices+2) == vertex)
    {
        if(!checkVertexExistsInClothPieceElements(*vertices))
            clothPieceElements.push_back(*vertices);
        if(!checkVertexExistsInClothPieceElements(*(vertices+1)))
            clothPieceElements.push_back(*(vertices+1));
        if(!checkVertexExistsInClothPieceElements(*(vertices+2)))
            clothPieceElements.push_back(*(vertices+2));
    }
    vertices = vertices + 3;
}

void MyCloth::findPieceVertices(NxU32 selectedVertex)
{
clothPieceElements.push_back(selectedVertex);
int i=0;
while(i<clothPieceElements.size())
{
    findVertexTriangles(clothPieceElements[i]);
    i++;
}

}

我做错了什么?我在网上找了个东西,说我用的文件是release模式编译的,我也应该这样做。问题是,如果我在 Release模式下编译,这些错误就会消失,但我的程序无法找到一个非常重要的非 C 库,该库由添加到 VCC 目录 -> 包含目录中的路径指向。

有谁知道为什么会出现这个错误?或者是什么意思

编辑:另外,谁能告诉我在 Debug模式或 Release模式下构建的区别?

最佳答案

您似乎把 CRT 库搞得一团糟。 Debug 和 Release 构建之间有两个主要区别:

  • 使用不同的 CRT 库
  • 对代码进行了不同的优化

两者都与您的问题有关。一、check this comment .看起来您的链接行中缺少 libcmtd.lib。检查您是否没有从链接器 -> 输入选项下的链接中排除像这样的重要库。

__CrtDbgReportW 函数与 vector::operator[] 在调试构建中执行的一些运行时检查有关。由于这些检查在 Release 构建中被禁用,因此您在 Release 中不会出现此错误。

还要确保您在 C/C++ -> 代码生成选项下使用正确版本的 CRT。您应该有一个用于调试配置的调试版本(动态或静态)和一个用于发布配置的发布版本。

这是一个棘手的问题,没有任何经验就可以解决。如果有可能,我建议您从默认模板创建一个新项目,并将您的所有文件添加到这个新项目,以确保默认情况下所有设置都是正确的。

关于C++ vector 操作错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16734957/

相关文章:

c++ - C++ 中真正空的 std::vector 是什么?

c++ - std::vector<Simd_wrapper> 是否在内存中有连续的数据?

c++ - libcurl:检测 block 编码响应的 block 边界

c++ - 为什么此代码(在 Matlab 的 MEX 文件中使用 OpenMP)给出不同的结果?

c++ - 如何编写c++ dll函数来获取com端口阅读

c++ - 具有不同类型的多个 vector

c++ - 在对象上使用 push_back,C++

c++ - 默认参数,gcc vs clang

c++ - 在代码块中运行我的第一个图形程序

c++ - 我如何正确地将 shared_pointers 添加到可能的派生类到 std::vector 中?