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
g++ -std=c++14 -c main.cpp -o main.o #编译_方法1
./cash_program.exe #运行2.方法2
D:/wecode_build_tools/mingw/bin/g++.exe -std=c++14 main.cpp AcceptCash.cpp -o cash_program.exe #编译_方法2
./cash_program.exe #运行
3.多文件编译
g++ -std=c++14 main.cpp MathUtils.cpp -o cash_program.exe #多文件编译
./cash_program.exe #运行整理不易,诚望各位看官点赞 收藏 评论 予以支持,这将成为我持续更新的动力源泉。若您在阅览时存有异议或建议,敬请留言指正批评,让我们携手共同学习,共同进取,吾辈自当相互勉励!
