Archived

This topic is now archived and is closed to further replies.

Utwo

Grrr...Under what circumstances would MinGW not recognize cin, cout, fstream, etc...

Recommended Posts

Here is my code:
#include <fstream>
#include <string>
#include <stdlib.h>


class characters
{

 public:

 int ATT;
 int AC;
 int AGI;
 int DEX;
 int INT;

};

int main()
{
 
 fstream file("test_file.dat", ios::out | ios::in | ios::binary);
 
 characters temp;
 characters loaded;
 
 cout << "ATT: ";
 cin >> temp.ATT;
 cout << "\nAC: ";
 cin >> temp.AC;
 cout << "\nAGI: ";
 cin >> temp.AGI;
 cout << "\nDEX: ";
 cin >> temp.DEX;
 cout << "\nINT: ";
 cin >> temp.INT;

 file.write(temp, sizeof(temp));
 
 file.seekg(ios::beg);
 
 file.read(loaded, sizeof(temp));
 
 cout << "\n\nATT: " << loaded.ATT << "\n";
 cout << "AC: " << loaded.AC << "\n";
 cout << "AGI: " << loaded.AGI << "\n";
 cout << "DEX: " << loaded.DEX << "\n";
 cout << "INT: " << loaded.INT << "\n";
 
 file.close();
 
 system("PAUSE");

 return 0;
 
}
And here are the errors I''m getting: 22 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp `fstream'' undeclared (first use this function) 22 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp parse error before `('' token 27 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp `cout'' undeclared (first use this function) 28 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp `cin'' undeclared (first use this function) 38 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp `file'' undeclared (first use this function) 40 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp `ios'' undeclared (first use this function) 40 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp parse error before `::'' token

Share this post


Link to post
Share on other sites
put

using namespace std;

after the #include lines or prefix cin, cout etc. with std::, ex:

std::cout << "hi" << std::endl;

BTW, the C++ way of including the standard C headers is:
#include <cstdlib>
etc. instead of
#include <stdlib.h>


[edited by - nonpop on March 18, 2004 4:56:58 PM]

Share this post


Link to post
Share on other sites
I thought that included but I guess not, because still had to include both in order for it to recognize cin/cout. I''ve included both, and now I receive this message:

39 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp
no matching function for call to `std::basic_fstream43 C:\Dev-Cpp\PROJECTS\readwrite\main.cpp
no matching function for call to `std::basic_fstream



Lines 39 and 43 are referencing my "file.read" and "file.write" lines, btw. I suck at this.

Share this post


Link to post
Share on other sites
Actually, if you use the standard namespace you can omit lines like std::cout or whatever...



#include <iostream>
using namespace std; //includes the entire namespace

using std::cout; //allows you to use cout

using std::cin; // '' '' cin

using std::ostream; // '' '' ostream

using std::istream; // '' '' istream


//Edit #1: You can also use this for the derived fstream class

using std::fstream;

//There are many others; just type using std:: and see what

//pops up




With the line (using namespace std) you get to use all of the objects that come with the iostream header file and you don't have to put the std:: qualifier before you use the object and the same goes with the others listed as well. However, if you want to be more efficient, just use the others listed above for the objects of iostream that you do use.

Dan...

[edited by - xdanillumx on March 18, 2004 5:08:06 PM]

Share this post


Link to post
Share on other sites
I always forget you can use using std::

However, for your new problem...
I guess it''s because fstream::read() doesn''t know how to handle your characters class. You should load the member variables one by one. Adding load_from_file() and save_to_file() style functions to the class will make things easier.

Oh, it''s getting too late now. I hope someone else will explain things in more detail if needed.

Share this post


Link to post
Share on other sites