【发布时间】: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