|
-
August 30th, 2002, 04:22 PM
#1
Is this piece of code correct ?
Hei all !
I have an BIG application consisting of tens of files i developed in VC++ 6.0 that compiled and ran great. Now i installed the VC++ .NET instead and it gives me compiler error
C2879 -
error C2879: 'DataCoach::globalMemFile' : only an existing namespace can be given an alternative name by a namespace alias definition
The following cxode which compiled great before under VC++:
[ccode]
//DataCoach_globalMemFile.h follows
class DataCoach::globalMemFile: public CMemFile
{
} //the class is not empty but here is for simpl. reasons
//...
//DataCoach_globalMemFile.cpp follows
#include "DataCoach_globalMemFile.h"
namespace globalMemFile = DataCoach::globalMemFile;
//...
[/ccode]
I might suspect some warning level raised which causes the warning in previous versions become an error. I am well aware of the C++ standard, but this issue is quite complicated just to lookup in the standard specification, so I am asking you gurues - Is this code legal ? Should it be ? Under which standard it should be legal ? Is Microsoft again inventing its own "language extensions" ....
Basically all i care is Bjarne Strustrup's C++ cpecs, not Microsofts, but here it would require to swap approx. 1000 lines of code because of this, so i basically need to REMOVE the error without changing the code - if it compiled before it just as well might compile now , rite ? 
Thanks a lot,
i hope others will benefit from this too..
Amn.
-
August 30th, 2002, 04:31 PM
#2
Post a small, compilable piece of code that demonstrates this (the code that compiles with VC 6 but not VC 7). You also don't need to inherit from CMemFile if it is not important to demonstrate the error.
Regards,
Paul McKenzie
-
August 30th, 2002, 04:38 PM
#3
class A
{
class B;
};
class A::B
{
};
namespace C = A::B; //compiler error C2879
Why in all **** do i need this ?
Well if one has 5 classes, each has a comfortable name (typically 6-9 chars), then defining functions in the innermost class would be a nightmare - f.e Alpha::Beta::Gamma::Epsilon::Zeta: oSomeStuff()
By shortening reference like namespace Zeta = Alpha::Beta::Gamma::Epsilon::Zeta and then defining Zeta: oSomeStuff (provided Zeta is the only Zeta used in the file)(along with 10 other functions, with only ONE mapping) - This seems a good option doesnt it ?
Paul, sorry for over explaining but at least you know where i am heading with this.
Thanks for reply, now what you think ?
Amn, Norway.
-
August 30th, 2002, 04:53 PM
#4
I believe this is called "namespace aliasing". I did not think it extended to class names. Perhaps it's not supposed to and was fixed in VC 7.
Somebody else can probably answer better if namespace aliasing can be used in this manner.
Jeff
-
August 30th, 2002, 05:43 PM
#5
Amn
Just a miscellaneous information regarding the above:
1. C# (dont have any opinion on it, were just testing) DOES TREAT classes as namespaces and the code above ported to C# compiles well.
2. The issues appears to be a hot discussion topic among c++ programmers and designers still and the results might end up in next generation C++ RFC document update.
3. Personally i think classes SHOULD be treated as namespaces, at least in aliasing. Here are pros and cons
Pros:
the '::' is used for both classes and namespaces and often it is problematic to have to distinguish what is used - a class name or namespace name, so generally relations between names can be generalized on the common ground.
class is a scope, and namespace is a scope. Scope is defined as hint for compiler to help avoid ambiguities in code parsing process. Ambiguities are resolved the same way in both namespaces and classes.
Static members of a class are on the language level the same thing as any member of a namespace (one exception).
Cons:
A same namespace can be extended in different modules when declared as one, while a class can't (hardcoded) and needs to be derived from to extend functionality.
Class , or at least non-static part of it, describes a template for object creation while namespace only serves the purpose of visual/logic aid to programmer and cannot describe any object template by nature.
-----------------------------------
Well, apart from that i can say my big program only benefits of so-called "class-antialiasing", it spared me hours of precious code design time, and that is what counts. I didn't find any negative features by using that scheme and the code is very rich on features, it employs nearly every single c++ aspect.
Anybody know any online C++ reference ?
My thick C++ book eludes the issue ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|