error C2039: 'Open' : is not a member of 'std::basic_fstream - c++

When i call
void fileOpen(const char*
fname_){file_.Open(fname_,ios::in|ios::out|ios::ate|ios::binary);};
Function like
tempobj->fileOpen("LastID.dat");
It gives me the error
Error 23 error C2039: 'Open' : is not a member of 'std::basic_fstream<_Elem,_Traits>'
How do i resolve this. This is the class I have this function. It is template class
#ifndef FileHandlerh_h
#define FileHandlerh_h
#include <iostream>
#include <cstdlib>
#include <fstream>
using namespace std;
template <class T>
class FileHandler
{
private:
fstream file_;
public:
FileHandler(){};
FileHandler(const char* fname_){fileOpen(fname_);};
void fileOpen(const char* fname_){file_.Open(fname_,ios::in|ios::out|ios::ate|ios::binary);};
void fileWrite(T);
void fileSeekWrite(T,int);
T fileRead(int);
int getNoOfRecords();
~FileHandler(){file_.close();};
};
Help me with this...!!

C++ is case sensitive. You need to use open() instead of Open().

Use a lowercase O, perhaps? It's quite uncommon to see capitals in function names in the standard library.

Related

C++ - error: class has not been declared/out of scope

So I have two classes - Dvd and DvdGroup. DvdGroup basically manages an array of dvds and provide manipulative member functions for that class. The problem is whenever I try to compile DvdGroup.cc using the command 'g++ -c Dvd.Group.cc', I get a bunch of errors all related to not having 'Dvd' declared and I'm not sure why.
Here are some errors below:
DvdGroup.h:14:12: error: ‘Dvd’ has not been declared void add(Dvd*);
DvdGroup.h:18:3: error: ‘Dvd’ does not name a type Dvd* dvdCollection[MAX_DVDS];
DvdGroup.cc: In copy constructor ‘DvdGroup::DvdGroup(DvdGroup&)’:
DvdGroup.cc:15:6: error: ‘Dvd’ was not declared in this scope for(Dvd d: dvds){
I feel like I'm missing something and they could all be fixed by one solution because they all involve having the Dvd class undeclared but I can't seem to figure out what. I was wondering if anyone could tell me what I'm doing wrong? I would really appreciate any help with fixing this.
DvdGroup.cc:
#include <iostream>
using namespace std;
#include "DvdGroup.h"
DvdGroup::DvdGroup(int n){
numDvds = n;
}
DvdGroup::DvdGroup(DvdGroup& dvds){
numDvds = dvds.numDvds;
for(Dvd d: dvds){
Dvd newDvd = Dvd;
}
}
DvdGroup::~DvdGroup(){
//code
}
void DvdGroup::add(Dvd* d){
//code
}
DvdGroup.h:
#ifndef DVDGROUP_H
#define DVDGROUP_H
#define MAX_DVDS 15
#include <string>
using namespace std;
class DvdGroup
{
public:
DvdGroup(int);
DvdGroup(DvdGroup&);
~DvdGroup();
void add(Dvd*);
private:
Dvd* dvdCollection[MAX_DVDS];
int numDvds;
};
#endif
Don't know if the Dvd header file is needed, but here:
Dvd.h:
#ifndef DVD_H
#define DVD_H
#define MAX_DVDS 15
#include <string>
class Dvd{
public:
Dvd(string, int);
void set(string, int);
Dvd(Dvd&);
int getYear();
~Dvd();
void print();
private:
string title;
int year;
};
#endif
What you need to do is to provide Dvd class definition for DvdGroup class. It is needed to know what type of symbol is this. Solution for your problem should be addition of:
#include "Dvd.h"
line to DvdGroup.h file.

Undeclared identifier and type 'int' unexpected error when calling a class from main

Hey guys so I am working on a Hash program in C++ for my class. I am using a template T class for my header file and when I try to call the class constructor from main it gives me a undeclared identifier and type 'int' unexpected error. Here is my HashTable.h file:
#pragma once
#include "Record.h"
#define MAXHASH 1000
#ifndef HASHTABLE_H
#define HASHTABLE_H
using namespace std;
template <class T> class HashTable
{
public:
HashTable();
~HashTable();
bool insert(int, T, int&);
bool remove(int);
bool find(int, T&);
float alpha();
private:
int key;
T value;
};
#endif
and here is my main:
#include "HashTable.h"
#include "stdafx.h"
#include <string>
#include <iostream>
using namespace std;
int main()
{
HashTable<int> *test = new HashTable<int>();
return 0;
}
Here's the constructor in the .cpp file as well:
#pragma once
#include "stdafx.h"
#include "HashTable.h"
#include "Record.h"
#define HASHTABLE_CPP
#ifndef HASTABLE_CPP
template <class T>
HashTable<T>::HashTable()
{
Record hashArray = new Record[MAXHASH];
for (int i = 0; i < MAXHASH; i++)
{
hashArray[i]->key = 0;
hashArray[i]->value = NULL;
}
}
The specific errors I am getting are:
Error C2062 type 'int' unexpected identifier
Error C2065 'HashTable': undeclared identifier
Both the errors point to the call line in main.
It is difficult because I can't test my program until I can get this test hash to work. Any input on how to fix this issue would be awesome!
the old Microsoft application file extensions interface "stdafx.h" has to be the first directive listed if the pre-compiled header is used. It actually works best because VS expects it...I always use it.

Member variable in scope of one member function but not the other

I'm seriously confused why this is happening. I get an error 'enzyme_acronyms_ was not declared in this scope'. It points to my writeAcronym function but not getAcronym, and both use enzyme_acronyms_. What can possibly cause this?
SequenceMap.h
#ifndef SequenceMap_h
#define SequenceMap_h
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class SequenceMap
{
private:
string recognition_sequence_;
vector<string> enzyme_acronyms_;
public:
string getAcronym();
void writeAcronym(string an_enz_acro);
}
SequenceMap.cpp
#include "SequenceMap.h"
string SequenceMap::getAcronym()
{
return enzyme_acronyms_[0]; //works fine
}
void writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro); //enzyme_acronyms_ not declared in this scope
}
You've missed the SequenceMap:: qualification on the second function definition:
void SequenceMap::writeAcronym(string an_enz_acro)
It must be declared like this:
void SequenceMap::writeAcronym(string an_enz_acro)
{
enzyme_acronyms_.push_back(an_enz_acro);
}
You forgot the class scope SequenceMap::.

String could not be resolved c++

I am wondering why I am getting error "string could not be resolved to type" when I have the proper inclusions?
#ifndef EVENTFILEREADER_H_
#define EVENTFILEREADER_H_
#include <string>
#include <stdlib.h>
#include <iostream>
class EventFileReader {
public:
EventFileReader(string fileName);
virtual ~EventFileReader();
};
#endif /* EVENTFILEREADER_H_ */
Your compiler is complaining about not being able to find string as a defined type.
You should add its namespace std:
EventFileReader(std::string fileName);
^^^^^
You need to specify namespace, e.g.
std::string
or put the using declaration after includes:
using std::string;

Global function header and implementation

how can I divide the header and implementation of a global function?
My way is:
split.h
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const string s, const string c);
split.cpp
#include "split.h"
void split(const string& s, const string& c){
...
}
main.cpp
// main.cpp : Defines the entry point for the console application.
//
#include <string>
#include <vector>
#include <functional>
#include <iostream>
#include "stdafx.h"
#include "split.h"
using namespace std;
int _tmain(int argc, _TCHAR* argv[])
{
vector<string> v;
string s = "The;;woraaald;;is;;not;;enoaaaugh";
string c = " aaa ;; ccc";
split(s,c);
return 0;
}
And errors are:
Error 1 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int ...\split.h 8
Error 2 error C2146: syntax error : missing ',' before identifier 's' ...\split.h 8
How can I solve this problem? thx
In header file use std:: namespace qualifier - std::string
In the header file, you have to give the fully qualified name std::string. In the source files, you can add using namespace std; or using std::string; and then just spell it string.
Also, you've declared the function taking arguments by value, but defined it taking arguments by reference.
At least one problem is, you are missing the 'std::' namespace qualifier in split.h:
#pragma once
#include <string>
#include <vector>
#include <functional>
#include <iostream>
void split(const std::string s, const std::string c);
I think you either forgot to put using std::string; before split declaration or use std::string const& as split parameter declarations.
Also split declaration mismatch from split definition string const vs string const&