问题:标识符“requires”未定义。并且:命名空间“std”没有成员“integral”

问题描述 投票:0回答:1

很抱歉,如果这个问题很愚蠢(我认为是这样),但我仍然是学习 C++ 的初学者,我花了一整天的时间试图找到解决方案,但找不到任何解决方案。

在通过 YouTube 视频学习 C++ 时,我尝试使用 Visual Studio Code 和 gcc 编译器版本 13.2.0 应用以下简单代码,出现以下问题:

  • 标识符“需要”未定义
  • 命名空间“std”没有成员“integral”
  • 需要一个“;” <<<<< I think this is related to the first two errors, because ';' is expected after std::integral in the template function, which is not correct.

代码是:

#include <iostream>
#include <concepts>

template <typename T> requires std::integral<T> T maximum (T a, T b)
{
    return (a>b)?a:b;
}
int main ()
{
    char a1{10};
    char a2{20};
    auto a = maximum(a1,a2);
    std::cout << "a : " << a << std::endl;

    int b1{11};
    int b2{5};
    auto b = maximum(b1,b2);
    std::cout << "b : " << b << std::endl;

    double c1{45.9};
    double c2{12.4};
    auto c = maximum(c1,c2);
    std::cout << "c : " << c << std::endl;

    return 0;
}

看起来像是

的内容

没有被认可或类似的事情。 我尝试按照某人的建议写,这使得上面提到的问题消失了,但是又出现了两个新问题:

  • #include 检测到错误。请更新您的 includePath。此翻译单元禁用波形图 (C:\PCHDD\Programming PCHDD\Learning\Daniel\Template\main.cpp)。
  • 无法打开源文件“concepts.h”

这些是tasks.json的内容,我应该使用c ++ 20,其中包含/支持

块引用

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "Build with GCC 13.2.0",
            "command": "C:\\mingw64\\bin\\g++.exe",
            "args": [
                "-fdiagnostics-color=always",
                "-g",
                "-std=c++20",
                "${workspaceFolder}/*.cpp",
                "-o",
                "${fileDirname}\\${fileBasenameNoExtension}.exe"
            ],
            "options": {
                "cwd": "${fileDirname}"
            },
            "problemMatcher": [
                "$gcc"
            ],
            "group": "build",
            "detail": "compiler: C:\\mingw64\\bin\\g++.exe"
        }
    ]
}

预先感谢您的帮助。

c++ std header-files libraries c++-concepts
1个回答
0
投票

这里有几个问题。

问题1

标识符

requires
只能在 C++20(及以上)中使用。同样,std::integral 也仅适用于 C++20。因此,您需要确保您使用的是 C++20 或更高版本。

问题2

接下来,由于

double
不是整型,所以调用
maximum(c1,c2)
无效。

© www.soinside.com 2019 - 2024. All rights reserved.