首页 > 解决方案 > 使用算法头时,同一程序无法在 Windows 上编译

问题描述

我正在学习 C++。

我已经编写了一个在 Ubuntu 18.04.2 上编译 find 的程序,但它不在 Windows 7 64 位上。

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

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

视觉 C++ 2017 00369-90013-89248-AA631
微软视觉 C++ 2017

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

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标头(因为我使用的是 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标题,我会收到以下错误:

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

标签: c++visual-studiovisual-c++

解决方案


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

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


推荐阅读