vs 2013 友元函数不可访问私有函数 但编译可通过 求解释
不可访问 为什么还能编译通过?
不可访问 为什么还能编译通过?
2019-04-27
你的并没有编译通过 很可能时你声明友元函数时名字与Match里不一致
Demo.cpp
#include<iostream> #include<stdlib.h> #include"Time.h" #include "Match.h" using namespace std; //void printTime(Time &t); int main(void) { Time t(6,34,25); Match m; m.PrintTime(t); system("pause"); return 0; } //void printTime(Time &t) //{ // cout<<t.m_iHour<<":"<<t.m_iMinute<<":"<<t.m_iSecond<<endl; //}
Match.h
#ifndef STUDENT_H #define STUDENT_H class Time; class Match { public: void PrintTime(Time &t); }; #endif
Match.cpp
#include"Match.h" #include"Time.h" #include<iostream> using namespace std; void Match::PrintTime(Time &t) { cout<<t.m_iHour<<":"<<t.m_iMinute<<":"<<t.m_iSecond<<endl; }
Time.h
#ifndef TIME_H #define TIME_H #include "Match.h" #include<iostream> using namespace std; class Time { friend void Match::PrintTime(Time &t); public: Time(int hour,int min,int sec); private: int m_iHour; int m_iMinute; int m_iSecond; }; #endif
Time.cpp
#include "Time.h" Time::Time(int hour,int min,int sec) { m_iHour=hour; m_iMinute=min; m_iSecond=sec; }
举报