PDA

View Full Version : endl' was not declared in this scope



k.qasempour
20th June 2012, 06:07
whats the problem here... it makes me angryy!!!!
i face with this error

" 'endl' was not declared in this scope"

here is the code



#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QFile>
#include <iostream>

MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
}

MainWindow::~MainWindow()
{
delete ui;
}

void MainWindow::on_pushButton_clicked()
{

QFile myFile("c://Victory.txt");
myFile.open(QIODevice::Append);
QDataStream ds(&myFile);

ds<<"bbbb/n"<<endl;

myFile.close();

}

ChrisW67
20th June 2012, 06:55
Please use
and 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).

Ali Reza
20th June 2012, 10:41
use "\n" (carriage return)
for Ex:

ds<<"bbbb\n";

sonulohani
20th June 2012, 12:08
mention this after the header file declaration:---->



using namespace std;

ChrisW67
21st June 2012, 00:29
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):


\0 \0 \0 \006 b b b b \n \0
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.

Ali Reza
21st June 2012, 08:50
@ 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

wysota
21st June 2012, 08:58
@ 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.

ChrisW67
21st June 2012, 09:21
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:


QFile f("out.txt");
if (f.open(QIODevice::WriteOnly | QIODevice::Text) {
}
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".

Ali Reza
21st June 2012, 09:36
yeah i got it
thanks very much