失踪了';在标识符之前
当我编译这个时,我收到这些错误。我找不到错误。特别是为什么需要 ;
以及为什么函数模板已经定义。我正在使用 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);
}
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);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用
std::fstream file;
而不是fstream file;
,因为fstream
是在标准命名空间std
中声明的,不在全局命名空间中。在与
FileOp.h
对应的.cpp
文件中,您可以选择在.cpp
中使用using namespace std;
> 文件,如果您不想在该文件中的任何地方键入std::
(这基本上是一个品味问题以及您希望发生名称冲突的可能性有多大。将所有名称设为std::
不合格的可见增加了名称的可能性与全局名称冲突)。但不要将这样的行放入标题中。由于您的代码似乎完全放入一个
.h
文件中,因此您将没有这样的选项。但是,您可以在成员函数中本地使用using namespace std;
或在成员函数的本地范围内将各个名称声明为别名,例如using std::fstream;
,如果您想。另外,您需要将这些成员函数(类模板的)的定义放在标头保护内,如果您愿意的话,如果不直接放在类主体内的话。出现其他错误消息是因为多次包含标头,但出现在标头防护之外的成员函数定义会多次错误地发送到翻译单元,从而在编译时引发多个定义错误。
Say
std::fstream file;
instead offstream file;
, becausefstream
is declared in the Standard namespacestd
, not in the global namespace.In the
.cpp
file corresponding toFileOp.h
you may choose to sayusing namespace std;
in that.cpp
file 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
.h
file 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.