C++: namespace conflict between extern “C” and class member

三世轮回 提交于 2019-12-23 10:26:18

问题


I stumbled upon a rather exotic c++ namespace problem:

condensed example:

 extern "C" {
 void solve(lprec * lp);
 }

 class A {
 public:
    lprec * lp;
    void solve(int foo);
 }

 void A::solve(int foo)
 {
     solve(lp);
 }

I want to call the c function solve in my C++ member function A::solve. The compiler is not happy with my intent:

  error C2664: 'lp_solve_ilp::solve' : cannot convert parameter 1 from 'lprec *' to 'int'

Is there something I can prefix the solve function with? C::solve does not work


回答1:


To call a function in the global namespace, use:

::solve(lp);

This is needed whether the function is extern "C" or not.




回答2:


The C functions are in the global namespace. So try

::solve(lp)



回答3:


Please try ::solve




回答4:


Simply ::solve(lp). Note you also need a semicolon after your class declaration.



来源:https://stackoverflow.com/questions/2706009/c-namespace-conflict-between-extern-c-and-class-member

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!