所以我对有一些问题
这是我当前的问题
const char* Char1 = "Foo ";
const char* Char2 = "Bar ";
const char* Char3 = Char1 + Char2;
/* ^
This is where the error is coming
Here the error I'm getting
expression must have integral or unscoped enum type
*/这是用于字符串和ImVec <- (ImGui)的。我的朋友告诉我,我的项目配置错了。但我不知道我做错了什么。
某个大脑力天才能帮我吗?
忘了添加我在使用VisualStudio2019
编辑:有人问我想做什么
void newStyle(const char* name) {
const char* path = "./Assets/Styles/" + name + ".style";
std::ofstream file(path);
std::string data("STYLE (WIP)");
file << data;
}发布于 2021-02-16 10:33:33
您不能在C++中添加两个指针。我建议你使用std::string。
在C++20中:
#include <string_view>
#include <format>
void newStyle(std::string_view const name) { // note string view
auto path = std::format("./Assets/Styles/{}.style", name);
// ...
}大多数编译器还不支持std::format。同时,您可以使用{fmt}库:
#include <string_view>
#include <fmt/core.h>
void newStyle(std::string_view const name) {
auto path = fmt::format("./Assets/Styles/{}.style", name);
// ...
}还可以考虑std::filesystem::path。
发布于 2021-02-16 10:07:08
写作
auto path = std::string("./Assets/Styles/") + name + ".style";
std::ofstream file(path); // Requires path.c_str() prior to C++11是个不错的解决办法。
否则,您将尝试将语言不允许的const char*指针添加到一起。将+的第一个参数声明为字符串类型实质上通过重载+操作符将+置于连接模式中。
发布于 2021-02-16 10:06:02
默认情况下,C++不为const char*的字符串提供+运算符。如果您的目标是连接字符串,则必须定义+运算符或使用std::string,
#include <string>
...
void newStyle(std::string name) {
std::string path = "./Assets/Styles/" + name + ".style"; // std::string has a '+' operator.
std::ofstream file(path);
std::string data("STYLE (WIP)");
file << data;
}C++为int、float、double等类型定义了加法(+)运算符,但没有为const char*指定加法运算符。正如@largest_prime_is_463035818所述,它与如何配置项目无关。
https://stackoverflow.com/questions/66222235
复制相似问题