c++ - 使用算法 header 时,同一程序无法在 Windows 上编译

标签 c++ visual-studio visual-c++

<分区>

我正在学习 C++。

我编写了一个程序,可以在 Ubuntu 18.04.2 上编译 find,但它不能在 Windows 7 64 位上编译。

要在 Windows 上编译,我使用的是 Visual Studio:

Microsoft Visual Studio Enterprise 2017 Version 15.9.12 VisualStudio.15.Release/15.9.12+28307.665

Visual C++ 2017 00369-90013-89248-AA631
Microsoft Visual C++ 2017

我得到的错误如下(当我添加 algorithm header 时):

c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\algorithm(5367): error C2988: unrecognizable template declaration/definition
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\algorithm(5367): error C2059: syntax error: '<parameter-list>'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\algorithm(5369): error C2143: syntax error: missing ';' before '{'
1>c:\program files (x86)\microsoft visual studio\2017\enterprise\vc\tools\msvc\14.16.27023\include\algorithm(5369): error C2447: '{': missing function header (old-style formal list?)

当我双击第一个错误时,它会转到这段代码的第三行:

        // FUNCTION TEMPLATE max
template<class _Ty,
    class _Pr>
    _NODISCARD constexpr const _Ty& (max)(const _Ty& _Left, const _Ty& _Right, _Pr _Pred)
        _NOEXCEPT_COND(_NOEXCEPT_OPER(_DEBUG_LT_PRED(_Pred, _Left, _Right)))
    {   // return larger of _Left and _Right using _Pred
    return (_DEBUG_LT_PRED(_Pred, _Left, _Right) ? _Right : _Left);
    }

我的代码需要algorithm header (因为我使用的是 std::find)在以下方法中:

#include <string>
#include <utility>
#include <vector>
#include <algorithm>
#include <utility>
#include <cmath>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <limits>
#include <sstream>

std::string ConvertToAStarMap::TruncateMap(const std::string& robot_map)
{
    std::string truncatedMap;

    std::vector<std::pair<int, int>> list;

    int current_x = 0;
    int current_y = 0;

    std::vector<std::string> map_cells = ConvertToAStarMap::split(robot_map, ';');

    for (std::vector<std::string>::iterator it = map_cells.begin(); it != map_cells.end(); ++it)
    {
        std::vector<std::string> locations = ConvertToAStarMap::split(*it, ',');

        double x = std::stod(locations[0]);
        double y = std::stod(locations[1]);

        if (x < 0)
            current_x = static_cast<int>(std::trunc(x));
        else
            current_x = static_cast<int>(std::trunc(x + 1));

        if (y < 0)
            current_y = static_cast<int>(std::trunc(y));
        else
            current_y = static_cast<int>(std::trunc(y + 1));

        std::pair<int, int> current = std::make_pair(current_x, current_y);

        if (std::find(list.begin(), list.end(), current) != list.end())
        {
            list.push_back(current);

            truncatedMap += std::to_string(current_x) + ",";
            truncatedMap += std::to_string(current_y) + ";";
        }
    }

    return truncatedMap;
}

如何解决这个错误?

更新:

如果我包含 Windows.h header ,我会收到以下错误:

warning C4003: not enough arguments for function-like macro invocation 'min'
error C2589: '(': illegal token on right side of '::'
error C2062: type 'unknown-type' unexpected
error C2059: syntax error: ')'
warning C4003: not enough arguments for function-like macro invocation 'max'
error C2589: '(': illegal token on right side of '::'
error C2062: type 'unknown-type' unexpected
error C2059: syntax error: ')'

在下面的代码中:

// Init max and min variables.
int max_x = std::numeric_limits<int>::min();
int min_x = std::numeric_limits<int>::max();

更新 2:

ConvertToAStarMap.h => https://pastebin.com/PEHEvhSm
ConvertToAStarMap.cpp => https://pastebin.com/Y4JWiyVU
Main.cpp => https://pastebin.com/6LdQKVpP

最佳答案

由于 struct max {},您似乎遇到了名称冲突在你的标题中。 C++ 标准库有一个 std::max <algorithm> 中的函数模板| Windows.h header 定义了一个 max类似函数的宏(除非您在包含 header 之前 #define NOMINMAX)。

我建议您为您的 struct 选择一个不同的(更有意义的)名称以避免名称冲突。

关于c++ - 使用算法 header 时,同一程序无法在 Windows 上编译,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57258136/

相关文章:

c++ - 引用是否有存储位置?

c++ - 线程调用成员函数

c# - Unity 找不到 dll 引用

c++ - 从现有项目构建 exe 包(可移植)

c++ - 如果返回类型从 auto 更改为 bool,Lambda 函数将抛出错误

c++ - 专业面试的意外答案

c++ - 在AVX2中有效实现log2(__ m256d)

sql-server - TSQL单元测试工具2017 : SQL Server Data Tools 2017 vs tSQLt

windows - 在 Windows 8 媒体基础转换中在 IMFMediaBuffer 上绘制文本

源代码中的条件编译