2010-11-11 16 views
1

我的頭文件如下:字符串在C頭文件

#include <iostream> 
#include <string> 
#include <windows.h> 
#include <math.h> 

//using namespace std; 

std::string StringMultiply(string Str, int Mult) 
{ 
    std::string Return; 

    for (int Index = 0; Index <= Mult; Index++) 
    { 
     Return += Str; 
    } 

    return Return; 
} 

編譯它產生錯誤的轉換,其中大多數是關於不存在string數據類型的。取消註釋using namespace std;一行修復它,但我已經被告知這是在頭文件中的不好的做法。

回答

8

變化

std::string StringMultiply(string Str, int Mult) 

std::string StringMultiply(std::string Str, int Mult) 
+0

謝謝,修復它。 – Maxpm 2010-11-11 21:26:22

2

你需要每次使用它的時間來限定stringstd::string如果您註釋掉using線。 StringMultiply的返回值正確,但參數不正確。

就我個人而言,我不明白建議與using namespace std; - 我不喜歡打字比我更多。

+0

通常的建議是「不要在名稱文件的全局範圍內使用namespace std;'說某人忽略了這個建議並完成了它,你正在使用某個人的頭文件,並且你有一些名爲' count'。呃哦,還有一個'std :: count'(IIRC)。它可以被解析,這就是命名空間的作用,但是頭文件的許多用戶並沒有做額外的工作,而是讓頭文件的作者做了一些額外的工作。乾杯, – 2010-11-11 21:45:46

2

如果在參數列表中將string Str替換爲std::string Str,則一切都會正常編譯。究竟是什麼問題?