PDA

View Full Version : Variable not declared in this scope error



hojoff79
29th December 2010, 23:58
I'm sure this is just a simple problem that I cannot find the answer to, but it is driving me crazy. I am declaring the QString variables oldName and oldAddress in my constructor function, but then when I try to use them in a function that I have in a slot, then I get the error "oldName was not declared in this scope"

Here is the relavent snippet of my code.


MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
ui->pushButton_2->hide();
ui->pushButton_3->hide();
ui->lineEdit->setReadOnly(true);
ui->textEdit->setReadOnly(true);
QString oldName;
QString oldAddress;
}

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

void MainWindow::add()
{
ui->pushButton_2->show();
ui->pushButton_3->show();
ui->pushButton->setEnabled(false);
ui->lineEdit->setReadOnly(false);
ui->textEdit->setReadOnly(false);

oldName = ui->lineEdit->text();
oldAddress = ui->textEdit->toPlainText();
}



Thank you for your help in advance

nroberts
30th December 2010, 00:29
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{ // ----------------------------------------------- constructor's function scope begins (has access to param names and class member names)
ui->setupUi(this); // use of class scope names, their members,.....and keywords.
ui->pushButton_2->hide();
ui->pushButton_3->hide();
ui->lineEdit->setReadOnly(true);
ui->textEdit->setReadOnly(true);
QString oldName; // new name declared in this scope.
QString oldAddress; // new name declared in this scope.
} // --------------------------------- constructor scope ends. Data associated with local names is destroyed (the QString destructor for oldName and oldAddress will be activated and they will be gone)

void MainWindow::add()
{ // ------------------------------------ add() function scope begins. Has access to class level names (nothing specific to the constructor's scope).
ui->pushButton_2->show();
ui->pushButton_3->show();
ui->pushButton->setEnabled(false);
ui->lineEdit->setReadOnly(false);
ui->textEdit->setReadOnly(false);

oldName = ui->lineEdit->text(); // attempted use of variable named 'oldName' where be it?
oldAddress = ui->textEdit->toPlainText();
} // ---------------------------------add() scope ends.



In C++ (and most other languages actually) there are rules about when a "name" can be reached in code. These are "scoping rules" and they say when a variable can be used and even WHICH variable is used when there are more than one with the same "name". In this case, your functions are members of a class. The code within them is able to access those names that are in "class wide scope" and anything above (globals and stuff). Every open '{' also creates a new scope.

A name declared in one scope only exists (can only be used) within that same scope and those scopes within. In your example you've declared two names in one function's scope and attempted to use them in another's. Can't do, sorry. To use the same variables in both scopes you need to declare them at a higher scope level. In your case, the best answer is almost certainly to declare them as class member variables by putting them in the "class { .... }" block.

I've skipped a LOT of details. Really knowing scoping rules (as best as possible because even experts have trouble with the Koenig lookup and such) is essential to programming in C++. I don't wish to offend, but you might not be ready for UI development yet. You should probably start more simply or you're likely to just frustrate yourself.