Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

declare a C function as friend inside CPP class

Tags:

c++

c

I need to use private variable of a class inside a C function. I was doing something like this

class Helper
{
private:
    std::string name;
public:
    std::getName(){return name;}
friend extern "C" void initializeHelper();
};

but this code segment gives error unqualified-id before string constant extern "C" {

I am not able to identify what I am doing wrong here.

like image 747
rajenpandit Avatar asked Dec 20 '22 05:12

rajenpandit


1 Answers

Just forward-declare this function before your class:

extern "C" void foo();

Then you can use it in friend-declaration:

class A {
public:
  A() {}
private:
  friend void foo();
  int a;
};
like image 159
PiotrNycz Avatar answered Jan 08 '23 00:01

PiotrNycz