當我編譯這個時,我得到這些錯誤。我找不到這個錯誤。特別是爲什麼需要;
以及爲什麼功能模板已經定義。我使用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);
}
我得到了第一個答案。這是因爲我沒有使用名稱空間。現在我想知道爲什麼說功能模板已經定義好了。我該如何解決這個問題 – Gihan
這裏不需要';'。 'FileOperations(){};'。你在很多地方都這樣做。 – Mahesh