Thread: Problem With Virtual Methods

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    24

    Problem With Virtual Methods

    Ok am in the header file I am going to post below, I i get the following errors

    error C2061: syntax error : identifier 'fstream'

    I get those two errors for the two methods, I searched for the error but what i found didn't help, so am here once again.

    Code:
    #ifndef INSURANCE_H
    #define INSURANCE_H
    #include "Date.h"
    using std::string;
    #include<string>
    #include<fstream>
    
    //Parent Class || Abstract Class
    class Policy{
    
    private:
    	
    	
    	
    	string policy_type;//Policy Type
    
    protected:
    	int pol_num;//policy number
    	string pol_hol;//policy holder
    	Date eff_date;//effective date of policy
    	Date exp_date;//expiry date of policy
    
    public:
    	Policy();//default constructor, used to instantiate the object
    	Policy(Date&, Date&,  string);//Primary Constructor For Policy
    	~Policy();//Destructor
    		
    	//Accessors
    		int getPolNum();
    		string getPolHol();
    		Date getEffDate();
    		Date getExpDate();
    		
    		
    		
    			//Mutators
    			void setPolNum(int);
    			void setPolHol(string);
    			void setEffDate(int, int, int);
    		
    			void setExpDate(int, int, int);
    		
    			
    			
    			//Calculation Method
    			virtual float Premium (float)= 0;//This is the polimorphic function declaration 
    			virtual void Add (fstream)=0;	
    			//virtual void Delete(void)= 0;
    
    			//virtual void Edit(void)= 0;
    
    			
    			virtual void View(fstream)= 0;
    };//Closes The Class
    
    #endif

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    you should not add using directive to the header.
    instead - use fully qualified names like

    std::string

    or

    std::fstream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  3. #3
    The larch
    Join Date
    May 2006
    Posts
    3,573
    It's std::fstream (like std::string, which you should type like that in a header file) and it cannot be passed by value.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  4. #4
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Thanks guys but the am std::fstream alone doesn't work but when i add the using before it, it works perfectly same for the std::string; and i set the parameter as pass by reference, thanx alot...

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    Quote Originally Posted by frassunit View Post
    Thanks guys but the am std::fstream alone doesn't work but when i add the using before it, it works perfectly same for the std::string; and i set the parameter as pass by reference, thanx alot...
    as i said - you should not add using into header.

    so better remove it and search for any occurense of fstream type in your header. Then replace it with std::fstream
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    Ok, thanx for tha previous help but i have a problem with viewing something after i save it to a file, i cant seem to get the syntax for it. Say I have the function for adding data to a file like the one am gonna post below how would i access that file from a different function to view specific records.
    Code:
    void HomeIns::  Add(ofstream &PolicyFile)
    	 	 
     {
    		//ofstream PolicyFile;
    	 PolicyFile.open("Policy.txt",ios::out | ios::app| ios::binary );
    		HomeIns  *nHome;
    		Date XDate, EDate;
    		int day, month, year;
    		int xday, xmonth, xyear;
    		float home_val;
    		string fladdr;
    		string sladdr;
    		string Roof_Type;
    		int hnum;
    
    		string person;
    		float premium_cost;
    		
    
    
    		cout<<"Enter The Name Of The Policy Holder: ";
    		cin>>person;
    		cout<<"\n\nPlease Enter The Value Of The House: ";
    		cin>>home_val;
    		cout<<"\n\nPlease Enter The First Line Of Address For The House: ";
    		cin>>fladdr;
    		cout<<"\n\nPlease Enter The Second Line Of Address For The House: ";
    		cin>>sladdr;
    		cout<<"\n\nPlease Enter The Roof Type of The House: ";
    		cin>>Roof_Type;
    		cout<<"\n\nEnter The Day The Policy begins: ";
    		cin>>day;
    		cout<<"\n\nEnter The Month The Policy begins: ";
    		cin>>month;
    		cout<<"\n\nEnter The Day The Year begins: ";
    		cin>>year;
    		cout<<"\n\nEnter The Day The Policy finishes: ";
    		cin>>xday;
    		cout<<"\n\nEnter The Month The Policy finishes: ";
    		cin>>xmonth;
    		cout<<"\n\nEnter The Year The Policy finishes: ";
    		cin>>xyear;		
    		system("CLS");
    		
    		Home home(home_val, fladdr, sladdr, Roof_Type);
    		
    		setEffDate(day, month, year);
    		
    		setExpDate(xday, xmonth, xyear);
    		
    		EDate=getEffDate();
    		
    		XDate= getExpDate();
    
    		hnum=policynumber();
    		
    
    		premium_cost= Premium(home_val);
    		
    			
    		nHome= new HomeIns(home, premium_cost,EDate, XDate, person, hnum );
    
    		if
    			(!PolicyFile){
    		cout<<"Error Creating FIle!!!\nSORRY!!!";
    		return;
    	}
    	PolicyFile<<"\n";
    	PolicyFile.write(reinterpret_cast<char*>(&nHome),sizeof(HomeIns));
    	PolicyFile<<"\n";
    	PolicyFile.close();
    
    	
    		//nHome= new HomeIns[sizeof(HomeIns)];
    		//nHome= &HomeIns(home, premium_cost,EDate, XDate, person );
    		
    		
    	}
    the coding above works like a charm. this is what i have for the viewing function say for instance i would like to view the third record what would the syntax be?
    Code:
    void HomeIns:: View(ifstream &Policyfile){
    	 HomeIns *vHome;
    	 int number;
    	 
    		  Policyfile.open("Policy.txt", ios::in | ios::binary);
    		
    	 	if(!Policyfile){
    		
    			cout<<"Error Creating File!!!\nSORRY!!!";
    		return;
    			}
    		cout<<"Enter The Policy Number Of For The Home You Would Like To View: ";
    		cin>>number;
    		vHome->pnum=number;
    		while(!Policyfile.eof() && vHome->pnum>0){
    			Policyfile.seekg( (vHome->pnum-1)* sizeof(HomeIns));
    			Policyfile.read(reinterpret_cast<char *>(&vHome), sizeof(HomeIns));
    			if(vHome->pnum!=0){
    				cout<<vHome->home.getFAddress();
    			}
    			else {
    				cout<<"Enter Another Record Number";
    				cin>>vHome->pnum;
    			}
    		}
    			Policyfile.close();
    
     }
    what i get after running the program and i try to view i get an exception handling error or something like that.
    I KNow programming can be a dick sometimes.
    Last edited by frassunit; 03-22-2009 at 05:05 PM. Reason: Just A Stress Releaver

  7. #7
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,794
    In your view function vHome is not initialized pointer - you cannot write there
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    24
    I don't want to write in the view i just want to get whats stored in the file at the given seek position. I dont know the syntax for it that's why i asked...

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need Partners (D&D fan preferably)
    By C_ntua in forum Game Programming
    Replies: 44
    Last Post: 11-22-2008, 09:21 AM
  2. Replies: 7
    Last Post: 11-17-2008, 01:00 PM
  3. Declaring instances of different implementations
    By dwks in forum C++ Programming
    Replies: 8
    Last Post: 07-16-2008, 11:43 AM
  4. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 01:30 PM
  5. Exporting Object Hierarchies from a DLL
    By andy668 in forum C++ Programming
    Replies: 0
    Last Post: 10-20-2001, 01:26 PM