Results 1 to 9 of 9

Thread: endl' was not declared in this scope

  1. #1
    Join Date
    May 2012
    Posts
    33
    Thanks
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default endl' was not declared in this scope

    whats the problem here... it makes me angryy!!!!
    i face with this error

    " 'endl' was not declared in this scope"

    here is the code


    Qt Code:
    1. #include "mainwindow.h"
    2. #include "ui_mainwindow.h"
    3. #include <QFile>
    4. #include <iostream>
    5.  
    6. MainWindow::MainWindow(QWidget *parent) :
    7. QMainWindow(parent),
    8. ui(new Ui::MainWindow)
    9. {
    10. ui->setupUi(this);
    11. }
    12.  
    13. MainWindow::~MainWindow()
    14. {
    15. delete ui;
    16. }
    17.  
    18. void MainWindow::on_pushButton_clicked()
    19. {
    20.  
    21. QFile myFile("c://Victory.txt");
    22. myFile.open(QIODevice::Append);
    23. QDataStream ds(&myFile);
    24.  
    25. ds<<"bbbb/n"<<endl;
    26.  
    27. myFile.close();
    28.  
    29. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 20th June 2012 at 11:20. Reason: missing [code] tags

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: endl' was not declared in this scope

    Please use [code] and [/code] tags around your code in future. You could even edit the post above to add them.

    The error message is exactly what it says, the name "endl" has not been declared. If you meant std::endl then you have to say so (or use the using keyword). This is basic C++ and nothing at all to do with Qt.

    It won't compile either way because there is no QDataStream operator<<() suitable for std::endl. Even if it did compile I suspect it would not do what you are expecting because QDataStream is not a good mechanism for writing a text file. Perhaps you wanted QTextStream, which does have an associated endl() function (unrelated to std::endl).

  3. #3
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: endl' was not declared in this scope

    use "\n" (carriage return)
    for Ex:
    Qt Code:
    1. ds<<"bbbb\n";
    To copy to clipboard, switch view to plain text mode 
    Life is about making the right decisions and moving on.

  4. #4
    Join Date
    May 2012
    Location
    Bangalore, India
    Posts
    271
    Thanks
    29
    Thanked 50 Times in 47 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: endl' was not declared in this scope

    mention this after the header file declaration:---->

    Qt Code:
    1. using namespace std;
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: endl' was not declared in this scope

    Ali Reza, while doing as you suggest (and dropping the std::endl) will make the code compile, it will still not do what the OP expects it to do. QDataStream is the wrong tool for the job of writing a text file... the output file will have these bytes written (using C-style escapes):
    Qt Code:
    1. \0 \0 \0 \006 b b b b \n \0
    To copy to clipboard, switch view to plain text mode 
    That is, it is a ten-byte binary file rather than a five-byte (or six on Windows) text file.

    Sonulohani, this will not help. The code will still fail to compile, just for different reasons.

  6. #6
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: endl' was not declared in this scope

    @ Chris
    i don't understand your mean.
    '\n' take 1 byte of a file, why this wrong??
    '\n' is a ASCII code and all ASCII codes take 1 byte (7 bit data and 1 bit zero)
    if those is not correct tell me why ??
    thanks dear Chris
    Life is about making the right decisions and moving on.

  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: endl' was not declared in this scope

    Quote Originally Posted by Ali Reza View Post
    @ Chris
    i don't understand your mean.
    '\n' take 1 byte of a file, why this wrong??
    '\n' is a ASCII code and all ASCII codes take 1 byte (7 bit data and 1 bit zero)
    if those is not correct tell me why ??
    Why don't you just try your code and see how many bytes the file generated with it has.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: endl' was not declared in this scope

    The character '\n' is a single byte, correct. I didn't say it wasn't.

    This operation is not on single characters, it is on the nul-terminated C-string "bbbb\n" with the intent of writing text to a file. In memory that string occupies 6 bytes, the 6th being a nul terminator ('\0').

    When a string of characters (i.e const char*) is streamed to a QDataStream the result is preceded by the length of the string, including the nul terminating byte, as a 32-bit integer then the bytes of the string including the nul terminator. You end up with 4 + 6 = 10 bytes in the file, some of which are non-printable. See my last post for the ten bytes in detail.

    The original poster almost certainly wants a plain text file, and QDataStream does not provide that. QTextStream, as the name suggests, is for manipulating text streams. When streamed to a QTextStream the string "bbbb\n" will result in the expected 5 or 6 bytes in the text file. If the underlying file is opened with the QIODevice::Text option:
    Qt Code:
    1. QFile f("out.txt");
    2. if (f.open(QIODevice::WriteOnly | QIODevice::Text) {
    3. }
    To copy to clipboard, switch view to plain text mode 
    then line-ending conversions may happen just as with the standard C/C++ library. On Windows machines the single '\n' in the original string will be output as the two character pair "\r\n" leading to a six byte file "bbbb\r\n".

  9. #9
    Join Date
    Jun 2012
    Location
    Iran , Tehran
    Posts
    93
    Thanks
    5
    Platforms
    Unix/X11 Windows Android

    Default Re: endl' was not declared in this scope

    yeah i got it
    thanks very much
    Life is about making the right decisions and moving on.

Similar Threads

  1. glTexImage3D is not declared in the scope.
    By Elberion in forum Newbie
    Replies: 6
    Last Post: 26th June 2011, 08:15
  2. ui not declared in scope
    By steve.bush in forum Newbie
    Replies: 5
    Last Post: 19th March 2011, 09:58
  3. `lineEdit' was not declared in this scope?
    By i4ba1 in forum Qt Programming
    Replies: 2
    Last Post: 18th November 2009, 15:36
  4. QDomDocument was not declared in this scope
    By grantbj74 in forum Newbie
    Replies: 5
    Last Post: 25th August 2009, 10:43
  5. Replies: 2
    Last Post: 28th December 2007, 19:30

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.