能extern一個struct嗎? |
尚未結案
|
frederick
中階會員 發表:101 回覆:108 積分:66 註冊:2002-08-01 發送簡訊給我 |
請問一下:
一般兩個檔案之間如果變數要互通,
是用extern來達到目的。
但extern好像是extern變數而已,
可不可以extern一個struct呢?
--------------------------------
我在formA開頭建立一struct
struct struct_tmp{
int iSetSelect;
}FormInfo; 我在formA的任一副程式都可
用FormInfo.iSetSelect取得 / 設定其值 我在FormB開頭寫下:extern FormInfo;
compiler是沒錯。但如用到FormInfo.iSetSelect
就會出現取不到其變數。 還是說extern只能用於變數而已?
我的目的是Form間互通一些資訊,
還是有比較好的作法呢?
謝謝!!!
|
RaynorPao
版主 發表:139 回覆:3622 積分:7025 註冊:2002-08-12 發送簡訊給我 |
引言: 請問一下: 一般兩個檔案之間如果變數要互通, 是用extern來達到目的。 但extern好像是extern變數而已, 可不可以extern一個struct呢? -------------------------------- 我在formA開頭建立一struct struct struct_tmp{ int iSetSelect; }FormInfo; 我在formA的任一副程式都可 用FormInfo.iSetSelect取得 / 設定其值 我在FormB開頭寫下:extern FormInfo; compiler是沒錯。但如用到FormInfo.iSetSelect 就會出現取不到其變數。 還是說extern只能用於變數而已? 我的目的是Form間互通一些資訊, 還是有比較好的作法呢? 謝謝!!!frederick 你好: 當然可以,請參考以下的連結文章 class="code"> 備註: (1)struct.h typedef struct struct_tmp { int iSetSelect; } FormInfo; (2)FormA.cpp #include "struct.h" FormInfo fi; (3)FormB.cpp #include "struct.h" extern FormInfo fi; -- Enjoy Researching & Developing -- 發表人 - RaynorPao 於 2003/03/05 15:30:38
------
-- 若您已經得到滿意的答覆,請適時結案!! -- -- 欲知前世因,今生受者是;欲知來世果,今生做者是 -- -- 一切有為法,如夢幻泡影,如露亦如電,應作如是觀 -- |
lgyui
初階會員 發表:21 回覆:29 積分:30 註冊:2003-01-31 發送簡訊給我 |
嗯嗯!!
基本上, extern 是用來 使用外部變數(include function variable).
宣告 struct 是宣告一個資料型態的哦,就如同你宣告一個Class一樣(just data type)
所以你只能 extern 一個資料型態為struct struct_tmp的變數
for example :
-----------------------------source_comm.hpp
typedef struct struct_tmp {
int somethings;
} stmp;
-----------------------------source_01.cpp
#include "source_comm.hpp"
stmp testme;
-----------------------------Source_01.cpp
#include "source_comm.hpp"
extern stmp testme;
|
BGman
初階會員 發表:28 回覆:85 積分:42 註冊:2003-01-10 發送簡訊給我 |
引述 請問一下:
一般兩個檔案之間如果變數要互通,是用extern來達到目的。
但extern好像是extern變數而已,可不可以extern一個struct呢? struct 是一種型態,和 int,char 一樣都是型態,
差別只在於他是C 提供給使用者自訂的型態!
我在formA 開頭建立一 struct struct struct_tmp { int iSetSelect; }FormInfo;我在formA 的任一副程式都可用FormInfo.iSetSelect取得 / 設定其值 我在FormB 開頭寫下:extern FormInfo; compiler是沒錯。但如用到FormInfo.iSetSelect 就會出現取不到其變數。 我看你在用Struct 怪怪的,><> ><>和 > 還是說>一般來說 > > 發表人 - |
本站聲明 |
1. 本論壇為無營利行為之開放平台,所有文章都是由網友自行張貼,如牽涉到法律糾紛一切與本站無關。 2. 假如網友發表之內容涉及侵權,而損及您的利益,請立即通知版主刪除。 3. 請勿批評中華民國元首及政府或批評各政黨,是藍是綠本站無權干涉,但這裡不是政治性論壇! |