ifstream error
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10

Thread: ifstream error

  1. #1
    Join Date
    Jul 2013
    Posts
    8

    ifstream error

    Running:

    ifstream fin1;

    results in the following error message:

    error C2371: 'fin1' : redefinition; different basic types

    Your help please.

  2. #2
    Join Date
    Apr 1999
    Posts
    27,426

    Re: ifstream error

    Quote Originally Posted by qabdullah View Post
    Running:

    ifstream fin1;

    results in the following error message:

    error C2371: 'fin1' : redefinition; different basic types

    Your help please.
    When you get a compiler error, you're supposed to post the smallest, but full example of the code that doesn't compile, not just one line plucked out of many lines.
    Code:
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
       ifstream fin;
    }
    Note that I

    1) Used code tags when posting the code, so that it appears properly formatted.

    2) #included all headers necessary for the sample to compile.

    There is no error when I compile this code. So take the code above, and add to it so that you get the compiler error you say you're getting. Once you do that, repost your entire example code here (within code tags).

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    Jul 2013
    Posts
    8

    Re: ifstream error

    Thank you Paul for the promt response. Here is my code that I am trying to compile.

    #include <stdlib.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include "stdafx.h"
    #include <iomanip>
    #include <ios>
    #include <vector>
    #include <sstream>

    using std::cin;
    using std::cout;
    using std::endl;
    using std::setprecision;
    using std::cerr;
    using std::ifstream;
    using std:fstream;
    using std::string;
    using namespace std;

    int main()
    {
    string XYZOPK;
    string XYZRPH;
    string fin1;
    string fin2;
    string fout;
    int i;

    cout << "\n Enter the name of the file containing the photogrammetric EOs format: ID X Y Z Omega Phi Kappa\n";
    cin >> XYZOPK;
    ifstream fin1;
    fin1.open(XYZOPK);
    if(!fin1){
    cout<<" cannot open input file .\n";
    }
    fin1.open(XYZOPK);



    if (!fin1.is_open())
    {
    cerr <<"Could not open"<<XYZOPK<<endl;
    fin1.clear();

    }

    cout << " Your file is: " << XYZOPK << "\n";

    cout << "\n Enter the name of the file containing the Inertia EOs format: ID X Y Z roll pitch heading\n";
    cin >> XYZRPH;
    ifstream fin2;
    fin2.open(XYZRPH);

    if (!fin2.is_open())
    {
    cerr <<"Could not open"<<XYZRPH<<endl;
    fin2.clear();

    }

    cout<< "\ Your file is: " << fin1 << "\n";
    ofstream fout("output.txt");

    fout <<"this is the first stored line\n";
    fout.close();
    return 0;
    }

    The latest errors I get are the following:

    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(17): error C2039: 'cin' : is not a member of 'std'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(17): error C2873: 'cin' : symbol cannot be used in a using-declaration
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(18): error C2039: 'cout' : is not a member of 'std'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(18): error C2873: 'cout' : symbol cannot be used in a using-declaration
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(21): error C2039: 'cerr' : is not a member of 'std'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(21): error C2873: 'cerr' : symbol cannot be used in a using-declaration
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(39): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(40): error C2065: 'cin' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(41): error C2079: 'fin1' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(41): error C2371: 'fin1' : redefinition; different basic types
    1> c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(34) : see declaration of 'fin1'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(42): error C2039: 'open' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(43): error C2088: '!' : illegal for class
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(44): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(46): error C2039: 'open' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(50): error C2039: 'is_open' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(52): error C2065: 'cerr' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(57): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(59): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(60): error C2065: 'cin' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(61): error C2079: 'fin2' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(61): error C2371: 'fin2' : redefinition; different basic types
    1> c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(35) : see declaration of 'fin2'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(62): error C2039: 'open' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(64): error C2039: 'is_open' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(66): error C2065: 'cerr' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(71): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(71): warning C4129: ' ' : unrecognized character escape sequence
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(72): error C2079: 'fout' uses undefined class 'std::basic_ofstream<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(72): error C2371: 'fout' : redefinition; different basic types
    1> c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(36) : see declaration of 'fout'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(72): error C2440: 'initializing' : cannot convert from 'const char [11]' to 'int'
    1> There is no context in which this conversion is possible
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(74): error C2088: '<<' : illegal for class
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication5\consoleapplication5\consoleapplication5.cpp(75): error C2039: 'close' : is not a member of 'std::basic_string<_Elem,_Traits,_Alloc>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>,
    1> _Alloc=std::allocator<char>

  4. #4
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: ifstream error

    Please use code tags as requested by Paul in post #2. Go Advanced, select code and click '#'.

    You have defined fin1 as

    Code:
    string fin1;
    Then you try to define fin1 again as

    Code:
    ifstream fin1;
    The same applies for fin2 and fout.

    Also the .open method takes as parameter const char * and not string. So

    Code:
    fin1.open(XYZOPK);
    should be

    Code:
    fin1.open(XYZOPK.c_str());
    The same for fin2.

    Also, if the first open for fin1 fails, you then try to open fin1 again with the same name!
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  5. #5
    Join Date
    Jul 2013
    Posts
    8

    Re: ifstream error

    I apologize in advance for not using the tag as I am not sure how to do that. I shortened the program so we can focus on the problem which remains even after applying all the changes you suggested. It is telling me that cout and cin is not part of the std library. t still having problem with the definition of fin1. Here is the code and the resulting of building it. Thank you in advance for your help. Qassim

    #include <stdlib.h>
    #include <math.h>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include "stdafx.h"
    #include <iomanip>
    #include <ios>
    #include <vector>
    #include <sstream>

    //using std::cin;
    using std::cout;
    using std::endl;
    using std::setprecision;
    using std::cerr;
    using std::ifstream;
    using std:fstream;
    using std::string;
    using namespace std;

    // using std::namespace;

    // int main (int argc, char **argv)
    int main()
    {
    string XYZOPK;
    string XYZRPH;

    int i;

    cout << "\n Enter the name of the file containing the photogrammetric EOs format: ID X Y Z Omega Phi Kappa\n";
    cin >> XYZOPK;
    ifstream fin1;
    fin1.open(XYZOPK.c_str());
    return 0;
    }
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(17): error C2039: 'cout' : is not a member of 'std'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(17): error C2873: 'cout' : symbol cannot be used in a using-declaration
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(20): error C2039: 'cerr' : is not a member of 'std'
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(20): error C2873: 'cerr' : symbol cannot be used in a using-declaration
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(36): error C2065: 'cout' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(37): error C2065: 'cin' : undeclared identifier
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(38): error C2079: 'fin1' uses undefined class 'std::basic_ifstream<_Elem,_Traits>'
    1> with
    1> [
    1> _Elem=char,
    1> _Traits=std::char_traits<char>
    1> ]
    1>c:\users\abdullahq\documents\visual studio 2012\projects\consoleapplication6\consoleapplication6\consoleapplication6.cpp(39): error C2228: left of '.open' must have class/struct/union

  6. #6
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,564

    Re: ifstream error

    Code:
    #include "stdafx.h"
    Should be the first line in the file. The includes before it are ignored.

  7. #7
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: ifstream error

    I apologize in advance for not using the tag as I am not sure how to do that.
    I told you in my post #4 how to do that!

    Try this. It compiles and runs OK on my system.

    Code:
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <iomanip>
    #include <ios>
    #include <vector>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
    string	XYZOPK;
    string	XYZRPH;
    
    	cout << "Enter the name of the file containing the photogrammetric EOs format: ID X Y Z Omega Phi Kappa ";
    	cin >> XYZOPK;
    
    ifstream	fin1;
    
    	fin1.open(XYZOPK.c_str());
    	if (!fin1.is_open()) {
    		cerr << "Could not open " << XYZOPK << endl;
    		return 1;
    	}
    
    	cout << "Your file is: " << XYZOPK << endl;
    
    	cout << "Enter the name of the file containing the Inertia EOs format: ID X Y Z roll pitch heading ";
    	cin >> XYZRPH;
    
    ifstream	fin2;
    	
    	fin2.open(XYZRPH.c_str());
    	if (!fin2.is_open()) {
    		cerr << "Could not open " << XYZRPH << endl;
    		return 2;
    	}
    
    	cout << "Your file is: " << XYZRPH << endl;
    
    ofstream	fout("output.txt");
    
    	fout << "this is the first stored line\n";
    	fout.close();
    
    	return 0;
    }
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

  8. #8
    Join Date
    Jul 2013
    Posts
    8

    Re: ifstream error

    Thank you very much, that was the problem. I am a happy camper now I guess it is the learning experience. Thank you for your patience. I find out where is the Go Advanced button, Heyy.

  9. #9
    Join Date
    Jul 2013
    Posts
    8

    Re: ifstream error

    Thank you for your determination in helping me. The code you sent me came clean from all errors except this one "unexpected end of file while looking for precompiled". I was able to run it by moving the #include "stdafx.h" to the front. Thanks again.

  10. #10
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,335

    Re: ifstream error

    Quote Originally Posted by Philip Nicoletti View Post
    Code:
    #include "stdafx.h"
    Should be the first line in the file. The includes before it are ignored.
    ...when using pre-compiled header and stdafx.h is specified as the 'use PCH through file' in the project properties. If PCH is used, then

    Code:
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <fstream>
    #include <iomanip>
    #include <string>
    #include <iomanip>
    #include <ios>
    #include <vector>
    #include <sstream>
    would all usually go in the PCH through file (usually stdafx.h). If a different name for the PCH through file is specified and PCH is enabled, then the specified PCH through file should be the first file included.
    Last edited by 2kaud; July 3rd, 2013 at 05:23 AM.
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

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


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center