Function/ifstream woes

This is a discussion on Function/ifstream woes within the C++ Programming forums, part of the General Programming Boards category; Ok, I am trying to learn other types of functions, because all I knew how to do was inline functions. ...

  1. #1
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094

    Function/ifstream woes

    Ok, I am trying to learn other types of functions, because all I knew how to do was inline functions. Anyway, in the process of rewriting an app I made, I am having problems I don't understand at all.

    Quote Originally Posted by Dev-C++ 5
    C:/Dev-Cpp/include/c++/3.4.2/bits/ios_base.h: In copy constructor `std::basic_ios<char, std::char_traits<char> >::basic_ios(const std::basic_ios<char, std::char_traits<char> >&)':
    C:/Dev-Cpp/include/c++/3.4.2/bits/ios_base.h:738: error: `std::ios_base::ios_base(const std::ios_base&)' is private
    NewMain.cpp:28: error: within this context

    C:/Dev-Cpp/include/c++/3.4.2/streambuf: In copy constructor `std::basic_filebuf<char, std::char_traits<char> >::basic_filebuf(const std::basic_filebuf<char, std::char_traits<char> >&)':
    C:/Dev-Cpp/include/c++/3.4.2/streambuf:769: error: `std::basic_streambuf<_CharT, _Traits>::basic_streambuf(const std::basic_streambuf<_CharT, _Traits>&) [with _CharT = char, _Traits = std::char_traits<char>]' is private
    NewMain.cpp:28: error: within this context

    NewMain.cpp: In function `int main()':
    NewMain.cpp:28: error: initializing argument 1 of `int loadfile(std::ifstream)'
    Code:
    #include <iostream>
    #include <fstream>
    #include <string>
    
    using namespace std;
    
    int loadfile(ifstream file);
    
    int main ()
    {
        cout<<"Welcome to TodesRunes 1.1!"<<endl;
        int iRun = 1;     //menu choice
        int iCubed = 0;   //if the cubing function has been called
        const int iNumOfRunes = 33;
        int aiRunes1[iNumOfRunes] = {0};
        int aiRunes2[iNumOfRunes] = {0};
        const string sRuneNames[iNumOfRunes] = {"El", "Eld", "Tir", "Nef", "Eth", "Ith",
                                                "Tal", "Ral", "Ort", "Thul", "Amn", "Sol",
                                                "Shael", "Dol", "Hel", "Io", "Lum", "Ko",
                                                "Fal", "Lem", "Pul", "Um", "Mal", "Ist",
                                                "Gul", "Vex", "Ohm", "Lo", "Sur", "Ber",
                                                "Jah", "Cham", "Zod"};
        
    
        ifstream file;     //ATMA dump
        ofstream save;     //TodesRune dump
    
        loadfile(file);    //loads the ATMA dump
    }
    
    int loadfile(ifstream file)
    {
            
        file.open("Runes.txt");
        if (!file.is_open())
            file.open( "runes.txt");
            if (!file.is_open())
                file.open( "rune.txt");
                if (!file.is_open())
                    file.open( "Rune.txt");
                    if (!file.is_open())
                    while(!file.is_open())
                    {
                        string filename;
                        cout<<"TodesRunes could not find your ATMA dump,"<<endl
                            <<"please specify the path to the txt file."<<endl;
                        getline(cin,filename);
                        file.open(filename.c_str());
                    }
        return 0;        
    }
    Thank you
    ~Wraith

  2. #2
    Registered User
    Join Date
    Dec 2004
    Posts
    32
    You just need to pass your stream by reference.
    Code:
    int loadfile(ifstream &file);

  3. #3
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,399
    Heh, a tricky one, and very non-obvious to a new programmer. Here's the deal: The ifstream class has a private copy constructor. You really don't want to have copies of the same stream floating around. So, when you pass your ifstream object by value to your function, it tries to create a copy of the stream.

    To fix this, you have to pass the object by reference.
    Code:
    int loadfile(ifstream& file)
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  4. #4
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Quote Originally Posted by Anubis
    You just need to pass your stream by reference.
    Code:
    int loadfile(ifstream &file);
    Oops, thank you!

  5. #5
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    I didn't want to make another post so soon, but now I am getting another error that I don't understand =/

    Quote Originally Posted by Dev-C++ 5
    NewMain.cpp: In function `int counter(int*, std::string*, int&, std::ifstream&)':
    NewMain.cpp:66: error: could not convert `(&sRune)->std::basic_string<_CharT, _Traits, _Alloc>::operator= [with _CharT = char, _Traits = std::char_traits<char>, _Alloc = std::allocator<char>](((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >&)((const std::basic_string<char, std::char_traits<char>, std::allocator<char> >*)((+(((unsigned int)iCounter) * 4u)) + sRuneNames))))' to `bool'
    NewMain.cpp:70: error: expected primary-expression before ')' token
    NewMain.cpp:70: error: expected `;' before ')' token
    NewMain.cpp:78: error: expected `}' at end of input
    Code:
    int counter(int aiRunes1[], string sRuneNames[], int& iNumOfRunes, ifstream& file)
    {   
        int iCounter = 0;
        string sLine;
        while (getline( file , sLine ))
    	{
            if (sLine.empty() || (sLine == "\r")) continue;
            int colon_pos = sLine.find(':');
            string sRune = sLine.substr(colon_pos+2,sLine.size()-colon_pos-7);
            while(iCounter>=iNumOfRunes)
            {
                if(sRune=sRuneNames[iCounter])
                {
                    aiRunes1[iCounter]++;
                    iCounter=iNumOfRunes+1;
                )
                else
                {
                    iCounter++;
                }
            }
        }
        return 0;
    }
    I know it has something to do with my array, but I dont know if just did it wrong, or what I am trying to do can't be done.

    EDIT:This was added to my previous code, I didn't want to repost it all, and make it a super long post. There is a prototype for it up above main().
    Last edited by Wraithan; 12-03-2005 at 02:25 PM.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    You're trying to convert a string to a bool, according to the first error. Can you show us which line it's on (ie, which line is line 66)?

    BTW:
    Code:
    if(sRune=sRuneNames[iCounter])
    Make that ==.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Code:
                if(sRune=sRuneNames[iCounter])
                {
                    aiRunes1[iCounter]++;
                    iCounter=iNumOfRunes+1;
                )
    Change that ) to }. That's what's causing your last three errors.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Quote Originally Posted by dwks
    You're trying to convert a string to a bool, according to the first error. Can you show us which line it's on (ie, which line is line 66)?

    BTW:
    Code:
    if(sRune=sRuneNames[iCounter])
    Make that ==.
    Ok the = to == is another dumb mistake lol. And that is line 66.

    Code:
    if(sRune=sRuneNames[iCounter])

    heh, you changed your post, anyway, you are right about the if statement and ) } problem, thank you!
    Last edited by Wraithan; 12-03-2005 at 02:53 PM.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Yes, sorry, my mistake on the last point. Edited the post.

    Look at my second post, too.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    *slaps head*
    Code:
    string sRuneNames[]
    I don't think you want those there.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  11. #11
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Code:
        const string sRuneNames[iNumOfRunes] = {"El", "Eld", "Tir", "Nef", "Eth", "Ith",
                                                "Tal", "Ral", "Ort", "Thul", "Amn", "Sol",
                                                "Shael", "Dol", "Hel", "Io", "Lum", "Ko",
                                                "Fal", "Lem", "Pul", "Um", "Mal", "Ist",
                                                "Gul", "Vex", "Ohm", "Lo", "Sur", "Ber",
                                                "Jah", "Cham", "Zod"};
    I do want that there .

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,047
    Okay. Fix the ) -> } and the == and see if you get any errors.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  13. #13
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    No errors, thanks a ton. Now time to write the ouput functions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. asctime woes
    By magis in forum C++ Programming
    Replies: 7
    Last Post: 06-23-2005, 04:01 PM
  2. Simple Linked list woes!
    By Bajanine in forum Windows Programming
    Replies: 4
    Last Post: 06-22-2005, 11:14 PM
  3. Ugh, ComboBox Woes
    By Zeusbwr in forum Windows Programming
    Replies: 11
    Last Post: 04-11-2005, 11:14 PM
  4. Doc/view woes fixed
    By VirtualAce in forum Windows Programming
    Replies: 4
    Last Post: 04-08-2005, 09:40 AM
  5. strstr woes
    By magis in forum C++ Programming
    Replies: 6
    Last Post: 04-02-2005, 09:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21