失踪了';在标识符之前

发布于 2024-12-09 18:23:41 字数 2603 浏览 0 评论 0原文

当我编译这个时,我收到这些错误。我找不到错误。特别是为什么需要 ; 以及为什么函数模板已经定义。我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

爱,才寂寞 2024-12-16 18:23:41

使用 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 of fstream file;, because fstream is declared in the Standard namespace std, not in the global namespace.

In the .cpp file corresponding to FileOp.h you may choose to say using namespace std; in that .cpp file if you don't want to type std:: 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 of std:: 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 say using namespace std; or declare individual names as aliases in local scope of member functions, like using 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文