C++函数式策略模式中配置修改

在从面向过程 .到面向对象 ,再到函数式策略模式的转变和精进时,

敲的代码编译运行不了,不支持多文件编译,分析定位后是之前的相关json 配置导致,修改如下:

一.原来配置

1.tasks.json

复制代码
//tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "type": "cppbuild",
            "label": "build",
            "type": "shell",
            "command": "D:/wecode_build_tools/mingw/bin/g++.exe",
            "args": [
                "-g",
                "${file}",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "problemMatcher": [
                "$gcc"
            ]
        }
    ]
  }

2.settings.json

复制代码
//settings.json

{
    "files.associations": {
        "iostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp"
    },
    "files.encoding": "utf8",
    "files.autoGuessEncoding": true,
    "terminal.integrated.shellArgs.windows": [
        "-NoProfile",
        "-ExecutionPolicy",
        "Bypass",
        "-Command",
        "chcp 65001"
    ],
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    "terminal.integrated.shellArgs.windows": ["/k", "D:\\wecode_build_tools\\mingw\\bin\\mingw32-make.exe"]
}

二.修改版本

编译配置是否正确,特别是编译器路径和C++标准。之前的代码修复中使用了C++14的特性,比如std::make_unique,这是C++14引入的,而tasks.json中指定的是-std=c++11,这会导致编译错误,因为C++11不支持std::make_unique。这是一个关键问题,需要用户将C++标准升级到C++14或更高。

另外,检查settings.json中的终端参数是否正确。用户可能希望终端在编译后自动运行程序,但当前配置可能没有设置。可能需要添加运行可执行文件的步骤,或者在tasks.json中添加一个执行任务。

1.tasks.json

复制代码
//tasks.json

{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build and Run",
            "type": "shell",
            "command": "D:/wecode_build_tools/mingw/bin/g++.exe",
            "args": [
                "-std=c++11",
                "main.cpp",
                "AcceptCash.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": "$gcc"
        }
    ]
}

2.settings.json

复制代码
// settings.json

{
    "files.associations": {
        "*.h": "cpp",
        "*.hpp": "cpp",
        "iostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp",
        "random": "cpp",
        "bit": "cpp",
        "bitset": "cpp",
        "chrono": "cpp",
        "condition_variable": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "list": "cpp",
        "map": "cpp",
        "set": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "numeric": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "future": "cpp",
        "iomanip": "cpp",
        "mutex": "cpp",
        "shared_mutex": "cpp",
        "thread": "cpp",
        "cinttypes": "cpp",
        "complex": "cpp",
        "unordered_set": "cpp",
        "cfenv": "cpp",
        "typeindex": "cpp"
    },
    "files.encoding": "utf8",
    "files.autoGuessEncoding": true,
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k",
        "D:\\wecode_build_tools\\mingw\\bin\\mingw32-make.exe"
    ],
    "C_Cpp.default.compilerPath": "D:/wecode_build_tools/mingw/bin/g++.exe",
    "C_Cpp.default.cppStandard": "c++14",
    "C_Cpp.default.cStandard": "c17",
    "files.trimTrailingWhitespace": true,
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
    "C_Cpp.intelliSenseEngine": "Default",
    "C_Cpp.errorSquiggles": "Enabled",
    "C_Cpp.autocomplete": "Disabled"
}

三.最终修改

我这本来编译就上午调好了,环境的也改的差不多了,改14版本后

用的是旧版配置键名 C_Cpp.default.*,但 VS Code 的 C/C++ 扩展(Microsoft C/C++)在新版中已经改用 C_C++(带加号)作为命名空间!

1.tasks.json

复制代码
//tasks.json
{
    "version": "2.0.0",
    "tasks": [
        {
            "label": "Build and Run",
            "type": "shell",
            "command": "D:/wecode_build_tools/mingw/bin/g++.exe",
            "args": [
                "-std=c++14",
                "main.cpp",
                "AcceptCash.cpp",
                "-o",
                "${fileDirname}/${fileBasenameNoExtension}.exe",
                "&&",
                "${fileDirname}/${fileBasenameNoExtension}.exe"
            ],
            "group": {
                "kind": "build",
                "isDefault": true
            },
            "presentation": {
                "echo": true,
                "reveal": "always",
                "focus": false,
                "panel": "shared"
            },
            "problemMatcher": {
                "owner": "cpp",
                "fileLocation": [
                    "relative",
                    "${workspaceFolder}"
                ],
                "pattern": {
                    "regexp": "^(.*):(\\d+):(\\d+):\\s+(warning|error):\\s+(.*)$",
                    "file": 1,
                    "line": 2,
                    "column": 3,
                    "severity": 4,
                    "message": 5
                }
            }
        }
    ]
}

2.settings.json

复制代码
// settings.json
{
    "files.associations": {
        "*.h": "cpp",
        "*.hpp": "cpp",
        "iostream": "cpp",
        "array": "cpp",
        "atomic": "cpp",
        "*.tcc": "cpp",
        "cctype": "cpp",
        "clocale": "cpp",
        "cmath": "cpp",
        "cstdarg": "cpp",
        "cstddef": "cpp",
        "cstdint": "cpp",
        "cstdio": "cpp",
        "cstdlib": "cpp",
        "cwchar": "cpp",
        "cwctype": "cpp",
        "deque": "cpp",
        "unordered_map": "cpp",
        "vector": "cpp",
        "exception": "cpp",
        "algorithm": "cpp",
        "memory": "cpp",
        "memory_resource": "cpp",
        "optional": "cpp",
        "string": "cpp",
        "string_view": "cpp",
        "system_error": "cpp",
        "tuple": "cpp",
        "type_traits": "cpp",
        "utility": "cpp",
        "fstream": "cpp",
        "initializer_list": "cpp",
        "iosfwd": "cpp",
        "istream": "cpp",
        "limits": "cpp",
        "new": "cpp",
        "ostream": "cpp",
        "sstream": "cpp",
        "stdexcept": "cpp",
        "streambuf": "cpp",
        "typeinfo": "cpp",
        "random": "cpp",
        "bit": "cpp",
        "bitset": "cpp",
        "chrono": "cpp",
        "condition_variable": "cpp",
        "cstring": "cpp",
        "ctime": "cpp",
        "list": "cpp",
        "map": "cpp",
        "set": "cpp",
        "functional": "cpp",
        "iterator": "cpp",
        "numeric": "cpp",
        "ratio": "cpp",
        "regex": "cpp",
        "future": "cpp",
        "iomanip": "cpp",
        "mutex": "cpp",
        "shared_mutex": "cpp",
        "thread": "cpp",
        "cinttypes": "cpp",
        "complex": "cpp",
        "unordered_set": "cpp",
        "cfenv": "cpp",
        "typeindex": "cpp"
    },
    "files.encoding": "utf8",
    "files.autoGuessEncoding": true,
    "terminal.integrated.shell.windows": "C:\\Windows\\System32\\cmd.exe",
    "terminal.integrated.shellArgs.windows": [
        "/k",
        "D:\\wecode_build_tools\\mingw\\bin\\mingw32-make.exe"
    ],
    "C_Cpp.default.compilerPath": "D:/wecode_build_tools/mingw/bin/g++.exe",
    "C_Cpp.default.cppStandard": "c++14",
    "C_Cpp.default.cStandard": "c17",
    "files.trimTrailingWhitespace": true,
    "editor.formatOnSave": true,
    "editor.codeActionsOnSave": {
        "source.organizeImports": true
    },
    "C_Cpp.intelliSenseEngine": "default",
    "C_Cpp.errorSquiggles": "enabled",
    "C_Cpp.autocomplete": "disabled"
}

四.验证

最后使用的配置可以运行

1.方法1

bash 复制代码
g++ -std=c++14 -c main.cpp -o main.o #编译_方法1
./cash_program.exe #运行

2.方法2

bash 复制代码
D:/wecode_build_tools/mingw/bin/g++.exe -std=c++14 main.cpp AcceptCash.cpp -o cash_program.exe #编译_方法2
./cash_program.exe #运行

3.多文件编译

bash 复制代码
g++ -std=c++14 main.cpp MathUtils.cpp -o cash_program.exe #多文件编译
./cash_program.exe #运行

整理不易,诚望各位看官点赞 收藏 评论 予以支持,这将成为我持续更新的动力源泉。若您在阅览时存有异议或建议,敬请留言指正批评,让我们携手共同学习,共同进取,吾辈自当相互勉励!

相关推荐
ysa05103027 分钟前
斐波那契上斐波那契【矩阵快速幂】
数据结构·c++·笔记·算法
6Hzlia2 小时前
【Hot 100 刷题计划】 LeetCode 45. 跳跃游戏 II | C++ 贪心算法最优解题解
c++·leetcode·游戏
森G2 小时前
48、柱状图---------QChart
c++·qt
Tanecious.2 小时前
蓝桥杯备赛:Day8-小苯的异或和
c++·蓝桥杯
王老师青少年编程2 小时前
csp信奥赛c++中的递归和递推研究
c++·算法·递归·递推·csp·信奥赛
样例过了就是过了3 小时前
LeetCode热题100 跳跃游戏
c++·算法·leetcode·贪心算法·动态规划
chen_ever3 小时前
从网络基础到吃透 Linux 高并发 I/O 核心(epoll+零拷贝 完整版)
linux·网络·c++·后端
无限进步_3 小时前
【C++&string】寻找字符串中第一个唯一字符:两种经典解法详解
开发语言·c++·git·算法·github·哈希算法·visual studio
小此方3 小时前
Re:思考·重建·记录 现代C++ C++11篇 (二) 右值引用与移动语义&引用折叠与完美转发
开发语言·c++·c++11·现代c++
深邃-3 小时前
【C语言】-数据在内存中的存储(1)
c语言·开发语言·数据结构·c++·算法