G++ - why am i getting errors compiling this simple code
can someone tell me why i'm getting errors when compiling this code using g++. it's almost like their may be a missing library needed in order to pass variable of type fstream to functions as formal parameters. i'm recieving errors such as: ifstream was not declared in this scope. let me know what you think.
#include <iostream> #include <fstream> #include <cstdlib> void greater(ifstream& fin_par); int main() { using namespace std; ifstream fin; fin.open("numfile.dat"); if (fin.fail()) { cout <<"Input file opening failed.\n"; exit (1); } greater(fin); fin.close(); return 0; } void greater(ifstream& fin_par) { using namespace std; int count, number; fin_par >> number; count = 0; do { if (number > count) { count = number; fin_par >> number; } else { fin_par >> number; } } while (! fin_par.eof()); cout << count; } |
You are using the name greater which is already declared
Also I changed the "uses std..." to only used once at the top Code:
#include <iostream> As a side note please use the code tags like this execept without the spaces [ code ] /* code goes in here */ [ /code ] |
thanks for your help....but what library is greater already declared in though.....and what does the function for greater do.......
|
Can't really help you there, all I know is what the compiler spat out.
I don't actually know how to code in C++ I only know enough to fake it. /usr/lib/gcc-lib/i686-pc-linux-gnu/3.3.2/include/g++-v3/bits/stl_function.h:188: error: also declared as `template<class _Tp> struct std::greater' here If you really care to find out check out the include file your copy of g++ spits out. look for the greater funtion |
What I might suggest, is not to use the line: "using namespace std" since that will bring all thing defined in that namespace into your local scope..
Instead pre-qualify std stuff with the std namespace qualifier. Code:
#include <iostream> This way you are fine to call your function 'greater' if you like... Its all about personal choice.. I see that namespace's most usefull purpose is to avoid name pollution, and 'using namespace ????' clauses do the opposite (ie, it pollutes the namespace).. Cheers.. |
All times are GMT -5. The time now is 09:06 AM. |