LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   G++ - why am i getting errors compiling this simple code (https://www.linuxquestions.org/questions/programming-9/g-why-am-i-getting-errors-compiling-this-simple-code-117097/)

leroy27336 11-17-2003 12:42 PM

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;
}

LogicG8 11-17-2003 12:54 PM

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>
#include <fstream>
#include <cstdlib>

using namespace std;

void mygreater(ifstream& fin_par);

int main()
{
        ifstream fin;

        fin.open("numfile.dat");
        if (fin.fail())
        {
                cout <<"Input file opening failed.\n";
                exit (1);
        }

        mygreater(fin);

        fin.close();

        return 0;
}

void mygreater(ifstream& fin_par)
{
        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;
}

Edit:
As a side note please use the code tags
like this execept without the spaces
[ code ] /* code goes in here */ [ /code ]

leroy27336 11-17-2003 12:57 PM

thanks for your help....but what library is greater already declared in though.....and what does the function for greater do.......

LogicG8 11-17-2003 01:02 PM

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

mr_segfault 11-18-2003 08:32 PM

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>
#include <fstream>
#include <cstdlib>

// using namespace std; // leave this commented out

void mygreater(std::ifstream& fin_par);

int main()
{
        std::ifstream fin;

        fin.open("numfile.dat");
        if (fin.fail())
        {
                std::cout <<"Input file opening failed.\n";
                exit (1);
        }

        mygreater(fin);

        fin.close();

        return 0;
}

void mygreater(std::ifstream& fin_par)
{
        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;
}

The reason for this is that the object 'greater''s name is really std::greater, but when you use the 'using namespace std' clause, you cause 'greater' to resolve to 'std::greater'..

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.