Advertisement

msvc++ trivia

Started by April 08, 2001 04:44 PM
8 comments, last by Succinct 23 years, 10 months ago
why does
  namespace NameSpaceA
{
    class Class 
    { 
    public: 
        virtual void Func() {} 
    };
}

namespace NameSpaceB
{
    class Class 
    { 
    public: 
        virtual void Func() {} 
    };
}

namespace NameSpaceC
{
    class Class: public NameSpaceA::Class,public NameSpaceB::Class 
    { 
    public: 
        void Func() { NameSpaceA::Class::Func(); } 
    };
}  
give me graphics.h(39) : error C2352: 'NameSpaceA::Class::Func' : illegal call of non-static member function i bet it'll compile under borland... anybody have any ideas or workarounds for this one? I'm sure i could go and incorporate the namespace's name into the classes' names, but then why even support namespaces? Thank you for your bandwidth. ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~Succinct Demos Online~ /edit, i added the "virtual" function modifiers, which i'd accidentally omitted, originally Edited by - succinct on April 9, 2001 7:14:33 PM
-- Succinct(Don't listen to me)
This compiles under VC++:

  namespace NameSpaceA{    class Class	{	public:		void Func() {}    };}namespace NameSpaceB{    	class Class	{     	public:		void Func() {}    };}namespace NameSpaceC{    class Class: public NameSpaceA::Class,public NameSpaceB::Class	{	public:		void Func() 		{ 			using namespace NameSpaceA;			Class::Func();		}    };}  


Hope I could help you,
Gero Gerber
Advertisement
I''m pretty sure the reason it won''t compile is because you haven''t declared any variables of NameSpaceA::Class, so its member functions aren''t loaded into memory, so the other can''t access them. If you make the function static, it should work. That might not be the problem though, since Gero''s doesn''t have any vars declared either, but that''s my guess anyway.



-Deku-chan

DK Art (my site, which has little programming-related stuff on it, but you should go anyway^_^)
Deku''s right. The error is exactly what was reported: you are trying to call a non-static class member function without an object of that class.

I''m fairly certain Gero''s code compiles, but will never work. Notice he says "using NameSpaceA", and tries to call Class::Func without explicitly specifying the namespace. The code compiles because Func is a member function in the current namespace (which has just had NameSpaceA added to it; it hasn''t replaced the current namespace). In other words, the code will this->Func () from Func (), which should give you a nice endless loop.

It won''t compile as Succinct has it under borland or gcc. It''s just wrong. Either create an object of class and call Func through it, or make Func static.
Put this in the member function:


static_cast&ltNameSpaceA::Class*>(this)->Func();



Stoffel: A NameSpaceC::Class instance IS_A NameSpaceA::Class instance.
Ahhh, right you are. I didn''t see what he was trying to do. I try to avoid hiding functions, so I''m not used to the methods used.

This would be the same as doing:
((NameSpaceA::Class*) this)->Func (); // right?
Advertisement
ahh, null_pointer, i forgot to add, those are virtual functions...
damn!!!

see, i tried that, too, and all it did was call the most recently overridden class''s virtual function.

stoffel-> there is an object, this, trying to call a virtual function that it has overridden in the overriding function.

this works with single inheritance. this works w/ multiple inheritance. it doesn''t seem to want to work using multiple inheritance but with identical parent names that are in different namespaces.

this function is not static, and neither is the function i''m trying to call, and they both share the same object. it is just further up the inheritance chain.

see, this compiles if you name each class differently, so that you can remove the namespace tag. i don''t see why it gives an error when you include the namespace, because i thought that was the whole purpose of namespaces, so that conflicting names can be resolved. well, these are some pretty dag-gone conflicting names, but they''re in different namespaces...

i agree w/ stoffel on this one, Gero Gerber, that''s just adding a new namespace, but calling the most current version of the function. I didn''t try compiling and running it, but i also think it would be an infinite loop. i''ll try it, though, just to check.

i''m also going to add the "virtual" modifiers to the original post, to clear up the confusion i produced...

-succinct
-- Succinct(Don't listen to me)
Stoffel: Yes, they''re the same. I find that the C++ cast operators make my code more descriptive and help to locate potential problems.


Succinct:

Hmm...yes, I see where virtual functions would cause a problem with my code.

I am more inclined to think that MSVC simply has trouble working with namespaces. I can see why it would confuse a poor implementation, but I cannot see why referring to the class function through the namespace shouldn''t work.

You''ll either have to rename the classes or the methods (preferrably the latter), or remove the virtual keyword. If I might ask, what are the real names of the namespaces, classes, and functions that cause the problem? Perhaps there are synonyms you could use?
Or try this:


namespace NameSpaceC
{

class Class : public NameSpaceA::Class, public NamespaceB::Class
{
typedef NameSpaceA::Class BaseClassA;
typedef NameSpaceB::Class BaseClassB;

public:
virtual void Func()
{
BaseClassA::Func();
BaseClassB::Func();
}
};

};



That should eliminate the compiler problems. (Why didn''t I think of this earlier?)
woohoo! that looks like it''ll work, null_pointer. thank you (and everybody else).

the namespaces are Standard and OGL, each having a basic IObject class. The Standard::IObject defines the interface necessary for 2-stage construction/destruction, and gives all objects deriving from it methods to do this, and the OGL::IObject give all objects methods to upload to the OGL server, unload from it, render, and also gives them access to static methods that retrieve system info such as output (window) width and height, screen width and height,and inverses of these, etc...

I could just rename the IObjects to reflect the namespaces, but thats all they really are, a standard object interface, and an OGL object interface, so using namespaces made more sense... oh well. i''m probably going to rename them, cuz adding the extra couple of typedefs adds more clutter to an already large class (OS::CVideoOutput), but it still makes me mad that msvc makes me do this... oh well, maybe i need to go to borland, code warrior, or hope that Visual.NET fixes this

thanks, again, guys!

-succinct
-- Succinct(Don't listen to me)

This topic is closed to new replies.

Advertisement