473,734 Members | 2,641 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

typedef question

Hello people,

I am getting errors from VS2003 when working with typedef'ed types.

For example, assume that I have a type T, defined in a 3rd party include file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So we get:

typedef char T;

Now, I want to use the unsigned form of T in my code:

unsigned T t;

This gives me the following errors:

error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)

When instead I try:

T unsigned t;

I get:

error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
error C2065: 't' : undeclared identifier
What am I doing wrong?
Best wishes,
Alex.

--
Address email to user "response" at domain "alexoren" with suffix "com"
Nov 28 '05 #1
6 7333

"Alex" <re******@myrea lbox.com> wrote in message
news:11******** *************** *************** ***@news.nntpse rver.com...
For example, assume that I have a type T, defined in a 3rd party include
file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So
we get:
typedef char T;

Now, I want to use the unsigned form of T in my code:
unsigned T t;

This gives me the following errors:
error C2628: 'T' followed by 'unsigned' is illegal (did you forget a
';'?)

When instead I try:
T unsigned t;

I get:
error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with any
other symbol. see declaration > of 'T'
error C2065: 't' : undeclared identifier

What am I doing wrong?


I don't think you are allowed to use typedef's in that manner. If instead
you were using a #define'd symbol, then adding "unsigned" in front of it
would be ok (assuming of course that the type you were using is valid when
qualified with unsigned). But a typedef defines a type, not just a symbol.
It's as if you declared a class CMyClass, and then tried to use "unsigned
CMyClass". That just doesn't make sense.

If you need an unsigned char or unsigned short, based on "condition" , then
you'll need to make a similar set of typedef's for the unsigned versions.

-Howard


Nov 28 '05 #2
Alex wrote:
Hello people,

I am getting errors from VS2003 when working with typedef'ed types.

For example, assume that I have a type T, defined in a 3rd party include file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So we get:

typedef char T;

Now, I want to use the unsigned form of T in my code:

unsigned T t;

This gives me the following errors:

error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)

When instead I try:

T unsigned t;

I get:

error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
error C2065: 't' : undeclared identifier
What am I doing wrong?


T is not a macro. It's a typedef-id. You cannot combine it with anything
except 'static', 'auto', 'register', 'const', 'volatile', or 'const
volatile'. IOW, since 'unsigned' is _not_ one of linkage specifiers or
cv-qualifiers, or storage specifiers, you cannot add it. Modify your code
to have another term for unsigned T:

#if (condition)
typedef char T
typedef unsigned char UT
#else
typedef short T
typedef unsigned short UT
#endif

and use "UT" instead of "unsigned T".

V
Nov 28 '05 #3
Alex wrote:
Hello people,

I am getting errors from VS2003 when working with typedef'ed types.

For example, assume that I have a type T, defined in a 3rd party include file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So we get:

typedef char T;

Now, I want to use the unsigned form of T in my code:

unsigned T t;

This gives me the following errors:

error C2628: 'T' followed by 'unsigned' is illegal (did you forget a ';'?)

When instead I try:

T unsigned t;

I get:

error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with any other symbol. see declaration of 'T'
error C2065: 't' : undeclared identifier
What am I doing wrong?


I would do this differently. First I would use a template class like:

template <typename T>
struct Type
{
typedef T t_signed;
typedef unsigned T t_unsigned;
};
typedef Type< ifelse<conditio n,char,short>:: type > AppType;

Now, in your code you can use:

AppType::t_sign ed t;

and

AppType::unsign ed t;

To your hearts content. Note - NO MACROS.

It's also easy to extend and specialize as needed.

Note - ifelse is somthing like

template <bool condition, typename T1, typename T2>
struct ifelse
{
typedef T1 type;
};

template <typename T1, typename T2>
struct ifelse<false, T1, T2>
{
typedef T2 type;
};

Warning - This is not compiled code but a brain dump, you will find
errors. VS2003 should easily handle this.

Nov 28 '05 #4
Gianni Mariani wrote:
Alex wrote:
Hello people,

I am getting errors from VS2003 when working with typedef'ed types.

For example, assume that I have a type T, defined in a 3rd party include
file based on some condition

#if (condition)
typedef char T;
#else
typedef short T;
#endif

Let's assume, for the sake of discussion that the condition is true. So
we get:

typedef char T;

Now, I want to use the unsigned form of T in my code:

unsigned T t;

This gives me the following errors:

error C2628: 'T' followed by 'unsigned' is illegal (did you forget a
';'?)

When instead I try:

T unsigned t;

I get:

error C2146: syntax error : missing ';' before identifier 't'
error C2377: 'T' : redefinition; typedef cannot be overloaded with
any other symbol. see declaration of 'T' error C2065: 't' :
undeclared identifier
What am I doing wrong?


I would do this differently. First I would use a template class like:

template <typename T>
struct Type
{
typedef T t_signed;
typedef unsigned T t_unsigned;


This will not fly: unlike const or volatile, unsigned is not a qualifier. It
cannot be stripped of or added by a template in a straight forward way. If
you want a template to yield the (un)signed version of a type, you need to
do a bunch of partial specializations like, for instance:
namespace DO_NOT_USE {

template < typename T >
struct signed_type {

typedef T the_type;

}; // signed_type

template <>
struct signed_type< unsigned char > {

typedef signed char the_type;

};

template <>
struct signed_type< char > {

typedef signed char the_type;

};

template <>
struct signed_type< unsigned short > {

typedef short the_type;

};

template <>
struct signed_type< unsigned int > {

typedef int the_type;

};

template <>
struct signed_type< unsigned long > {

typedef long the_type;

};

template < typename T >
struct unsigned_type {

typedef T the_type;

}; // unsigned_type

template <>
struct unsigned_type< char > {

typedef unsigened char the_type;

};

template <>
struct unsigned_type< signed char > {

typedef unsigened char the_type;

};

template <>
struct unsigned_type< signed short > {

typedef unsigned short the_type;

};

template <>
struct unsigned_type< signed int > {

typedef unsigned int the_type;

};

template <>
struct unsigned_type< signed long > {

typedef unsigned long the_type;

};

}

template < typename ArithmeticType >
class arithmetic_trai ts {
public:

typedef ArithmeticType value_type;
typedef typename
DO_NOT_USE::sig ned_type< ArithmeticType >::the_type signed_type;
typedef typename
DO_NOT_USE::uns igned< ArithmeticType >::the_type unsigned_type;
};
Best

Kai-Uwe Bux

Nov 28 '05 #5
Kai-Uwe Bux wrote:
Gianni Mariani wrote:

....
I would do this differently. First I would use a template class like:

template <typename T>
struct Type
{
typedef T t_signed;
typedef unsigned T t_unsigned;

This will not fly: unlike const or volatile, unsigned is not a qualifier. It
cannot be stripped of or added by a template in a straight forward way. If
you want a template to yield the (un)signed version of a type, you need to
do a bunch of partial specializations like, for instance:


You are right.
Nov 29 '05 #6
Thanks.
Using a template is a good idea.

Dec 2 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
2959
by: Generic Usenet Account | last post by:
This is a two-part question. (1) I have implemented a "Datastructure Registry" template class. I am getting no compiler warnings with older compilers, but newer compilers are generating the following error messages: warning: implicit typename is deprecated, please see the documentation for details Can someone kindly suggest how to get rid of these warnings? The source code follows.
7
2145
by: Tony Johansson | last post by:
Hello Experts! I have the following Array template class see below. I execute these three statements statement 1: Array<int> x(5); statement 2: cin >>x; statement 3: Array<int>::element_type y = x; but I can't understand the last one which is Array<int>::element_type y = x; We have a typedef T element_type; in the template Array class see below
30
4259
by: stephen henry | last post by:
Hi all, I have a question that I'm having difficulty answering. If I have a struct: typedef struct my_struct_tag{ struct my_other_struct *other; } my_struct_tag
3
310
by: James Brown | last post by:
I am defining the following typedefs: typedef int* pint; typedef pint* ppint; typedef ppint* pppint; taking the last typedef - "pppint" - would this be referred to as: an alias to type "ppint *" or an alias to type "int ***"
12
8240
by: Thomas Carter | last post by:
Imagine that there is some include file f.h that contains the following line: typedef unsigned int ui32 ; My question is: If I have a C source file F.c that includes f.h, is it possible for the preprocessor to detect that ui32 already exists, when preprocessing F.c? The idea is that F.c will typedef ui32 as above if and only if such typedef is not already in some include file used by F.c.
7
15287
by: Michael B Allen | last post by:
I have a forward reference like: struct foo; int some_fn(struct foo *param); Because the parameter is a pointer the compiler is satisfied. But now I wan to change 'struct foo' to a typedef'd type like 'foo_t'. The following all fail to compile:
12
4649
by: Googy | last post by:
Hi!! Can any one explain me the meaning of following notations clearly : 1. typedef char(*(*frpapfrc()))(); frpapfrc f; 2. typedef int (*(arr2d_ptr)()); arr2d_ptr p; 3. typedef int (*(*(*ptr2d_fptr)()))();
16
2776
by: mdh | last post by:
A quick ? :-) question about Typedefs. There is a very brief discussion about this in K&R ( p146). Googling this group, there is a surprising dearth of questions about these. From one of the threads, there is sound advice ( to me at any rate) not to hide pointers behind typedefs. So, may I ask the group when the use of typedefs really makes sense? Sorry if this is somewhat general, but there are no exercises ( not that I am asking for...
7
18446
by: MJ_India | last post by:
Style 1: struct my_struct { ... }; typedef my_struct my_struct_t; Style 2: typedef struct my_struct {
0
8946
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8777
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
9237
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9184
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8187
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
4551
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3262
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2729
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.