English 中文(简体)
* 与空虚一起 充斥着Cast Char *
原标题:ostringstream overloading const char * with the void *
  • 时间:2012-05-24 14:44:47
  •  标签:
  • c++

我写下了一个非常基本的宏, 它应该印出它的参数。

请见下文示例代码:

#define LOG Message(__FILE__, __LINE__,__func__)

class Message : public std::ostringstream 
{
    public:
        Message(const char *param1, int param2, const char *param3)
        {
            *this<<param3<<"("<<param1<<":"<<param2<<")";
        }
        ~Message()
        {
            *this<<endl;
            cout<< rdbuf()->str();
        }
};


int main()
{
    int integer = 1;
    string str = "XYZ";

    LOG<<integer<<"DEBUGLOG1: "<<str<<" "<<integer;
    return 0;
}

问题是如果我使用:

LOG << "LOG1: " << str << " " << integer;

输出打印 Const char* " LOG1: " 的地址*,而不是数值。

But, If I use:

LOG << integer << "DEBUGLOG1: " << str << " " << integer;

输出工作完美, 打印整数值和字符值 。

它看起来像是 而不是使用 ocream& 运算符 & lt; & lt; (Ostream& amp; 出去, const char* s);

它正在使用 ocream& 运算符 & lt; & lt; ( const void * val; ?

有没有人能说明可以做些什么来克服这种超载问题?

提前感谢

问题回答

问题是 Message (...) 是临时的。 临时无法与 operator< & lt; 的非星系引用参数绑定 。

它只能用于成员函数和操作员, 如 ocream:: operator<< & lt; (int)

奇怪的是,成员操作员会返回一个参考文件,而这个参考文件又可以与非成员操作员一起使用。

之前的复杂度, 并观察宏观被拆解的情况。 与宏合作是困难的( 并且无法咨询... ), 而这是一个非常基本上可以调试的方法, 您需要知道这个方法才能避免继续接触 Google 或 SO 。

我相信你想要 g++ -E





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?