Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6678365
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:14:02+00:00 2026-05-26T04:14:02+00:00

I get these errors when I compile this. I can not find out the

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T04:14:02+00:00Added an answer on May 26, 2026 at 4:14 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm getting two compile errors: /home/sater/projects/MotionManager/videoProcess.cpp:11:1: error: a function-definition is not allowed here before
I get errors when viewing forms and there are not any. I can close
I get this error: Can't locate Foo.pm in @INC Is there an easier way
I'd like to do this: System.out.println(안녕하세요!); But I get a Some characters could not
Can anyone help me with this? I have not been able to find anything
I continually get these errors when I try to update tables based on another
I am having trouble validating my xml schema. I get these errors on the
I'm surprised that I could not find any info on this. I must be
When we try to create a view within a funcion we get ERROR: there
When debugging in Visual Studio 2008 I get the error There is no source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.