Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

namespace requirement inside class member function

Posted on 2002-06-21
15
Medium Priority
?
308 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 2
  • +1
15 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 7099027
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
ID: 7099053
>>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
ID: 7099066
>>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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:Zulma9999
ID: 7099073
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
ID: 7099089
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
ID: 7099127
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
ID: 7099149
>>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
 
LVL 30

Expert Comment

by:Axter
ID: 7099164
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
ID: 7099949
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
ID: 7102674
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
ID: 7103422
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 2000 total points
ID: 7103745
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
ID: 7106723
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
ID: 7107063
>>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
ID: 7153316
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
Suggested Courses

609 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