0

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
Biffen
  • 6,249
  • 6
  • 28
  • 36
Ahmed Nabil
  • 41
  • 1
  • 2
  • 8

1 Answers1

0

The problem: inside your MyClass.h, you use classes you don't include. Include them inside MyClass.h

MyClass.h:

#include<iostream>
#include<fstream>
#include<string>
using namespace std;

class MyClass
{
public:
    void setData(string);
    void write(fstream&);

private:
    string data;
};

Inside MyClass.cpp, you don't need to include them, only MyClass.h.

MyClass.cpp:

#include "MyClass.h"

void MyClass::setData(string input)
{
    data = input;
}

void MyClass::write(fstream &outfile)
{
    outfile << data << endl;
}

I'm not sure why you're returning void, but that can be a source of problem depending on your compiler. Here, in my computer, I used g++ and that was a problem. I had to change it to return int and added return 0; in the end. Maybe you could that too.

Main.cpp

#include"MyClass.h"
#include<iostream>
#include<fstream>
#include<string>
using namespace std;

int 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);
    return 0;
}

Now, it will work since it worked in my computer. :)

Paulo
  • 1,458
  • 2
  • 12
  • 26
  • Someone downvoted this answer but I don't really get the downvote. It really worked with me after the changes. And before I was getting compile errors. Can someone point out why the downvote? – Paulo Feb 02 '16 at 08:41
  • 2
    I don't get the downvote either. The only "problem" is `using namespace std;` in a header, which has a tendency to cause problems far away, but that doesn't warrant a downvote. – molbdnilo Feb 02 '16 at 08:44
  • @Paulo .... thanks i did included all of them into the header file the included header file into cpp files but it didn't work till i add (using namespace std;) in the heade file – Ahmed Nabil Feb 02 '16 at 08:51
  • After the changes, what error are you getting? Did you change your main to return `int`? One more question: which compiler are you using? – Paulo Feb 02 '16 at 08:56
  • Only one more advice: after including them in the header file, remove them in MyClass.cpp. Did you do that? – Paulo Feb 02 '16 at 08:57
  • @AhmedNabil No need to remove the includes from the `.cpp` file. You should, however, use the `std::` prefix everywhere, especially in the header file. – Biffen Feb 02 '16 at 11:47
  • It's strange if it's not working. I've just compiled simply with `g++ main.cpp MyClass.cpp` and it worked perfectly. Copy my code (of three files) and paste them in a separated folder just to be sure it's really not working (even with my code). – Paulo Feb 02 '16 at 20:25
  • Remove the include files that are the ones that are standard i.e. enclosed in <>` from the header and only have then in the .cpp file. This makes compiling quick as these declarations are likely to be used in multiple header files in the project. In addition use the signature `setData(const std::string&)` – Ed Heal Feb 03 '16 at 18:39