LinuxQuestions.org
Visit Jeremy's Blog.
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 12-14-2004, 02:12 PM   #1
javakid
LQ Newbie
 
Registered: Nov 2004
Location: Mesa, AZ
Distribution: Redhat
Posts: 8

Rep: Reputation: 0
Unhappy 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
 
Old 12-14-2004, 02:29 PM   #2
LauroMoura
Member
 
Registered: Feb 2004
Location: Carpina, PE, Brazil
Distribution: Ubuntu 5.10
Posts: 44

Rep: Reputation: 15
I don't know C++, but it would help the other guys if you use "CODE" tag to place the source code.
 
Old 12-14-2004, 02:30 PM   #3
dakensta
Member
 
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
test.h doesn't know it is dealing with std::ifstreams
 
Old 12-14-2004, 02:39 PM   #4
javakid
LQ Newbie
 
Registered: Nov 2004
Location: Mesa, AZ
Distribution: Redhat
Posts: 8

Original Poster
Rep: Reputation: 0
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)
 
Old 12-14-2004, 02:45 PM   #5
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 12-14-2004, 02:54 PM   #6
javakid
LQ Newbie
 
Registered: Nov 2004
Location: Mesa, AZ
Distribution: Redhat
Posts: 8

Original Poster
Rep: Reputation: 0
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
 
Old 12-14-2004, 03:26 PM   #7
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 23,005
Blog Entries: 11

Rep: Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903Reputation: 903
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
 
Old 12-14-2004, 03:36 PM   #8
javakid
LQ Newbie
 
Registered: Nov 2004
Location: Mesa, AZ
Distribution: Redhat
Posts: 8

Original Poster
Rep: Reputation: 0
Thanks all,,,That worked and I really appreciate your help. I will spread your goodwill around.

Steve
 
Old 12-16-2004, 01:03 AM   #9
suowei1979
LQ Newbie
 
Registered: Sep 2004
Location: BeiJing,China
Distribution: Red Hat
Posts: 25

Rep: Reputation: 16
 
  


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
Problem compiling Kami.JZ Linux - Newbie 6 10-26-2004 07:28 AM
compiling problem ... chaks Programming 2 03-29-2004 10:14 AM
problem compiling :( celeron2002 Linux - Software 1 02-22-2004 08:19 PM
Compiling Problem... ThE_DeaD Linux - Newbie 10 12-14-2003 04:14 PM
Compiling 2.4.20 problem frankietomatoes Linux - General 1 01-09-2003 06:43 PM


All times are GMT -5. The time now is 07:29 PM.

Main Menu
Advertisement
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