Welcome! Log In Create A New Profile

Advanced

HELP!!!!! I/O Streams Error Compiling

Posted by Anonymous User 
Announcements Last Post
Announcement SoC Curricula 09/30/2017 01:08PM
Announcement Demarcation or scoping of examinations and assessment 02/13/2017 07:59AM
Announcement School of Computing Short Learning Programmes 11/24/2014 08:37AM
Announcement Unisa contact information 07/28/2011 01:28PM
Anonymous User
HELP!!!!! I/O Streams Error Compiling
May 02, 2008 07:26AM
Chapter 6: I/O Streams as an introduction to Objects and Classes.. On page 327 of Savitch. Display 6.6: Formatting output.I typed the program as it appears in the book.

On compiling, I get the error;
[Warning] In function “int main().�
Statement cannot resolve address of overloaded function
Statement cannot resolve address of overloaded function

Please help, I am getting the same error for all my I/O Stream programs.. Thanks

Francis
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 02, 2008 10:43AM
Hi

Can you please post your code. I just typed in the example as per book and don't see a problem.

Also try first deleting ALL COMMENTS. This example is a bit cluttered and could be the cause.
Also try cut 'n pasting my code and see if you get the same error:


#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>

using namespace std;

void make_neat (ifstream& messy_file, ostream& neat_file, int number_after_decimalpoint, int field_width);


int main ()
{
        ifstream fin;
        ofstream fout;

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

        fout.open("neat.dat"winking smiley;
        if (fout.fail())
                {
                        cout << "Output file openeing failed.\n";
                        exit(1);
                }


        make_neat(fin, fout, 5, 12);

        fin.close();
        fout.close();

        cout << "End of program.\n";
        return 0;
}

void make_neat (ifstream& messy_file, ostream& neat_file, int number_after_decimalpoint, int field_width)
{
        neat_file.setf(ios::fixed);
        neat_file.setf(ios::showpoint);
        neat_file.setf(ios::showpos);
        neat_file.precision(number_after_decimalpoint);

        cout.setf(ios::fixed);
        cout.setf(ios::showpoint);
        cout.setf(ios::showpos);
        cout.precision(number_after_decimalpoint);

        double next;
        while (messy_file >> next)
                {
                        cout << setw(field_width) << next << endl;
                        neat_file << setw(field_width) << next << endl;
                }
}



// [eof]

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 02, 2008 11:06AM
I dont have my code here with me..but it looks exactly identical to yours. I have pasted yours into a txt file.. Will try compiling it when I get home. If all fails, I am gonna re-install my compiler and see.. Thanks mate.
Ohh, i have a screen shot of the error, u wanna see it?
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 02, 2008 11:29AM
Ok post the screen shot or you can mail it to me. Just click on my name for my email.

BTW what OS your running XP or VISTA?

Im running UBUNTU and all our code for this module works perfectly. (I also checked this in XP and it was also fine).

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 05, 2008 03:08PM
HERE IS THE CODE: DO U SEE ANYTHING WRONG WITH IT?

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
using namespace std;

void make_neat(ifstream& messy_file, ofstream& neat_file, int num_after, int field_width);
int main()
{ ifstream fin;
ofstream fout;
fin.open("rawdata.dat"winking smiley;
if (fin.fail())
{ cout<<"Input file opening failed..";
exit(1);
}
fout.open("neatdata.dat"winking smiley;
if (fout.fail())
{ cout<<"Output file opening failed..";
exit(1);
}

make_neat(fin, fout, 5, 12);

fin.close;
fout.close;
return 0;
}
void make_neat(ifstream& messy_file, ofstream& neat_file, int num_after, int field_width)
{ neat_file.setf(ios::fixed);
neat_file.setf(ios::showpoint);
neat_file.setf(ios::showpos);
neat_file.precision(num_after);
cout.setf(ios::fixed);
cout.setf(ios::showpoint);
cout.setf(ios::showpos);
cout.precision(num_after);

double next;
while (messy_file >> next)
{ cout<<setw(field_width)<<next<<endl;
neat_file<<setw(field_width)<<next<<endl;
}
}
Re: HELP!!!!! I/O Streams Error Compiling
May 05, 2008 03:58PM
fin.close;
fout.close;

should be

fin.close();
fout.close();
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 06, 2008 07:13AM
WHOA!!! I am such a jerk! Thanks man..
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 06, 2008 01:11PM
I wouldn't advise using exit. Use return rather.

Exit shuts down the program on the spot whilst return, passes control back to the system code that started the program up so that it can finish cleaning up before closing the program.

Remember that the operating system doesn't just start executing your program in main(), it runs some other code first that is added by the compiler and those functions need to finish executing before closing the program down.
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 07, 2008 03:54PM
Hey man,

could you post some references backing this statement up? I've never heard that before. When you call exit() from main() no harm can really happen as the OS will clean up all memory and open file descriptors etc. Even if you interrupt a program (ctrl-c etc) midway the memory will be taken back by the OS.

<Quote>
"it runs some other code first that is added by the compiler and those functions need to finish executing before closing the program down."
</Quote>

Which function specifically are you referring to?

later,

kramlen
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 07, 2008 06:59PM
When your writing some "Hello Worlds", it will appear that an ungraceful exit for your code will do no harm - and most likely true.

But when your code has objects and you've allocate half the RAM south of equator to it - you will see the difference. Its never too early to do it right.

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Re: HELP!!!!! I/O Streams Error Compiling
May 08, 2008 08:59AM
dynamically allocated memory should be cleaned up by the programmer. but neither a return or exit will do that for you. return, just returns control to the calling function. exit, jumps out of the program completely.
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 08, 2008 05:35PM
This is compiler specific. My VS compiler treats return and exit the same way but this is not guaranteed. It forces exit to complete the stack unwinding and clears up everything properly.

Most modern operating systems isolate your memory well enough to prevent memory leaks but this is not the only resource you should be worried about. Files, network connections, etc. etc. etc.

return causes the functions to exit normally and the stack variables to be destroyed properly: base types just get deleted, objects have their destructors called. exit does not guarantee that destructors are called.

This means that although the memory that all your objects take up will most likely be freed thanks to the operating system, any code that should be run in the destructors might not run. Any non-memory resources might not be shut down and cleaned up properly.

As I said, VS doesn't allow this to happen, and I checked MinGW as well so you're just plain lucky. If you're using another compiler, you might not be.
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 09, 2008 09:34AM
Hey,

you guys can go and test it if you like...create some large object pointers in an app...and the kill the app (doing a ctrl-c or something) the memory will be taken back by the OS...

Memory leaks only are a problem on long running programs that cannot be shut down and retstarted easily (for instance a mission critical database.)

I can go and allocate half the memory (as cbrunsdonza mentioned) in a short running console app and not call delete on the pointer before the application ends. The operating system will reclaim that memory when the app finishes. This is not a feature of C++ or the compiler etc, but rather the way the OS controls access to it's own resources.

If you've done COS321(Operating Systems and architecture) you'll know what I'm talking about.

See this page for details
http://www.linuxhq.com/guides/TLK/mm/memory.html

Note the part where they say:

"These will be freed when the image has finished executing and is unloaded"

Anyway - that's the way I always thought this stuff worked.

later,

kram
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 14, 2008 10:01PM
@thornza

Windows users do not have the luxury of an OS that actually works.

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 14, 2008 11:07PM
I shall repeat what I said earlier, memory leaks are not really a problem with modern operating systems - XP included - so you don't really need to worry about that too much.

Other resources you should be worrying about are open files, data connections, network connections, drawing resources (GDI in particular for Windows apps).

If your destructors do any cleanups on these and you don't have a sophisticated compiler, using exit instead of return could cause these resources to be leaked or data corrupted, or file locks remaining in place, etc., etc.

And yes, Chris, his DOES happen on *nix as well. I'm a support programmer and our systems run on either AIX or Redhat and during the start of loadshedding, when cheapskate clients didn't have UPSs on their servers, we had our hands full of locked records in databases due to unopened row locks.
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 14, 2008 11:57PM
Whats a locked record ... never had that problem in PostgreSQL thumbs up smiley

But yes, LINUX can leak memory, Firefox is a prime example.

Ouch on the open files ... learn't the hard way years ago during my DOS days that you can only have so many files open without closeing them.

--------------------------------------------------------------
Don't Assume Everything is a Nail Just Because You Have a Really Big Hammer
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 15, 2008 10:49AM
Hey guys,

I still don't get what you guys are trying to say here. What does this have to do with the compiler you choose? I've written and debugged through assembly code - there are no "magic" functions that get added to clean up any resources you have open. Open files are handled in the kernel as are open memory resources and network connections. When a process dies these all get taken care of automatically by the kernel.

As for databases (well - I work with Postgres) as soon as a connection dies all transactions get rolled back - no locked rows. Not sure how other databases manage this. But it would seem kind of stupid for them to not clean up after themselves if some client decides to disconnect for some reason (i.e. network failure or seg faulting etc).



Your destructors do NOT need to be called at all. They are only necessary for managing resources during the lifetime of the process. Once the process is killed the OS takes back all resources that were allocated to the running image.


This is the way most modern OSs work - if they didn't work this way then there would be major problems with them.

Please post some reference supporting your argument. If I'm wrong about this then I would really like to know - cos then I have a totally wrong mental model of how my programs actually work.



thanks,

thornza
Anonymous User
Re: HELP!!!!! I/O Streams Error Compiling
May 15, 2008 11:16AM
As a further argument from my side:

Consider that your program is acting as a client of the operating system kernel. Any request you make for a resource from the OS (file, mem etc) is actually a system call into the kernel. The kernel actually manages the resource and allows the client
application access to the resource. The kernel manages it's own internal data structures for controlling processes and resource allocations.

This is why I always was of the impression that when a process is killed or shut down improperly, the kernel would manage the correct deallocation of any open resources held by the offending process.

any way - perhaps my mental model is flawed??


later guys!
avatar Re: HELP!!!!! I/O Streams Error Compiling
May 15, 2008 01:22PM
@thornza:

The issue that I raised is the difference between exit and return. How this is handled is compiler-specific. My MS compiler has heap management code that gets compiled into the executable and calls main or WinMain. It's a bit like a garbage collector and ensures that destructors are called before exiting the program. Even if exit is called and aborts the program, the compiler-added code makes sure to clean up before closing down.

Not all compilers have heap management code, they rely on the OS to clean up. In this case, return will cause the stack to unwind properly and exit will just abort.

When I use 'resource' I'm being very general here, not just memory and file accesses. Your program architecture might require that some logic be called when an object is destroyed and if exit is called and aborts the program, this might not happen.

As far as databases go, my company still uses the ISAM database model. It's antiquated but lightening fast and very easy to use. It has some SQL functionality to it but not much. I don't understand the system completely since I work on the code side, not the DB side but if a network connection gets severed, ghost users can keep a table row locked.

Thanks goodness the company is moving into the 21st century. With the release of the newest version of the program, they're handing over DB control to Oracle and discontinuing R&D into the proprietary DB engine. Unfortunately, this means headaches for us and flexibility will greatly deminish.
Sorry, only registered users may post in this forum.

Click here to login