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

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

相关推荐
kyle~4 小时前
C++---value_type 解决泛型编程中的类型信息获取问题
java·开发语言·c++
NiNi_suanfa7 小时前
【Qt】Qt 批量修改同类对象
开发语言·c++·qt
信奥胡老师8 小时前
苹果电脑(mac系统)安装vscode与配置c++环境,并可以使用万能头文件全流程
c++·ide·vscode·macos·编辑器
妖灵翎幺8 小时前
C++ 中的 :: 操作符详解(一切情况)
开发语言·c++·ide
star _chen8 小时前
C++实现完美洗牌算法
开发语言·c++·算法
繁星星繁9 小时前
【C++】脚手架学习笔记 gflags与 gtest
c++·笔记·学习
路痴楷9 小时前
无法定位程序输入点问题
c++·qt·visual studio
Source.Liu10 小时前
【LibreCAD】 RS_Units 类完整解析
c++·qt·rust
我是一棵无人问荆的小草10 小时前
编码演变史
开发语言·c++
potato_may11 小时前
CC++ 内存管理 —— 程序的“五脏六腑”在哪里?
c语言·开发语言·数据结构·c++·内存·内存管理