简体   繁体   English

#define的新变量

[英]New variable with #define

I'd like to create variables "automatically" with #declare. 我想用#declare“自动”创建变量。 So, I don't want to type to new variable's class definition many times (actually I'm declaring multidimensional vectors, not simple integers...) 所以,我不想多次输入新变量的类定义(实际上我是在声明多维向量,而不是简单的整数......)

I have this code: 我有这个代码:

#define inti(aa)(int (aa)=3)

...

inti(a);

But the compiler says: 但是编译器说:

"error: 'a' was not declared in this scope" “错误:'a'未在此范围内声明”

Is it possible to solve this problem in C++? 是否有可能在C ++中解决这个问题? Please help! 请帮忙!

Use: 采用:

 #define inti(aa) int aa=3

That's because 那是因为

(int aa=3);

is illegal, even more what you have there. 是非法的,甚至更多你在那里。

Actually, scratch that. 实际上,划伤那个。 Don't use a macro . 不要使用宏 Just declare your variables the good old-fashioned way. 只是声明你的变量是老式的方式。

actually I'm declaring multidimensional vectors 实际上,我正在声明多维向量

+1 for the question for stating your actual problem. +1表示您的实际问题。 This is what a typedef is for. 这就是typedef的用途。

typedef std::vector<std::vector<int> > MDVector;
MDVector multiDimensionalVector;

You could use a typedef instead of a #define like this: 您可以使用typedef而不是#define如下所示:

typedef complexClassName<PossiblyWithManyTemplateParameters> Name
Name var1;
Name var2;
Name var3;

if you don't want to type the long name many times. 如果您不想多次输入长名称。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM