1. 程式人生 > >strcpy沒有宣告 c++標頭檔案詳解

strcpy沒有宣告 c++標頭檔案詳解

協議分發程式程式在公司機器上編譯執行良好,而我在我的虛擬機器上編譯,卻出下如下錯誤:

msg.cpp:3101: error: ‘strcpy’ was not declared in this scope

檢視原始碼已經包含了相關標頭檔案及名稱空間,如下:

而且程式中使用的map, queue,list等都能正確使用,唯獨string不能使用。

而且通過#find ./ -name string –print 也能以現在/usr/include/c++/4.4.0目錄下存在string檔案。

如何解決?

分析:

#include <string.h> c++中,是指標準化以前的標準

c庫中的字char*字串處理函式

#include <string>中在1988年標準化以後,c++中含的STLstring容器

#include <cstring> 是在1988年標準化以後,<string.h>的演變。

總之你記住一句話:<string><string.h>是完全不同的兩個東西。

========================================================================

用歷史來讓你更理解吧!!!

C語言是1972年由美國貝爾實驗室的D.M.Ritchie

研製成功。C語言的標頭檔案包含格式如下:

#include <stdio.h>

#include <string.h>

#include <math.h>

20世紀80年代初,Bjarne Stroustrup博士及其同事在C語言的基礎上成功研發出C++語言。C++是由C發展來的,與C相容,是對C的擴充套件,或者說是C的超集(當時的名稱也不叫c++, 而叫C with class)。因為最初的C++的標頭檔案包含格式自然而然與C保持了一致:

// (標準化以前c)

#include <stdio.h>

#include <string.h>

#include <math.h>

// 標準化以前的標準c++

#include <iostream.h>

1988年,對C++進行了標準化。在這次行動中,把標準C++庫的元件放在一個名位stdnamespace裡面,同時為了區別c庫,也產生了如下名稱:

// 標準化以後的標準C

#include <cstdio>

#include <cstring>

#include <cmath>

// 標準化以後的標準c++

#include <iostream>

1995-2000年,出現並開始大量使用標準模板庫STLBoost

#include <map>

#include <string>

#include <vector>