English 中文(简体)
符合标准
原标题:get code line with __LINE__
  • 时间:2011-10-28 12:33:05
  •  标签:
  • c++
  • c
  • macros

我试图通过使用:

#include <stdio.h>

void err (char *msg)
{
    printf ("%s : %d" , msg , __LINE__);
}

int main ( int argc , char **argv )
{
    ERR ("fail..");
    return 0;
}

但是,如果总是有错线编号,那么它就应当改为10<>/code>,而不是5,如何确定这一编号?

另外,我还试图使用一些宏观方法:

#define ERR (msg) do { printf ("%s : %d " , msg , __LINE__); } while (0)

页: 1

最佳回答
#define ERR(msg) printf("%s : %d", (msg), __LINE__)

如果是这样的话。

你不需要这一职能!

问题回答

为了完成这项工作,你需要将<_LINE__作为单独的参数通过。

#include <stdio.h>

void err (char *msg, int line)
{
    printf ("%s : %d" , msg , line);
}

int main ( int argc , char **argv )
{
    err("fail..", __LINE__);
    return 0;
}

这样做的更好办法是将采用这种方法界定为macro,例如:

#define PRINTERR(msg) err((msg), __LINE__)

带上 当前线,意思是要求上线。 你们需要把它当作一个参数:

ERR ("fail..", __LINE__);

否则,它就永远是你工作中的错误内容,例如5。 更改您接受<代码>int的职能 缩略语

我将使用@Ed Heal回答的宏观方法。 另外,你之所以“未申报的”是,宏观变量需要放在括号中(即(msg),因为宏观名称和从参数清单开始的括号之间有空间。

您可提出以下案文:

#define ERR(msg) fprintf(stderr, "ERROR on line %d: %s
", __LINE__, (msg))




相关问题
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?

热门标签