Skip to main content
gamedev.net
🔒 Locked

very simple question

Started by smittix 11 replies 600+ views
Original Post
smittix
smittix
Could anyone tell me why this gives me errors? class CXfileEntity; CXfileEntity* m_xFileEntity; m_xFileEntity = 0; Thanks
GnuVince
GnuVince
m_xFileEntity is of type CXfileEntity and you are assigning it an integer.
Colin Jeanne
Colin Jeanne
quote:
Original post by GnuVince
m_xFileEntity is of type CXfileEntity and you are assigning it an integer.

m_xFileEntity is a pointer to a CXfileEntity. The code is valid.

What errors are you getting?


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
eleusive
eleusive
quote:
Original post by GnuVince
m_xFileEntity is of type CXfileEntity and you are assigning it an integer.


Actually, this is legal, as it sets the pointer to NULL(NULL is defined as 0 anyways)
"What are you trying to tell me? That I can write an O(N^2) recursive solution for a 2-dimensional knapsack?" "No, programmer. I'm trying to tell you that when you're ready, you won't have to." -Adapted from "The Matrix"
smittix
smittix
The error messages I am getting are:

''m_xFileEntity'' : missing storage-class or type specifiers
''m_xFileEntity'' : ''int'' differs in levels of indirection from ''CXfileEntity *''
Colin Jeanne
Colin Jeanne
Try using more than just a forward declaration before the line

m_xFileEntity = 0;


Thanks Salsa!Colin Jeanne | Invader''s Realm
"I forgot I had the Scroll Lock key until a few weeks ago when some asshole program used it. It even used it right" - Conner McCloud
Tylon
Tylon
I wouldnt be surprised if some compilers complain about assigning 0 to a pointer. It is implicitly identified as an integer, while NULL is typed to be (* void).

[edited by - Tylon on May 4, 2004 6:28:31 AM]
Polymorphic OOP
Polymorphic OOP
quote:
Original post by Tylon
I wouldnt be surprised if some compilers complain about assigning 0 to a pointer. It is implicitly identified as an integer, while NULL is typed to be (* void).

[edited by - Tylon on May 4, 2004 6:28:31 AM]


In C that would possibly be true, though it should NOT be true as assigning 0 to a pointer is valid. This however, is even moreso not true in C++. In fact, in C++ standard libraries NULL is literally #define'd as 0, not as 0 cast to a void pointer. According to the C++ standard, the literal value 0 can be assigned to pointers with no problems, therefore you will not get a warning. Anyways, if in C++ NULL was defined as(void*)0 your code wouldn't compile unless you were using void pointers because in C++ (unlike C) the conversion from void* to other pointer types is NOT implicit. Moreso, since in the standard libraries NULL is defined as 0, if your compiler were to give you a warning with an assignment from 0, the same warning that you argue could occur when using 0 would be true for when you use NULL as well. This, of course, is not the case for any compiler.

In fact, in C++, it is usually recommended to use 0 instead of NULL in all cases because it is possible for someone to #define NULL to be something different, not to mention the fact that in order to be able to use NULL you'd have to either include a file which declares it or define it yourself. In general, avoid using NULL in all cases. 0 is your friend.

[edited by - Polymorphic OOP on May 4, 2004 6:39:43 AM]
McZ
McZ
quote:
Original post by bengaltgrs
The error messages I am getting are:

''m_xFileEntity'' : missing storage-class or type specifiers
''m_xFileEntity'' : ''int'' differs in levels of indirection from ''CXfileEntity *''


do you have a body for the class ?

class CXfileEntity {
// some stuff
// e.g.
int myvalue;
};
CXfileEntity* m_xFileEntity;
m_xFileEntity = 0;
Polymorphic OOP
Polymorphic OOP
quote:
Original post by McZ
quote:
Original post by bengaltgrs
The error messages I am getting are:

''m_xFileEntity'' : missing storage-class or type specifiers
''m_xFileEntity'' : ''int'' differs in levels of indirection from ''CXfileEntity *''


do you have a body for the class ?

class CXfileEntity {
// some stuff
// e.g.
int myvalue;
};
CXfileEntity* m_xFileEntity;
m_xFileEntity = 0;



He doesn''t''t need a class definition, only the declaration. All he''s doing is declaring a pointer to an instance and setting the pointer to 0 which just requires the type to be known, not the type''s definition.

McZ
McZ
quote:

He doesn''t''t need a class definition, only the declaration. All he''s doing is declaring a pointer to an instance and setting the pointer to 0 which just requires the type to be known, not the type''s definition.



if so.. then I don''t know, it was just a thougth I had.
smittix
smittix
I changed the code to:

class CXfileEntity;
CXfileEntity* m_xFileEntity = 0;

It compiles fine now, but does this mean the same as:

class CXfileEntity;
CXfileEntity* m_xFileEntity;
m_xFileEntity = 0;

Thanks
Jingo
Jingo

class CXfileEntity;
CXfileEntity* m_xFileEntity;
m_xFileEntity = 0;


has the same result as


class CXfileEntity;
CXfileEntity* m_xFileEntity = 0;


and


class CXfileEntity* m_xFileEntity = 0;


Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.