全國最多中醫師線上諮詢網站-台灣中醫網
發文 回覆 瀏覽次數:1221
推到 Plurk!
推到 Facebook!

使用標頭檔問題

尚未結案

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#1 引用回覆 回覆 發表時間:2003-12-17 20:46:30 IP:163.23.xxx.xxx 未訂閱
各位先進: 小弟是要練習繼承的用法 想說大概可以在同一個資料夾下讀到 .h 檔應該就沒有問題 結果小弟只是把 class 寫在 .h 檔下 把 constructor 及 class function 寫在 .cpp 檔下 連繼承都還沒開始,只是呼叫 class 來用測試而已 compile 的時候會出現: unresolved external myclass::myclass(int ,int) referenced from objpath / UNITOBJ 小弟用 C Builder 的 console mode 來測試應該沒錯吧? 因為在 VC 下測試沒有問題,是不是小弟沒注意到什麼呢? 還是小弟應該用別的 mode 來開啟呀? 感謝回答
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#2 引用回覆 回覆 發表時間:2003-12-17 20:54:54 IP:140.135.xxx.xxx 未訂閱
流您好: 方便將.h以及.cpp 程式碼PO上來嗎? 好讓有經驗的人可以幫您看看 我用下面code測試可行
//Unit1.cpp
#pragma hdrstop
#include 
#include 
#include "File1.h"
//---------------------------------------------------------------------------
#pragma argsused
int main(int argc, char* argv[])
{
   MyClass Test;
   cout<MyClass::Add(int a,int b)
{
 return (a b);
}
順心 發表人 -

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#3 引用回覆 回覆 發表時間:2003-12-17 22:37:50 IP:163.23.xxx.xxx 未訂閱
taishyang 您好: 
     myclass.h 內容如下:
# ifndef MYCLASS_H
# define MYCLASS_H    # include     class MyClass
{
  friend ostream &operator << ( ostream & , const MyClass &) ;
  public:
    MyClass( int = 0 , int = 0 ) ;
    void setpoint( int , int ) ;
    int getX() const { return x ; }
    int getY() const { return y ; }
  private:
    int x , y ;
} ;
# endif    ....................    myclass.cpp 內容如下:    # include 
# include "myclass.h"    MyClass::MyClass( int a , int b )
{
  setpoint(a,b) ;
}    void MyClass::setpoint ( int a , int b )
{
  x = a ;
  y = b ;
}    ostream &operator << ( ostream &stream , const MyClass &t )
{
  stream << "[ " << t.x << " , " << t.y << " ]\n" ;
  return stream ;
}    ..............................    Unit1.cpp 如下:
# include 
# include 
# include 
# include "myclass.h"    void main(void)
{
MyClass t(20,20) ;
cout << t << "\n" ;
getch();
}    試了很多次,錯誤的地方都修正完後,就是無法用自己定義的 class,錯誤如下:    [Linker Error] 
Unresolved external 'MyClass::MyClass(int, int)' referenced from my_file_path\UNIT1.OBJ    [Linker Error] 
Unresolved external 'operator <<(_STL::basic_ostream >&, const MyClass&)' referenced from my_file_path\UNIT1.OBJ    不過很奇怪,要是把它們全都寫在 Unit.cpp 下,倒是可以 compile
不曉得是小弟哪邊觀念不通,頭大大,可執行的如下(在同一個 cpp 檔中):    # ifndef MYCLASS_H
# define MYCLASS_H    # include     class MyClass
{
  friend ostream &operator << ( ostream & , const MyClass &) ;
  public:
    MyClass( int = 0 , int = 0 ) ;
    void setpoint( int , int ) ;
    int getX() const { return x ; }
    int getY() const { return y ; }
  private:
    int x , y ;
} ;
# endif    # include 
# include "myclass.h"    MyClass::MyClass( int a , int b )
{
  setpoint(a,b) ;
}    void MyClass::setpoint ( int a , int b )
{
  x = a ;
  y = b ;
}    ostream &operator << ( ostream &stream , const MyClass &t )
{
  stream << "[ " << t.x << " , " << t.y << " ]\n" ;
  return stream ;
}    void main(void)
{
MyClass t(20,20) ;
cout << t << "\n" ;
getch();
}    麻煩前輩了,感謝感謝    
發表人 - 流 於 2003/12/17 22:40:43
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#4 引用回覆 回覆 發表時間:2003-12-17 22:59:13 IP:140.135.xxx.xxx 未訂閱
流您好: 我COPY您的程式沒做任何修改可以執行耶 結果如下圖所示 < src="http://delphi.ktop.com.tw/loadfile.php?TOPICID=13239913&CC=296107"> 順心

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#5 引用回覆 回覆 發表時間:2003-12-17 23:15:55 IP:163.23.xxx.xxx 未訂閱
taishyang 您好:    竟然會發生這種事 QQ 小弟的 myclass.h 是一個檔案,小弟的 myclass 又是一個檔案 與 unit.cpp 都放在同一個資料夾下,可是小弟呼叫不成功耶 ~  還有,不是執行下面那一個,下面那一個小弟的也可以.. ^^" 還有,附帶一提: 小弟昨晚在測試的時候 發現只有把
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#6 引用回覆 回覆 發表時間:2003-12-17 23:36:22 IP:140.135.xxx.xxx 未訂閱
流您好:  我把專案上傳至下面連結,看看是不是您所說的情況 < href="http://delphi.ktop.com.tw/topic.php?TOPIC_ID=42303">http://delphi.ktop.com.tw/topic.php?TOPIC_ID=42303 順心

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#7 引用回覆 回覆 發表時間:2003-12-18 00:04:40 IP:163.23.xxx.xxx 未訂閱
taishyang 您好: 對呀,就是這樣分開的 但說出來您可能不相信! 用您上傳的檔案來 compile 出現一樣的錯誤 QQ 選檔的步驟是: [File] → [Open] → unit.cpp ,再 compile 應該不會是沒註冊或沒 update 造成的吧? 如果選檔的步驟是: [File] → [Open] → Project2.bpr ,再 compile 會出現...[Linker Fatal Error] Fatal: Unable to open file 'RZ30CTLS60.BPI' 感謝回答
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#8 引用回覆 回覆 發表時間:2003-12-18 00:15:21 IP:140.135.xxx.xxx 未訂閱
流您好:
引言: 選檔的步驟是: [File] → [Open] → unit.cpp ,再 compile 應該不會是沒註冊或沒 update 造成的吧? 如果選檔的步驟是: [File] → [Open] → Project2.bpr ,再 compile 會出現...[Linker Fatal Error] Fatal: Unable to open file 'RZ30CTLS60.BPI'
抱歉請重新下載,並利用您說的第二個步驟選檔 [File] → [Open] → Project2.bpr ,再 compile 個人疏失造成您的困擾請見諒

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#9 引用回覆 回覆 發表時間:2003-12-18 00:40:00 IP:163.23.xxx.xxx 未訂閱
taishyang 您好: 先讓小弟感謝您,您每次都很熱心的幫忙小弟! 不過這次還是不行,依第二個步驟,compile出現一樣的錯誤 其實是先出現一個對話框,內容寫著 Unable to find package import : Rz30Ctls60.bpi 按了好幾下 cencel 後才出現 [Linker Fatal Error] Fatal: Unable to open file 'RZ30CTLS60.BPI' 依第一個步驟的話,是先出現一個 MessabeBox,內容如下: would you like to create a project so this file can compile and run ? 小弟也有試過把您檔案中的 myclass.h 及 myclass.cpp 複製到一個資料夾下 開一個 unit1.cpp 檔 ,再 include "myclass.h" 進來,但依然出現錯誤 就是兩個 unresolved external 的錯誤 再一次感謝,謝謝您
taishyang
站務副站長


發表:377
回覆:5490
積分:4563
註冊:2002-10-08

發送簡訊給我
#10 引用回覆 回覆 發表時間:2003-12-18 13:55:34 IP:140.135.xxx.xxx 未訂閱
流您好: 我只能說真是太神奇了 我下載到學弟的電腦也是可以 > <>< face="Verdana, Arial, Helvetica">引言: 其實是先出現一個對話框,內容寫著 Unable to find package import : Rz30Ctls60.bpi 按了好幾下 cencel 後才出現 [Linker Fatal Error] Fatal: Unable to open file 'RZ30CTLS60.BPI' 您可以在專案設定中的Include以及Lib Path將不必要的路徑給刪除 應該可以解決 順心 <>~我也是在學習的階段,回答的不好請您多多見諒與指教~

中階會員


發表:36
回覆:142
積分:70
註冊:2003-07-24

發送簡訊給我
#11 引用回覆 回覆 發表時間:2003-12-18 23:09:46 IP:163.23.xxx.xxx 未訂閱
taishyang 您好:    小弟沒測試您說的方法,因為畢竟那不是長久之計 呵,總不能要一個 class 就請版上前輩幫忙測試再上傳給小弟下載吧 ^^" 小弟就此結案了,應該說是認命了... 這是第二次發生了,但真的不是小弟故意找您麻煩,是真的有此問題存在 希望小弟下次發問時不要再遇到這樣很烏龍的狀況了 很感謝前輩您幫忙測試,花了您相當多時間    謝謝您
系統時間:2024-05-21 11:14:11
聯絡我們 | Delphi K.Top討論版
本站聲明
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。
2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。
3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇!