![]() |
Problem compiling
I have a test scenrio I have wrote up to pass a ifstream object to a class.
Both files are in the same directory, and I'm using: g++ -o splitter splitter.cpp on the commandline to compile. I then get this for the error: In file included from splitter.cpp:9: /home/stevey/835/test.h:16: error: `ifstream' was not declared in this scope /home/stevey/835/test.h:16: error: `file835' was not declared in this scope /home/stevey/835/test.h:16: error: parse error before `*' token /home/stevey/835/test.h:27: error: `ifstream' was not declared in this scope /home/stevey/835/test.h:27: error: `file835' was not declared in this scope /home/stevey/835/test.h:27: error: parse error before `*' token splitter.cpp: In function `int main(int, char**)': splitter.cpp:26: warning: cannot pass objects of non-POD type `struct std::ifstream' through `...'; call will abort at runtime here is the code I'm testing with: The class file --------------------------------------------------------------------------------------------- //test.h //12-14-2004 #include <iostream> #include <fstream> /*---------------------------------*/ class TEST_Class{ public: TEST_Class(); ~TEST_Class(); int gettoken (ifstream &file835, char *element, int length); };// /*****************************************************/ TEST_Class::TEST_Class(){ }//TEST_Class TEST_Class::~TEST_Class(){ }//~TEST_Class /*---------------------------------------------------------------------*/ int TEST_Class::gettoken(ifstream &file835, char *element, int length){ return(0); }//gettoken --------------------------------------------------------------------------------------------- The main file --------------------------------------------------------------------------------------------- //splitter.cpp //12-14-2004 #include <iostream> #include <fstream> #include "/home/stevey/835/test.h" using namespace std; /*---------------------------------------------------------------------*/ int main( int argc, char *argv[] ){ int done; char Elements[30]; ifstream file835("test"); if(!file835.is_open()){ cout<<"Could not open file\n"; return(1); }//if TEST_Class testobject; done=testobject.gettoken(file835, (char *)Elements, 30); file835.close(); return(0); }//main --------------------------------------------------------------------------------------------- I'm still learning and I need a little help here. Steve |
I don't know C++, but it would help the other guys if you use "CODE" tag to place the source code. ;)
|
test.h doesn't know it is dealing with std::ifstreams
|
I'm sorry LauroMoura, Im not sure what you mean by "CODE" tag, but I will look around on this site to see if there is an example.
dakensta I'm unfamiliar w this, are you saying when programming in linux I need to add a: using std::ifstream in the header file test.h? I'm new to the c++ linux programming, though Ive done a bit with Visual Studio(all that is done for you in the IDE) |
Code:
/home/stevey/835/test.h:16: error: `file835' was not declared in this scopesomething in .h that you're going to define in .cpp which kind of suggests that the compiler may be right complaining ;) Cheers, Tink |
Tink, I dont believe that is the problem. That is just the name I use in the protocol of the file in the class, and then it is used in the implementation later in the same file. That is a legal call. It could have been named anything such as bugger:
class TEST_Class{ public: TEST_Class(); ~TEST_Class(); int gettoken (ifstream &bugger, char *element, int length); };// int TEST_Class::gettoken(ifstream &bugger, char *element, int length){ return(0); }//gettoken So long as the object being passed is a ifstream, the name used in the method implementation should be fine. Steve |
Quote:
/me is slowly waking up... The problem indeed goes away with a using namespace std; before the class definition (as dakensta suggested)... alternatively you can use std::ifstream (std::cout, std::string ... and so on) (which, btw, is the recommended but typing-intense method). Cheers, Tink |
Thanks all,,,That worked and I really appreciate your help. I will spread your goodwill around.
Steve |
:)
|
| All times are GMT -5. The time now is 02:58 PM. |