Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何在c+;中使用友元函数时避免访问错误+;?_C++_Friend_Access Specifier - Fatal编程技术网

C++ 如何在c+;中使用友元函数时避免访问错误+;?

C++ 如何在c+;中使用友元函数时避免访问错误+;?,c++,friend,access-specifier,C++,Friend,Access Specifier,boss中的friend total(boss,employe)声明失败,因为employe尚未声明 在boss上方添加employe的转发声明 16:31:40 **** Incremental Build of configuration Debug for project Practice **** make all 'Building file: ../src/Practice.cpp' 'Invoking: Cross G++ Compiler' g++ -O0 -g

boss
中的
friend total(boss,employe)
声明失败,因为
employe
尚未声明

boss
上方添加
employe
的转发声明

        16:31:40 **** Incremental Build of configuration Debug for project Practice ****
make all 
'Building file: ../src/Practice.cpp'
'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Practice.d" -MT"src/Practice.o" -o "src/Practice.o" "../src/Practice.cpp"
../src/Practice.cpp:20:26: error: 'employe' has not been declared
   20 |  friend void total(boss, employe);
      |                          ^~~~~~~
../src/Practice.cpp: In function 'void total(boss, employe)':
../src/Practice.cpp:38:8: error: 'int boss::salary' is private within this context
   38 |  T = b.salary + e.salary;
      |        ^~~~~~
../src/Practice.cpp:13:6: note: declared private here
   13 |  int salary = 3000;
      |      ^~~~~~
make: *** [src/subdir.mk:20: src/Practice.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

16:31:40 Build Failed. 3 errors, 0 warnings. (took 655ms)

class雇员;//您需要employe类的转发声明。
        16:31:40 **** Incremental Build of configuration Debug for project Practice ****
make all 
'Building file: ../src/Practice.cpp'
'Invoking: Cross G++ Compiler'
g++ -O0 -g3 -Wall -c -fmessage-length=0 -MMD -MP -MF"src/Practice.d" -MT"src/Practice.o" -o "src/Practice.o" "../src/Practice.cpp"
../src/Practice.cpp:20:26: error: 'employe' has not been declared
   20 |  friend void total(boss, employe);
      |                          ^~~~~~~
../src/Practice.cpp: In function 'void total(boss, employe)':
../src/Practice.cpp:38:8: error: 'int boss::salary' is private within this context
   38 |  T = b.salary + e.salary;
      |        ^~~~~~
../src/Practice.cpp:13:6: note: declared private here
   13 |  int salary = 3000;
      |      ^~~~~~
make: *** [src/subdir.mk:20: src/Practice.o] Error 1
"make all" terminated with exit code 2. Build might be incomplete.

16:31:40 Build Failed. 3 errors, 0 warnings. (took 655ms)
class employe; // <--- add this

class boss{
int salary = 3000;

public:
 boss();
 boss(int b){
  salary = b;
 }
 friend void total(boss, employe);
};