CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Apr 2010
    Posts
    5

    Help with error message 2248 cannot access private member declared in class

    Ok so I have a simple program that reads in numbers from a file and counts the amount of even and odd numbers in the file and prints out the results, but when I try to compile I get the error C2248 and when i try to find the error it brings me to another page, so is my error not even part of my code and it is something else. Any help would be great

    Code:
    #include <iostream>		// For cin / cout
    #include <string>		// For file name
    #include <iomanip>		// For setw
    #include <fstream>		// For file input
    using namespace std;
    
    void IsEven(ifstream din);
    
    int main ()
    {  
    	string fileName;
    
    	cout << "Please enter a file name "<<endl;
    	cin >> fileName;
    
    	ifstream data;
    
    	data.open(fileName.c_str());
    
    	if(!data)
    	{
    		cout << "No such file found!" << endl;
    	}
    	else
    	{
    		IsEven(data);
    	}
        return 0;
    }
    
    void IsEven(ifstream din)
    {
    	/*
    		Purpose: Takes input from a file and determines how many even numbers and odd numbers are in the file
    
    		Pre:	 File
    
    		Post:	 How many even numbers are in the file and how many odd numbers are in the file
    	*/
    	int num;
    	int even = 0;
    	int odd = 0;
    	din >> num;
    	while(din)
    	{
    		if(num % 2 == 0)
    			even = even ++;
    		else
    			odd = odd ++;
    		din >> num;
    	}
    
    	cout << "There are " << even << " numbers in the file and " << odd << " numbers in the file." << endl;
    
    }


    this is my error message

    1>c:\program files\microsoft visual studio 8\vc\include\fstream(675) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> c:\program files\microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1> This diagnostic occurred in the compiler generated function 'std::basic_ifstream<_Elem,_Traits>::basic_ifstream(const std::basic_ifstream<_Elem,_Traits> &)'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: Help with error message 2248 cannot access private member declared in class

    you can not pass a stream by value ... you need to pass by reference:

    Code:
    void IsEven(ifstream & din);
    And change the actual function in the same way.

  3. #3
    Join Date
    Apr 2010
    Posts
    5

    Re: Help with error message 2248 cannot access private member declared in class

    works perfect ... thanks a lot

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured