By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,750 Members | 1,221 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,750 IT Pros & Developers. It's quick & easy.

Question...

P: n/a
I have a very simple code which is consited of a simple class with a
constructor which takes an argument of type ifstream. Basically I
would like to provide a string in the main method which will
initialize a ifstream object and call the constructor... well I get
compiling errors...

#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>

class Demo
{ public:

Demo(ifstream);
};

Demo::Demo(ifstream inputImg)
{ ifstream input = inputImg;
}

void main(char args[])
{ ifstream file = (args);
Demo d(file);
}

What's wrong with it??? I am just got started with C++ ;-)
Thanks!
Jul 22 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
Vassilis Stratis wrote:
#include <iostream.h>
#include <fstream.h>
#include <stdlib.h>
Don't use these headers, they are old and, in the case of the first two,
non-standard. Instead use:
#include <iostream>
#include <fstream>
#include <cstdlib>
using namespace;

You're also not using <iostream> and <cstdlib> in the example you provided,
so why include them?
class Demo
{ public:

Demo(ifstream);
};
Not possible. ifstream has no copy constructor, so you can't have a
pass-by-value argument of type ifstream.
Demo::Demo(ifstream inputImg)
{ ifstream input = inputImg;
}
Again, ifstream has no copy constructor, so you can't assign one ifstream to
another.
void main(char args[])
This is not a valid definition of main. Main *always* returns int. If you
want to use parameters, you must write is as follows:
int main(int argc, char *argv[])
argc will contain the amount of parameters, argv is an array than contains
argc elements. argv[0] is the name of your executable, argv[1] the first
argument (if any) and so on.
{ ifstream file = (args);
argv is an array, so use argv[1]. Also, use parentheses or =, not both: so

ifstream file(argv[1]);

or

ifstream file = argv[1];
Demo d(file);
Not possible, because you can't pass an ifstream by value, like I said
above.
}

What's wrong with it??? I am just got started with C++ ;-)


You'll need to find another solution for what you're trying to do, because
besides a lot of errors, it also contains something that is impossible:
copying an ifstream.

--
Unforgiven

"You can't rightfully be a scientist if you mind people thinking
you're a fool."
Jul 22 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.