tags:

views:

112

answers:

3

To save some space in my code, I made this typedef:

typedef clients.members.at(selectedTab) currentMember;

however, g++ gives me this error:

error: expected initializer before '.' token

I figure that I'm misusing typedef, since clients.members.at(selectedTab) is a function call and not a type. Is there a way to do what I'm trying to do here?

+3  A: 

You seem to be trying to create a macro.

#define currentMember clients.members.at(selectedTab)

Though depending on how it's used, a simple function could be much better.

Chuck
That makes it work, though I'm a bit wary of macros. I'll just deal with the less concise code. Thanks for your input.
Max
Like Chuck said, if you're wary of macros, just make a simple function `currentMember()` that returns `clients.members.at(selectedTab)`.
Justin Ardini
+5  A: 

If this is used function-local and neither clients.members nor selectedTab change between its uses, just use references. E.g.:

Member& currentMember = clients.members.at(selectedTab);
currentMember.foo();
currentMember.bar();
Georg Fritzsche
This is a much better solution than what I was trying to do, so I'll go with this. Thanks for your help.
Max
+1  A: 

In C++0x, you can do

decltype(clients.members.at(selectedTab)) currentMember(clients.members.at(selectedTab));

However, what you're doing is fundamentally not possible. You're trying to get a value and make it a type. That's just not doable. Consider the following code:

typedef clients.members.at(selectedTab) currentMember;
currentMember a;

What on earth is a, and what does it do? You're going to have to explain more what you want.

DeadMG