Using ofstream in private of a class-errors

This is a discussion on Using ofstream in private of a class-errors within the C++ Programming forums, part of the General Programming Boards category; Hi! Trying to use ofstream object like that: Code: class X { ofstream FileHandle; int f(); }; int X::f() { ...

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    104

    Using ofstream in private of a class-errors

    Hi!

    Trying to use ofstream object like that:

    Code:
    class X 
    { 
       ofstream FileHandle; 
       int f(); 
    }; 
    
    int X::f() 
    { 
       FileHandle.open("c:\1.txt",ios::binary); 
       return 0; 
    }
    Now, this produces errors. Anyone has ANY idea why I can't use ofstream there? I read that I should put "static" before "ofstream" but it didn't work. I tried using std:fstream, too, but it too didn't work. I tried using pointer:
    ofstream *FileHandle;
    ....
    FileHandle = new ofstream;
    FileHandle.open(...);

    But this didn't work also.

    I forgot to mention:
    There is no error if I don't use any member functions of the ofstream class... i.e. if I do not use the FileHandle.open() function.
    The errror comes when I try to use the open() function. The error is:

    error LNK2001: unresolved external symbol "private: static class ofstream waveIn::FileHandle" (?FileHandle@waveIn@@0Vofstream@@A)
    Ilia Yordanov,
    http://www.cpp-home.com ; C++ Resources

  2. #2
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    Your ofstream variable should be static only if you want it to be static. That means you only want one single version of the ofstream for all instances of your class. If you do want it to be static, then you have to instantiate it once outside the class declaration. That is likely why you got the error you displayed (even though that error doesn't correspond to the code you posted). If you don't want it to be static, then show the error you are getting when it is not static (e.g. the errors you get with the posted code).

    A couple of other notes that I don't think are causing your problems:
    1. "c:\1.txt" should be "c:\\1.txt". To put the '\\' character into a string you must use an escape sequence, in this case, "\\".
    2. When you tried it as a pointer, you should have used FileHandle->open(...);
    3. In your small example, your function is private, so any code outside the class wouldn't be able to call it.
    Try building this simple code by itself and see if you still get errors. One class uses a static ofstream, the other doesn't.
    Code:
    #include <fstream>
    using namespace std;
    
    class StaticX
    {
    public:
       static ofstream FileHandle; 
       int f(); 
    }; 
    
    ofstream StaticX::FileHandle;
    int StaticX::f() 
    { 
       FileHandle.open("c:\\1.txt",ios::binary); 
       return 0; 
    } 
    
    class NonStaticX 
    {
    public:
       ofstream FileHandle; 
       int f(); 
    }; 
    
    int NonStaticX::f() 
    { 
       FileHandle.open("c:\\1.txt",ios::binary); 
       return 0; 
    } 
    
    int main()
    {
        StaticX myStaticX;
        myStaticX.f();
        NonStaticX myNonStaticX;
        myNonStaticX.f();
    }

  3. #3
    Disturbed Boy gustavosserra's Avatar
    Join Date
    Apr 2003
    Posts
    244
    I do not like to post if I won´t help, but in this case I would like to remember to post the error message.
    If you do want it to be static, then you have to instantiate it once outside the class declaration
    If I understood well, I think that it is not necessary in some cases. You could use a static for an inline function, for example.
    But you must declare it, not define it, in the files the use that static variable others than the file that has the class.
    Sorry any english mistakes.
    Last edited by gustavosserra; 12-13-2003 at 09:10 PM.
    Nothing more to tell about me...
    Happy day =)

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    do you mean std:fstream? Indeed, what are the errors? Everything in a class is private by defautl do you want to make them public?

    jliou: are you sure he wants this to be static?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can Nested class access parent class's private member?
    By meili100 in forum C++ Programming
    Replies: 4
    Last Post: 06-05-2009, 09:42 AM
  2. Replies: 8
    Last Post: 12-02-2008, 12:45 PM
  3. Linking errors with static var
    By Elysia in forum C++ Programming
    Replies: 8
    Last Post: 10-27-2007, 06:24 PM
  4. class errors
    By romeoz in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 08:57 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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