<分区>
我正在学习 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