LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 11-17-2003, 12:42 PM   #1
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Rep: Reputation: 15
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;
}
 
Old 11-17-2003, 12:54 PM   #2
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
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 ]

Last edited by LogicG8; 11-17-2003 at 12:58 PM.
 
Old 11-17-2003, 12:57 PM   #3
leroy27336
Member
 
Registered: Nov 2003
Distribution: LTS Ubuntu 6.06
Posts: 127

Original Poster
Rep: Reputation: 15
thanks for your help....but what library is greater already declared in though.....and what does the function for greater do.......
 
Old 11-17-2003, 01:02 PM   #4
LogicG8
Member
 
Registered: Jun 2003
Location: Long Island, NY
Distribution: Gentoo Unstable (what a misnomer)
Posts: 380

Rep: Reputation: 30
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
 
Old 11-18-2003, 08:32 PM   #5
mr_segfault
Member
 
Registered: Oct 2003
Location: Australia
Distribution: Redhat 9
Posts: 95

Rep: Reputation: 15
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..
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
errors with simple C++ Code... openGL problem? poeta_boy Programming 4 11-08-2004 12:34 PM
error in compiling simple code Tinku Programming 6 09-17-2004 01:38 AM
Major compilation errors in emacs while compiling c code dualcyclone Linux - Software 0 03-26-2004 01:31 PM
Simple Tutorial for compiling source code? SoliTear Linux - Newbie 17 03-15-2004 08:39 AM
Getting Incompatibility Errors While Compiling The Following Code. pritesh Programming 4 10-23-2003 04:18 PM


All times are GMT -5. The time now is 02:10 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration