Solved

namespace requirement inside class member function

Posted on 2002-06-21
15
298 Views
Last Modified: 2010-04-01
What is the namespace requirement inside class member function?

namespace WinX_Namespace
{
     class Widget{
     public:
           bool Function(){return false;};
     };
}

class foo : public WinX_Namespace::Widget
{
public:
 bool Function();
};

//In cpp file
bool foo::Function()
{
 return Widget::Function();//Does this need namespace
}


Does the function call to Widget::Function also need the namespace WinX_Namespace.

I can compile the above code to VC++ with no problem, but it will not compile in an Apex C++ compiler.

If I change the code like this
bool foo::Function()
{
 return WinX_Namespace::Widget::Function();
}
Then it will compile in the Apex compiler, but it does not compile in the VC++ compiler.

The VC++ compiler gives me this error
error C2352: 'WinX_Namespace::Widget::Function' : illegal call of non-static member function

So occurring to the C++ standards, which one of these method are right, and which one is wrong?
0
Comment
Question by:Zulma9999
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 30

Expert Comment

by:Axter
Comment Utility
Technically speaking, both methods should have been able to compile, so I would say they're both wrong.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>error C2352: 'WinX_Namespace::Widget::Function' : illegal call of non-static member function

The compiler is absolutely correct about this:

bool foo::Function()
{
return WinX_Namespace::Widget::Function();
}

bool foo::Function()
{
return Widget::Function();
}

are both calls to a nonstatic member of "Widget". You probably wanted to use

bool foo::Function()
{
WinX_Namespace::Widget w;
return w.Function();
}

or

bool foo::Function()
{
Widget w;
return w.Function();
}

respectively.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>re both calls to a nonstatic member of "Widget". You >>probably wanted to use

According to the questioner's code, these are calls to the parrent function.  Remember foo is a decedent of Widget.
So calling Widget::Function(), should call the parrent function.

Moreover, you can accomplish the same thing by calling ::Function(), which should also compile on both compilers.
0
 

Author Comment

by:Zulma9999
Comment Utility
jkr,
No, I don't want to call a new instance of Widget.  I want to call the base class member function.
0
 

Author Comment

by:Zulma9999
Comment Utility
Axter,
>>Moreover, you can accomplish the same thing by
>>calling ::Function(), which should also compile on both
>>compilers.

In the example I posted, this could fix the problem, but what if I have to parent classes with the same Function name, and I want to specify which base class function I want to use?

namespace WinX_Namespace
{
     class Widget1{
     public:
           bool Function(){return false;};
     };
     class Widget2{
     public:
           bool Function(){return true;};
     };
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
 bool Function1();
 bool Function2();
};

//In cpp file
bool foo::Function1()
{
 return Widget1::Function();
}

//In cpp file
bool foo::Function2()
{
 return Widget2::Function();
}

Now I can't use ::Function call because the compiler is not going to know which one to pick.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
In this case, you can try adding using namespace to your function.
Example:
bool foo::Function1()
{
     using namespace WinX_Namespace;
     return Widget1::Function();
}

That should make both VC++ happy and Apex happy.
0
 

Author Comment

by:Zulma9999
Comment Utility
>>In this case, you can try adding using namespace to your
>>function.
That method would pull everything in the namespace, which I don't want to do.
I'm sorry to be so picky, but I'm trying to port some code, and I want to make sure it comes out right.

namespace WinX_Namespace
{
     class Widget1{
     public:
          bool Function(){return false;};
     };
     class Widget2{
     public:
          bool Function(){return true;};
     };
     bool SomeXFunction(){return false;}
}

bool SomeXFunction()
{
     return true;
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
     bool Function1();
     bool Function2();
};

//In cpp file
bool foo::Function1()
{
     using namespace WinX_Namespace;
     bool x = SomeXFunction(); //Error here
     return Widget1::Function();
}

If I use your method I get this error from VC++
error C2668: 'SomeXFunction' : ambiguous call to overloaded function
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 30

Expert Comment

by:Axter
Comment Utility
Ok, this is not going to be pretty, but it's the only method I can think of which will let your code compile on both compilers correctly.

When calling the base function, wrap {} around the function, and inside the {}, and before the function call, add a using namespace.
The curly cues will prevent the using namespace from pulling everything in the namespace into the entire function.  The using namespace stays within side of the scope of the curly cues {}
Example:

namespace WinX_Namespace
{
     class Widget1{
     public:
          bool Function(){return false;};
     };
     class Widget2{
     public:
          bool Function(){return true;};
     };
     bool SomeXFunction(){return false;}
}

bool SomeXFunction()
{
     return true;
}

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
public:
     bool Function1();
     bool Function2();
};

//In cpp file
bool foo::Function1()
{
     bool x = SomeXFunction();
     {
          using namespace WinX_Namespace;
          if (x) return Widget1::Function();
     }
     SomeXFunction();
     WinX_Namespace::SomeXFunction();
     {
          using namespace WinX_Namespace;
          return Widget1::Function();
     }
}

//In cpp file
bool foo::Function2()
{
     bool x = SomeXFunction();
     {
          using namespace WinX_Namespace;
          if (x) return Widget2::Function();
     }
     SomeXFunction();
     WinX_Namespace::SomeXFunction();
     {
          using namespace WinX_Namespace;
          return Widget2::Function();
     }
}

0
 

Author Comment

by:Zulma9999
Comment Utility
Axter,
That works, but I like to keep this question open to see if any one else has a better method.
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
what does your Apex compiler say about the following, VC doesnt complain..

namespace WinX_Namespace
{
    class Widget1{
    public:
          bool Function(){return false;};
    };

    class Widget2{
    public:
          bool Function(){return false;};
    };
}

typedef WinX_Namespace::Widget1 wid1;
typedef WinX_Namespace::Widget2 wid2;

class foo : public wid1, public wid2
{
public:
bool Function();
};

//In cpp file
bool foo::Function()
{
     wid1::Function();
     return wid2::Function();
}

0
 

Author Comment

by:Zulma9999
Comment Utility
ambience,
Good try, but that puts wid1 & wid2 into the global namespace.  I'm trying to keep object in WinX_Namespace out of the global namespace.
0
 
LVL 30

Accepted Solution

by:
Axter earned 500 total points
Comment Utility
You could try the following in your class.

class foo : public WinX_Namespace::Widget1, WinX_Namespace::Widget2
{
#ifdef  APEX
     typedef WinX_Namespace::Widget1 Widget1;
     typedef WinX_Namespace::Widget2 Widget2;
#endif//APEX
public:
     bool Function1();
     bool Function2();
};

Unfortunatly, these typedef's don't compile on VC++, but if it compiles in your Apex compiler, it should allow you to keep the rest of your code the same in both VC++ and Apex, and it should not effect the global namespace.
0
 
LVL 22

Expert Comment

by:ambience
Comment Utility
Axter is right, in fact i anticipated that issue .. and i think i was going to suggest the same as Axter has mentioned.

Axter, i tried that

class foo : public WinX_Namespace::Widget1, public WinX_Namespace::Widget2
{
     typedef WinX_Namespace::Widget1 wid1;
     typedef WinX_Namespace::Widget2 wid2;
public:
bool Function();
};

and it compiled fine on VC++, no issues.
0
 
LVL 30

Expert Comment

by:Axter
Comment Utility
>>and it compiled fine on VC++, no issues.

Yes, it'll compile the way you have it, but the example I posted does not rename the base class.
It uses the same name, so you don't have to use a different name in your decedent class implementation.

That should fource the Apex compiler to work just like the VC++ compiler, in accepting code using just the class name with out the namespace.
0
 

Author Comment

by:Zulma9999
Comment Utility
ambience, I posted additional points for you.

http://www.experts-exchange.com/jsp/qManageQuestion.jsp?ta=cplusprog&qid=20323276


Thank you both for your help, and sorry it took me so long to get back to this question.
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now