Results 1 to 11 of 11

Thread: problem with pointer-reference

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default problem with pointer-reference

    Qt Code:
    1. class File {
    2. private:
    3. std::fstream file;
    4. ...............................
    5. public:
    6. std::fstream open_file(int);
    7. }
    8. std::fstream File::open_file() {
    9. file.open(file_name.c_str(), std::ios_base::in); //read only
    10. return file;
    11. }
    12.  
    13. int readFile(File& f) {
    14. std::fstream ff = f.open_file(); //EDIT
    15. if (ff) { //EDIT : if I put if (!f.open_file()) is always 0
    16. // I need check if file exists
    17. }
    To copy to clipboard, switch view to plain text mode 
    Hi this doens't LINK. anyone could explain me the error? thanks
    Last edited by mickey; 22nd February 2007 at 19:27. Reason: forget a part
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Alingsås, Sweden
    Posts
    437
    Thanks
    3
    Thanked 39 Times in 39 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with pointer-reference

    If you show us the errors we don't have to try to find them in your code. What does your compiler tell you?

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: problem with pointer-reference

    Try adding a semicolon after the class definition.

  4. #4
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with pointer-reference

    (semicolon is a cut&past error)
    Above I edited the code because there wasn't a a thing.. it isn't LINKING but it compile
    microsoft visual studio 8\vc\include\fstream(933) : error C2248: 'std::basic_ios<_Elem,_Traits>::basic_ios' : cannot access private member declared in class 'std::basic_ios<_Elem,_Traits>'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    microsoft visual studio 8\vc\include\ios(151) : see declaration of 'std::basic_ios<_Elem,_Traits>::basic_ios'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    This diagnostic occurred in the compiler generated function 'std::basic_fstream<_Elem,_Traits>::basic_fstream( const std::basic_fstream<_Elem,_Traits> &)'
    with
    [
    _Elem=char,
    _Traits=std::char_traits<char>
    ]
    Last edited by jacek; 22nd February 2007 at 19:40. Reason: changed [code] to [quote]
    Regards

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with pointer-reference

    I suggest that File::open_file returns a reference to the stream instead of a copy.

  6. #6
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with pointer-reference

    Quote Originally Posted by wysota View Post
    I suggest that File:pen_file returns a reference to the stream instead of a copy.
    why? it doens't linking............
    It works if I declare std::fstream& ff (ff as reference). Could me explain why?
    thanks
    Regards

  7. #7
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with pointer-reference

    Because you can't copy streams and you try to do that two times (first you return a copy of the stream from the open function and then you try to assign it to another object, which copies it again. You have to work on references.

  8. #8
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with pointer-reference

    Qt Code:
    1. class File {
    2. private:
    3. std::fstream file;
    4. ...............................
    5. public:
    6. std::fstream& open_file();
    7. }
    8. std::fstream& File::open_file() {
    9. file.open(file_name.c_str(), std::ios_base::in); //read only
    10. return file;
    11. }
    12.  
    13. int readFile(File& f) {
    14. std::fstream* ff = f.open_file(); // here I used '*', heere ERROR
    15. if (ff) {
    16. ........................
    17. }
    To copy to clipboard, switch view to plain text mode 
    Hi I changed it but doens't compile. Now ff isn't a copy! Compiler says that I cant convert
    fstream into fstream*. Why? Isn't f.open_file returing a 'reference'???
    Regards

  9. #9
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with pointer-reference

    Qt Code:
    1. int readFile(File& f) {
    2. std::fstream* ff = f.open_file(); // here I used '*', heere ERROR
    3. if (ff) {
    4. ........................
    5. }
    To copy to clipboard, switch view to plain text mode 

    ff is now a pointer to std::fstream and not a reference...

    Try this:
    Qt Code:
    1. std::fstream &ff = f.open_file();
    To copy to clipboard, switch view to plain text mode 

  10. #10
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: problem with pointer-reference

    hi, I just tried it and goes well but I'm finding to understand this:
    my function open_file return a reference so I do fstream* pfile = open_file() because I always do this eg: int j; int* pint = &j (your suggest for me is something like
    Qt Code:
    1. int j; int& pint = &j
    To copy to clipboard, switch view to plain text mode 
    And this above doens't compile..
    So the returned reference of open_file for me is like &j. Isn't that? or what?
    thanks...
    Regards

  11. #11
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: problem with pointer-reference

    "int &" is a "reference to int", whereas int* is a "pointer to int". These are types. Now we also have a "dereference operator" (*) which is a unary operator that returns an object pointed by its argument and an "address operator" (&) which is a unary operator that returns an address of its argument. So "&j" is something completely different than "int&". Same goes for asterisks.

    I think it's high time for you to download and read Thinking in C++.

Similar Threads

  1. how to correctly compile threads support?
    By srhlefty in forum Installation and Deployment
    Replies: 9
    Last Post: 25th June 2006, 19:15
  2. Strange error while using Q3Canvas
    By Kapil in forum Newbie
    Replies: 13
    Last Post: 15th June 2006, 19:36
  3. undefined reference to fftw libraries
    By kmyadam in forum General Programming
    Replies: 2
    Last Post: 9th March 2006, 01:01
  4. linking user space and kernel space programs with qmake
    By zielchri in forum Qt Programming
    Replies: 9
    Last Post: 8th March 2006, 23:11
  5. Problem: Undefined reference
    By stiank81 in forum Qt Programming
    Replies: 4
    Last Post: 16th February 2006, 14:17

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.