posted on 2005-12-20 23:34
vince yuan
在2023年年中的時候,公司就準備轉移到visual studio 2005上開發產品。本人有幸參與了公升級的過程,成功的把30個左右solutions、幾百個projects公升級到了vc8。由於專案眾多,並且專案還在持續開發中,我們的基本策略是讓**同時在vc7.1和vc8下面編譯通過,最後再完全轉到vc8,鏈結並執行成功。從總體上說vc8比vc7.1語法上嚴格且嚴謹。下面是我的一些經驗總結。文中只涉及到編譯問題,鏈結的問題沒有包含在內。我的同事mr han對此文有巨大貢獻,在此非常感謝。
1. 變數作用域
在vc7.1中, 如果乙個變數定義在for語句的條件從句中,那麼這個變數可以在for之後使用。但vc8禁止這樣,會報告乙個c2065錯誤.
for(
inti =0
; i
<
10;
++i)
if
(i <
10)..
//error in vc8
for(i =0
; i
<5;
++i)
//error in vc8
解決方法:
在for語句之前宣告變數(可保證**在vc7.1和vc8下同時編譯通過)
inti =0
;
for(i =0
; i
<
10;
++i)
for(i =0
; i
<5;
++i)
2. 指標和引用的宣告
在vc7.1中, 下面的**可以編譯, 但是vc8會報c4430 錯誤。(很難想象有些美國程式設計師竟然這樣宣告)
const
&int
a; //
error in vc8
const
*int
b;
//error in vc8
intmyfun (
const
&b);
//error in vc8
解決方法:
把* 或&放到型別的後面.
const
int&
a; const
int*
b;int
myfun (
constb&
);
3. 預設int型別
在vc7.1中,如果定義乙個變數但不宣告型別,那麼預設為int。vc8不支援。
statici =
0;
//c4430 error in vc8
consti =
0;
//c4430 error
解決方法:
加上int.
static
inti =0
; const
inti =0
;
4. 函式的預設返回值型別
同上,vc8不支援把 int 作為預設返回值類
func()
;
//error in vc8
解決方法:
明確宣告函式返回值型別為 int.
intfunc()
;
5. 函式位址
vc7中函式名就是位址。在vc8中,必須要使用&操作符同時寫出這個方法的全名(fully qualified name).
class
a ;
void
fun(
int(a::
*test) (
void
));int
main()
解決方法:
加上 &.
fun(
&a::test);
6. 隱式型別轉換
vc8不允許b* 到const b*&的隱式轉換.
classb{}
;void
fun (
constb*
&);
//if possible use const b* instead
intmain()
解決方法:
強制轉換或函式引數變成const b*。
void
fun (
constb*
);
7. 友元方法(friend function)
vc8不允許宣告乙個private或protected函式為友元.
class
a ;
class
b ;
解決方法 1:
宣告友元類.
class
a ;
class
b ;
解決方法 2:
把函式宣告為public
class
a ;
class
b ;
8. stl的stdext 命名空間
在vc8中,hash_map 和hash_set 被移進了stdext命名空間中.
#include
<
hash_map
>
std::hash_map
//error in vc8
解決方法:
使用stdext 命名空間.
#include
<
hash_map
>
stdext::hash_map
9. 標頭檔案
許多標頭檔案如fstream.h 和iostream.h在vc8中已經不存在了.
#include
<
fstream.h
>
//error in vc8
解決方法:
使用stl.
#include
<
fstream
>
10. iterator
一些 stl 類, iterators 不再用指標實現
std::vector
<
dmdetailrow
>
m_data;
std::vector
<
dmdetailrow
>
::iterator iter =&
m_data[rownum];
解決方法:
std::vector
<
dmdetailrow
>
::iterator iter
=m_data.begin()
+rownum;
11. enum
使用乙個enum的成員時,不要使用enum的名字
enum
e ;
e e1
=e::a;
//warning in vc8
解決方法:
去掉enum 的名字.
e e1 =a;
VC7到VC6工程的轉換工具
先來個圖 一 簡介 這個工具自動將vc7工程轉換回vc6工程,換言之,就是將.sln vcproj這兩個檔案轉換到.dsw dsp檔案。注意 本轉換器在轉換期間只是建立 或覆蓋 dsw dsp檔案,並沒有改變任何源 二 由來 首先當然是ms並沒有提供這樣的工具,ms在 開發工具時並沒有提供工程回退的...
VC小專案 13 0專案導引(1)
程式閱讀 多型性與抽象類 1 虛函式 includeusing namespace std class a virtual void print const 輸出 5ee5e 除錯過程 除錯心得 通過虛函式,可以通過基類的指標達到訪問子類成員函式的目的,去掉virtual關鍵字將得到不一樣的結果 參...
VC小專案 13 0專案導引(2)
1 請寫出程式的執行結果,並在除錯時對照理解 include using namespace std class vehicle 交通工具 請回答 當基類的指標指向派生類時,用指標呼叫同名成員函式,執行的是基類的成員函式,還是派生類的成員函式?為什麼會這樣?答 輸出結果 說明執行的是基類的成員函式。...