2011-10-15 47 views
-2

當我編譯這個時,我得到這些錯誤。我找不到這個錯誤。特別是爲什麼需要;以及爲什麼功能模板已經定義。我使用Visual Studio 2010中這適用於的Turbo C++,但我想知道爲什麼這些錯誤帶有的Visual C++失蹤';'之前標識符

Error 1 error C2146: syntax error : missing ';' before identifier 'file'  
Error 4 error C2146: syntax error : missing ';' before identifier 'file'  
Error 10 error C2995: 'int FileOperations<T>::getNoOfElements(void)' : function template has already been defined  
Error 9 error C2995: 'T FileOperations<T>::readFromFile(int)' : function template has already been defined 
Error 8 error C2995: 'void FileOperations<T>::swriteToFile(T,int)' : function template has already been defined 
Error 7 error C2995: 'void FileOperations<T>::writeToFile(T)' : function template has already been defined 
Error 2 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Error 3 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Error 5 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 
Error 6 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int 

#ifndef FileOp_h 
#define FileOp_h 
#include <iostream> 
#include <cstdlib> 
#include <fstream> 

template <class T> 
class FileOperations 
{ 
    private: 
     fstream file; 


    public: 
     FileOperations(){}; 

     FileOperations(const char* fileName){fileOpen(fileName);}; 

     void fileOpen(const char* fileName){file.open(fileName,ios::in|ios::out|ios::ate|ios::binary);}; 

     void writeToFile(T); 
     void swriteToFile(T,int); 
     T readFromFile(int); 
     int getNoOfElements(); 

     ~FileOperations(){file.close();}; 

}; 

#endif 

template <class T> 
void FileOperations<T>::writeToFile(T fileOb) 
{ 
    file.clear(); 
    file.write((char*)&fileOb, sizeof(fileOb))<<flush; 
} 

template <class T> 
void FileOperations<T>::swriteToFile(T fileOb,int seekTo) 
{ 
    file.clear(); 
    file.seekp(seekTo,ios::beg); 
    file.write((char*)&fileOb, sizeof(fileOb))<<flush; 
} 

template <class T> 
T FileOperations<T>::readFromFile(int seekTo) 
{ 
    T object; 
    file.seekg(seekTo,ios::beg); 
    file.read((char*)&object,sizeof(object)); 
    file.clear(); 
    return object; 
} 

template <class T> 
int FileOperations<T>::getNoOfElements() 
{ 
    file.seekg(0,ios::end); 
    int size=file.tellg(); 
    return size/sizeof(T); 
} 
+0

我得到了第一個答案。這是因爲我沒有使用名稱空間。現在我想知道爲什麼說功能模板已經定義好了。我該如何解決這個問題 – Gihan

+0

這裏不需要';'。 'FileOperations(){};'。你在很多地方都這樣做。 – Mahesh

回答

3

std::fstream file;代替fstream file;,因爲fstream在標準的命名空間std宣佈,而不是在全局命名空間。

,在對應於FileOp.h你可以選擇說using namespace std;.cpp文件,如果你不想在該文件中無處不在鍵入std::(它基本上是一個品味的問題,你希望得到怎樣有可能的.cpp文件名稱衝突使std::的所有名稱不可見可見增加名稱與全局名稱衝突的可能性)。但是不要把這樣一行放到標題中。

因爲你的代碼似乎完全變成一個.h文件,你不會有這樣一個選項,然後。但你可以在本地的成員函數說using namespace std;或者個人的姓名,聲明中的成員函數的本地別名,像using std::fstream;如果你想要的。

另外,如果你喜歡,你還需要把頭文件中的那些成員函數(類模板的)定義在之內,如果不是直接在類體中的話。您的其他錯誤消息的出現是因爲您多次包含標題,但出現在標題守護程序之外的成員函數定義會錯誤地發送到翻譯單元多次,從而在編譯時引發多個定義錯誤。

+0

感謝您的快速回復。我想到了。我現在使用名稱空間它不會給出該錯誤,但它仍然給出錯誤10錯誤C2995:'int FileOperations :: getNoOfElements(void)':功能模板已被定義 錯誤9錯誤C2995:'T FileOperations : :readFromFile(int)':函數模板已定義 錯誤8錯誤C2995:'void FileOperations :: swriteToFile(T,int)':函數模板已定義 錯誤。你能告訴我這是什麼原因...? – Gihan

+0

你能告訴我最後一個問題的一塊繩子嗎?如何將這些定義放入頭衛隊。請致電 – Gihan

+0

@Gihan:我們不會發送-u-the-codez。 – Puppy