課程
/后端開發
/C++
/C++遠征之模板篇
不可訪問 為什么還能編譯通過?
2019-04-27
源自:C++遠征之模板篇 2-2
正在回答
在Match.h中不能包含Time.h,只能用class Time預聲明有這個類,我就是去掉time的頭文件,和老師一樣就行了
我遇到過這個問題,就是因為你的time.h中沒有加 using namespace std; 導致的, 但我不知道為什么?
你的并沒有編譯通過 很可能時你聲明友元函數時名字與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; }
執行不變錯。是為什么呢?
+1一樣
編譯沒有通過???你應該是其他地方有問題
舉報
本C++教程力求即學即會,所有知識以實踐方式講解到操作層面
Copyright ? 2025 imooc.com All Rights Reserved | 京ICP備12003892號-11 京公網安備11010802030151號
購課補貼聯系客服咨詢優惠詳情
慕課網APP您的移動學習伙伴
掃描二維碼關注慕課網微信公眾號
2020-03-14
在Match.h中不能包含Time.h,只能用class Time預聲明有這個類,我就是去掉time的頭文件,和老師一樣就行了
2020-02-09
我遇到過這個問題,就是因為你的time.h中沒有加 using namespace std; 導致的, 但我不知道為什么?
2019-10-15
Demo.cpp
Match.h
Match.cpp
Time.h
Time.cpp
2019-10-13
執行不變錯。是為什么呢?
2019-09-25
+1一樣
2019-04-29
編譯沒有通過???你應該是其他地方有問題