I get these errors when I compile this. I can not find out the error. Specially why ; is required and why function templates are already defined. I am using Visual Studio 2010. This works with Turbo C++ but I want to know why these errors comes with 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);
}
Say
std::fstream file;instead offstream file;, becausefstreamis declared in the Standard namespacestd, not in the global namespace.In the
.cppfile corresponding toFileOp.hyou may choose to sayusing namespace std;in that.cppfile if you don’t want to typestd::everywhere in that file (it’s basically a question of taste and how likely you want to be to get name conflicts. Making all names ofstd::unqualified visible increases the likelihood of name conflicts with global names). But don’t put such a line into a header.Since your code appears to be entirely into one
.hfile you won’t have such an option then. But you can locally in the member functions sayusing namespace std;or declare individual names as aliases in local scope of member functions, likeusing std::fstream;, if you want.Also, you need to put the definition of those member functions (of the class template) within the header guard, if not directly within the class body if you like. Your other error messages appear because you include the header multiple times, but the member function definitions, appearing outside the header guard, wrongly will be emitted into the translation unit multiple times, raising multiple definition errors at compile time.