LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   Problem compiling (http://www.linuxquestions.org/questions/programming-9/problem-compiling-266356/)

javakid 12-14-2004 02:12 PM

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

LauroMoura 12-14-2004 02:29 PM

I don't know C++, but it would help the other guys if you use "CODE" tag to place the source code. ;)

dakensta 12-14-2004 02:30 PM

test.h doesn't know it is dealing with std::ifstreams

javakid 12-14-2004 02:39 PM

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)

Tinkster 12-14-2004 02:45 PM

Code:

/home/stevey/835/test.h:16: error: `file835' was not declared in this scope
And that's quite clear as well ... you're using
something in .h that you're going to define in
.cpp which kind of suggests that the compiler
may be right complaining ;)


Cheers,
Tink

javakid 12-14-2004 02:54 PM

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

Tinkster 12-14-2004 03:26 PM

Quote:

Originally posted by javakid
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:


So long as the object being passed is a ifstream, the name used in the method implementation should be fine.

You're right, I was wrong :)
/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

javakid 12-14-2004 03:36 PM

Thanks all,,,That worked and I really appreciate your help. I will spread your goodwill around.

Steve

suowei1979 12-16-2004 01:03 AM

:)


All times are GMT -5. The time now is 03:22 AM.