枚举的分段错误

发布于 2024-12-14 13:48:31 字数 368 浏览 0 评论 0原文

我有以下功能

template<class T> T stringTo(const std::string& s)
{
 std::istringstream iss(s);
  T x;
  iss>>x;
  return x;
};

,当我按如下方式应用它时:

session ft = stringTo<session>("F");

会话在哪里:

enum session {F, S, T};

我收到分段错误。

你能帮我找出我的错误出在哪里吗...

I have the following function

template<class T> T stringTo(const std::string& s)
{
 std::istringstream iss(s);
  T x;
  iss>>x;
  return x;
};

and when I apply it as follows:

session ft = stringTo<session>("F");

where session is:

enum session {F, S, T};

I get a Segmentation fault.

can you help me figure out where my error lies...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

ㄖ落Θ余辉 2024-12-21 13:48:31

问题可能是您使用的是 'F' 而不是 "F" 吗?

看起来您的 stringTo 函数需要一个 string 而您正在发送一个 char

顺便说一句,我认为你的方法无效。您可能需要设置一个从字符串到枚举的映射,反之亦然。

也许,可能会给你一个选择。

Could the problem be that you are using 'F' instead of "F"?

It seems like your stringTo function wants a string and you are sending in a char.

Btw, I don't think your approach is valid. You probably need to setup a map that takes you from string to enum or vice versa.

Maybe, this might give you an option.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文