I have class that contains 2 functions; one to set data from the user and the other to write this data to file.
Here is the header file:
class MyClass
{
public:
void setData(string);
void write(fstream&);
private:
string data;
};
MyClass.cpp
:
#include "MyClass.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void MyClass::setData(string input)
{
data = input;
}
void MyClass::write(fstream &outfile)
{
outfile << data << endl;
}
And here is the main function:
#include"MyClass.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
void main()
{
MyClass ob;
string input;
fstream ofile("D:/Blank.txt", ios::out);
cout << "Enter the data .... " << endl;
getline(cin, input);
ob.setData(input);
ob.write(ofile);
}
It always gives me syntax errors and some other errors and I can't figure out where is the error of this code, known that I tried this code without using class and it worked well.
Error messages:
Error 2 error C2061: syntax error : identifier 'fstream' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 6 1 Files
Error 8 error C2061: syntax error : identifier 'fstream' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 6 1 Files
Error 1 error C2061: syntax error : identifier 'string' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 5 1 Files
Error 7 error C2061: syntax error : identifier 'string' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 5 1 Files
Error 12 error C2065: 'data' : undeclared identifier c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.cpp 9 1 Files
Error 14 error C2065: 'data' : undeclared identifier c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.cpp 14 1 Files
Error 3 error C2146: syntax error : missing ';' before identifier 'data' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 9 1 Files
Error 9 error C2146: syntax error : missing ';' before identifier 'data' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 9 1 Files
Error 11 error C2511: 'void MyClass::setData(std::string)' : overloaded member function not found in 'MyClass' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.cpp 8 1 Files
Error 13 error C2511: 'void MyClass::write(std::fstream &)' : overloaded member function not found in 'MyClass' c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.cpp 13 1 Files
Error 5 error C2660: 'MyClass::setData' : function does not take 1 arguments c:\users\ahmed\documents\visual studio 2013\projects\files\files\main.cpp 14 1 Files
Error 6 error C2660: 'MyClass::write' : function does not take 1 arguments c:\users\ahmed\documents\visual studio 2013\projects\files\files\main.cpp 16 1 Files
Error 4 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 9 1 Files
Error 10 error C4430: missing type specifier - int assumed. Note: C++ does not support default-int c:\users\ahmed\documents\visual studio 2013\projects\files\files\myclass.h 9 1 Files