C++ Error
Oct 25 2007, 9:30 pm
By: l3lack-l3ahamut  

Oct 25 2007, 9:30 pm l3lack-l3ahamut Post #1



Basically this program is used to count the number of words in a file and also average the number of words per line. However I get this error:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

Usually I can figure out what the hell its talking about but I'm not sure on this one.

Code
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

string getPath();
int wordsPerLine(ifstream inFile, int &lines);

int main()
{
    int words,
        lines,
        avgPerLine;

    string filePath;

    ifstream inFile;

    filePath = getPath();

    inFile.open(filePath.c_str());

    if (!inFile.is_open())
    {
        cerr << "Cannot open file: " << filePath << endl << endl;
        exit (3);
    }

    words = wordsPerLine(inFile, lines);

    avgPerLine = words / lines;

    cout << "File Path: " << filePath
        << "\nNumber of words in file: " << words
        << "\nNumber of lines in file: " << lines
        << "\nAverage number of words per line: " << avgPerLine << endl;
   
    return 0;
}

string getPath()
{
    string path;

    cout << "Enter full path of text file to be read: ";

    cin >> path;

    return path;
}

int wordsPerLine(ifstream inFile, int &lines)
{
    int words = 0;
   
    char ch;

    lines = 0;

    while (inFile.get(ch))
    {
        if (!(ch == (' ' || '\t' || '\n')))
        {
            words++;
            inFile.ignore(100, (' ' || '\t' || '\n'));
        }
        else if (ch == '\n')
            lines++;
    }

    return words;
}




None.

Oct 25 2007, 10:04 pm Kellimus Post #2



Quote from Vhaeraun
Basically this program is used to count the number of words in a file and also average the number of words per line. However I get this error:

error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'

Usually I can figure out what the hell its talking about but I'm not sure on this one.

Code
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

string getPath();
int wordsPerLine(ifstream inFile, int &lines);

int main()
{
    int words,
        lines,
        avgPerLine;

    string filePath;

    ifstream inFile;

    filePath = getPath();

    inFile.open(filePath.c_str());

    if (!inFile.is_open())
    {
        cerr << "Cannot open file: " << filePath << endl << endl;
        exit (3);
    }

    words = wordsPerLine(inFile, lines);

    avgPerLine = words / lines;

    cout << "File Path: " << filePath
        << "\nNumber of words in file: " << words
        << "\nNumber of lines in file: " << lines
        << "\nAverage number of words per line: " << avgPerLine << endl;
   
    return 0;
}

string getPath()
{
    string path;

    cout << "Enter full path of text file to be read: ";

    cin >> path;

    return path;
}

int wordsPerLine(ifstream inFile, int &lines)
{
    int words = 0;
   
    char ch;

    lines = 0;

    while (inFile.get(ch))
    {
        if (!(ch == (' ' || '\t' || '\n')))
        {
            words++;
            inFile.ignore(100, (' ' || '\t' || '\n'));
        }
        else if (ch == '\n')
            lines++;
    }

    return words;
}

Well, std::basic_ios<_Elem,_Traits>::basic_ios sounds like its a class within the Standard Namespace. It also seems like there is a "private" section of this class, and it can't access a function within there [/endstateobvious]

basic_ios sounds like Basic Input/Output Stream (Kind of like iostream.h), so it must have something to do with file access maybe?

<_Elem,_Traits> kind of sounds like a preset Global Variable (because in PHP, preset Global Variables are surrounded by _ _ s, like: __FILE__)...

Did you make that class? Cause if so, should there be a , after Elem? *Shrugs*

I'm not as advanced as you, so I don't know if I'm much help, except for ideas that I can provide... Maybe go through std namespace and check out the code?



None.

Oct 26 2007, 5:54 am l3lack-l3ahamut Post #3



Quote from Kellimus
Well, std::basic_ios&lt;_Elem,_Traits&gt;::basic_ios sounds like its a class within the Standard Namespace. It also seems like there is a "private" section of this class, and it can't access a function within there [/endstateobvious]

basic_ios sounds like Basic Input/Output Stream (Kind of like iostream.h), so it must have something to do with file access maybe?

&lt;_Elem,_Traits&gt; kind of sounds like a preset Global Variable (because in PHP, preset Global Variables are surrounded by _ _ s, like: __FILE__)...

Did you make that class? Cause if so, should there be a , after Elem? *Shrugs*

I'm not as advanced as you, so I don't know if I'm much help, except for ideas that I can provide... Maybe go through std namespace and check out the code?

What I do know is that the linker or whatever it was adds the _ before stuff so that is probably why is says _Elem, _Traits



None.

Oct 26 2007, 8:33 am ShadowFlare Post #4



You are trying to access a private member of the class; it is as simple as that. The compiler won't allow you to do that. You must be trying to access a part of the class that you aren't supposed to.



None.

Oct 26 2007, 8:32 pm Kellimus Post #5



Quote from ShadowFlare
You are trying to access a private member of the class; it is as simple as that. The compiler won't allow you to do that. You must be trying to access a part of the class that you aren't supposed to.

Hey, good job at stating the obvious :)

He wants to know how to fix it, not be told the obvious...



None.

Oct 26 2007, 11:31 pm Esponeo Post #6



Input/Output streams must be passed by reference.

Also, your code sucks.



None.

Oct 26 2007, 11:36 pm Akar Post #7



Kellimus arguing with ShadowFlare? This'll be good. ShadowFlare pwnzorz in coding.



None.

Oct 30 2007, 6:44 pm Twitch Post #8



Quote from Akar
Kellimus arguing with ShadowFlare? This'll be good. ShadowFlare pwnzorz in coding.
Like I said soon he will run into a brick wall.In this case its gonna hurt big time ;).



None.

Oct 30 2007, 11:10 pm Kellimus Post #9



Quote from Akar
Kellimus arguing with ShadowFlare? This'll be good. ShadowFlare pwnzorz in coding.

Wow, Akar. This post has set in stone, the mentality that you have.

Pointing out to someone that someone is asking for advice not for the obvious, isn't arguing with them.

If I was to argue with him, I'd say that he's wrong. All he did, was say what has already been pointed out.

Commenting != Arguing

Quote from Twitch
Quote from Akar
Kellimus arguing with ShadowFlare? This'll be good. ShadowFlare pwnzorz in coding.
Like I said soon he will run into a brick wall.In this case its gonna hurt big time ;).

Twitch, I think you've ran into too many already.

Commenting != Arguing



None.

Nov 9 2007, 12:10 am l3lack-l3ahamut Post #10



I just had another question not related to this specific bit of code but I'm getting these errors while linking and I'm not really sure what it means by unresolved?

Code
1>lab08.obj : error LNK2019: unresolved external symbol "void __cdecl writeFile(class std::basic_ofstream<char,struct std::char_traits<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,int * const,int * const,int * const,double * const)" (?writeFile@@YAXAAV?$basic_ofstream@DU?$char_traits@D@std@@@std@@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@2@QAV32@2QAH33QAN@Z) referenced in function _main



Code
1>lab08.obj : error LNK2019: unresolved external symbol "void __cdecl getData(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > * const,int * const,int * const,class std::basic_ifstream<char,struct std::char_traits<char> > &)" (?getData@@YAXAAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@QAV12@1QAH2AAV?$basic_ifstream@DU?$char_traits@D@std@@@2@@Z) referenced in function _main


Code
1>E:\School\CS121 Kuroski\Labs\lab08\Debug\lab08.exe : fatal error LNK1120: 2 unresolved externals




None.

Nov 9 2007, 1:32 am Esponeo Post #11



That means it doesn't know what the fuck writeFile is. Did you forget an include, a prototype, or maybe a scope resolution operator?



None.

Options
  Back to forum
Please log in to reply to this topic or to report it.
Members in this topic: None.
[10:41 am]
v9bettel -- Nice
[01:39 am]
Ultraviolet -- no u elky skeleton guy, I'll use em better
[10:50 pm]
Vrael -- Ultraviolet
Ultraviolet shouted: How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
hey cut it out I'm getting all the minerals
[10:11 pm]
Ultraviolet -- :P
[10:11 pm]
Ultraviolet -- How about you all send me your minerals instead of washing them into the gambling void? I'm saving up for a new name color and/or glow
[2024-4-17. : 11:50 pm]
O)FaRTy1billion[MM] -- nice, now i have more than enough
[2024-4-17. : 11:49 pm]
O)FaRTy1billion[MM] -- if i don't gamble them away first
[2024-4-17. : 11:49 pm]
O)FaRTy1billion[MM] -- o, due to a donation i now have enough minerals to send you minerals
[2024-4-17. : 3:26 am]
O)FaRTy1billion[MM] -- i have to ask for minerals first tho cuz i don't have enough to send
[2024-4-17. : 1:53 am]
Vrael -- bet u'll ask for my minerals first and then just send me some lousy vespene gas instead
Please log in to shout.


Members Online: Roy, Excalibur