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 #运行

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

相关推荐
小李小李快乐不已20 小时前
栈和堆理论基础
c++·算法·leetcode
夏幻灵20 小时前
CMD是什么
c++
HABuo21 小时前
【Linux进程(一)】进程深入剖析-->进程概念&PCB的底层理解
linux·运维·服务器·c语言·c++·后端·进程
图形学爱好者_Wu21 小时前
每日一个C++知识点|菱形继承
c++·程序员·编程语言
.简.简.单.单.21 小时前
Design Patterns In Modern C++ 中文版翻译 第十章 外观模式
c++·设计模式·外观模式
十五年专注C++开发21 小时前
Jieba库: 一个中文分词领域的经典库
c++·分布式·自然语言处理·中文分词
_OP_CHEN21 小时前
【C++数据结构进阶】从 Redis 底层到手写实现!跳表(Skiplist)全解析:手把手带你吃透 O (logN) 查找的神级结构!
数据结构·数据库·c++·redis·面试·力扣·跳表
菜菜的院子21 小时前
vcpkg配置
c++
我的offer在哪里1 天前
c++的回调函数
开发语言·c++
Hello eveybody1 天前
C++四级考试要点
开发语言·c++