首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Visual Studio "item + item“问题

Visual Studio "item + item“问题
EN

Stack Overflow用户
提问于 2021-02-16 09:59:33
回答 3查看 44关注 0票数 1

所以我对有一些问题

这是我当前的问题

代码语言:javascript
运行
复制
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

编辑:有人问我想做什么

代码语言:javascript
运行
复制
        void newStyle(const char* name) {
            const char* path = "./Assets/Styles/" + name + ".style";
            std::ofstream file(path);
            std::string data("STYLE (WIP)");
            file << data;
        }
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2021-02-16 10:33:33

您不能在C++中添加两个指针。我建议你使用std::string

在C++20中:

代码语言:javascript
运行
复制
#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}库:

代码语言:javascript
运行
复制
#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

票数 2
EN

Stack Overflow用户

发布于 2021-02-16 10:07:08

写作

代码语言:javascript
运行
复制
auto path = std::string("./Assets/Styles/") + name + ".style";
std::ofstream file(path); // Requires path.c_str() prior to C++11

是个不错的解决办法。

否则,您将尝试将语言不允许的const char*指针添加到一起。将+的第一个参数声明为字符串类型实质上通过重载+操作符将+置于连接模式中。

票数 3
EN

Stack Overflow用户

发布于 2021-02-16 10:06:02

默认情况下,C++不为const char*的字符串提供+运算符。如果您的目标是连接字符串,则必须定义+运算符或使用std::string

代码语言:javascript
运行
复制
#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++为intfloatdouble等类型定义了加法(+)运算符,但没有为const char*指定加法运算符。正如@largest_prime_is_463035818所述,它与如何配置项目无关。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66222235

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档