【问题标题】:C++ convert int to string inlineC ++将int转换为字符串内联
【发布时间】:2014-12-09 14:21:10
【问题描述】:

我的问题似乎很基本,但我找不到解决方案。我需要编写一个代码,通过报告异常抛出的行和位置来帮助调试。问题是__LINE__ 是一个int 值,我在使用std::string(line) 的以下代码中将其转换为字符串时遇到问题:

#pragma once

#include <stdexcept>
#include <cstring>


class CRuntime_error_line: public std::runtime_error
{
public:
    CRuntime_error_line(const char * msg, const char * file,int line)
        :runtime_error(std::string(msg)+" @"+":"+std::string(line)){}
};

#define runtime_error_line(msg) CRuntime_error_line(msg,__FILE__,__LINE__)

似乎std::string(line) 无法将 int 转换为字符串,并且在线建议的其他解决方案无法内联实现,我不知道如何在第二行调用基本构造函数!

编译器输出:

log.h: 在构造函数'CRuntime_error_line::CRuntime_error_line(const char*, const char*, int)’: log.h:10:124: error: 无效转换 从 ‘int’ 到 ‘const char*’ [-fpermissive] CRuntime_error_line(const char * msg, const char * file,int line):runtime_error(std::string(msg)+"@"+":"+std::string(line)){}

(使用g++和linux环境)

编辑:

应该这样调用宏:

throw runtime_error_line("Invalid somethihng ...!");

【问题讨论】:

  • 错误:‘to_string’ is not a member of ‘std’
  • 包括 没有改变。 error: ‘to_string’ is not a member of ‘std’。 C++11 是否适用于我的代码?
  • @barej 是的,to_string 来自 C++11
  • @barej 你必须明确enable C++11 support: g++ -std=c++11 -o prog.x prog.cpp

标签: c++ string data-conversion


【解决方案1】:

正如Borgleader 所建议的,std::to_string 是您的解决方案。它还会为您构造一个临时的std::string,因此无需从msg 构造一个临时字符串:

#pragma once

#include <stdexcept>
#include <cstring>
#include <string> // Add this to support std::to_string


class CRuntime_error_line: public std::runtime_error
{
public:
    CRuntime_error_line(const char* msg, const char* file, int line)
        : runtime_error(msg + " @:"s + std::to_string(line)){} // Use std::to_string here
};

#define runtime_error_line(msg) CRuntime_error_line(msg, __FILE__, __LINE__)

如果没有 C++11,你仍然可以这样做,只是不够干净:

#pragma once

#include <stdexcept>
#include <cstring>
#include <sstream> // Use to include std::ostringstream    

class CRuntime_error_line: public std::runtime_error
{
public:
    CRuntime_error_line(const char* msg, const char* file, int line)
        : runtime_error(static_cast<std::ostringstream&>(std::ostringstream() << msg << " @:" << line).str()){} // Use std::ostringstream here
};

#define runtime_error_line(msg) CRuntime_error_line(msg, __FILE__, __LINE__)

【讨论】:

  • 似乎 g++ 编译器无法为我识别 std::to_string
  • log.h: In constructor ‘CRuntime_error_line::CRuntime_error_line(const char*, const char*, int)’: log.h:12:40: error: ‘to_string’ is not a member of ‘std’ : runtime_error(msg + (" @:" + std::to_string(line))){} // Use std::to_string here
  • 你添加了:--std=c++11 吗?
  • 代码编译但输出有问题:0x401938 @:47。这不是我期望看到的!
  • 非常感谢,请在您的解决方案末尾添加` g++ -std=c++11 -o prog.x prog.cpp `,以供其他有类似问题的人使用。
【解决方案2】:

这种情况可能会更好:

#define STRING_DEFINE1(x) #x
#define STRING_DEFINE(x) STRING_DEFINE1(x)
...
CRuntime_error_line(msg,__FILE__,STRING_DEFINE(__LINE__))

【讨论】:

  • 创意,即使应用有限。
  • 或者更好地定义 RUNTIME_ERROR_MAGIC 是 __FILE__ " " STRING_DEFINE(__LINE__) 然后你有一个字符串文字要处理。
【解决方案3】:

我能想到的最简单的事情就是自己写一个to_string

#include <sstream>

std::string to_string(int i)
{
    std::ostringstream os;
    os << i;
    return os.str();
}

然后按照其他人的建议调用它。

【讨论】:

    猜你喜欢
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多