Results 1 to 2 of 2

Thread: Variable not declared in this scope error

  1. #1
    Join Date
    Dec 2010
    Posts
    23
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Windows

    Default Variable not declared in this scope error

    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.

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. ui->pushButton_2->hide();
    7. ui->pushButton_3->hide();
    8. ui->lineEdit->setReadOnly(true);
    9. ui->textEdit->setReadOnly(true);
    10. QString oldName;
    11. QString oldAddress;
    12. }
    13.  
    14. MainWindow::~MainWindow()
    15. {
    16. delete ui;
    17. }
    18.  
    19. void MainWindow::add()
    20. {
    21. ui->pushButton_2->show();
    22. ui->pushButton_3->show();
    23. ui->pushButton->setEnabled(false);
    24. ui->lineEdit->setReadOnly(false);
    25. ui->textEdit->setReadOnly(false);
    26.  
    27. oldName = ui->lineEdit->text();
    28. oldAddress = ui->textEdit->toPlainText();
    29. }
    To copy to clipboard, switch view to plain text mode 

    Thank you for your help in advance

  2. #2
    Join Date
    Nov 2010
    Posts
    97
    Thanks
    6
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Variable not declared in this scope error

    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. { // ----------------------------------------------- constructor's function scope begins (has access to param names and class member names)
    5. ui->setupUi(this); // use of class scope names, their members,.....and keywords.
    6. ui->pushButton_2->hide();
    7. ui->pushButton_3->hide();
    8. ui->lineEdit->setReadOnly(true);
    9. ui->textEdit->setReadOnly(true);
    10. QString oldName; // new name declared in this scope.
    11. QString oldAddress; // new name declared in this scope.
    12. } // --------------------------------- 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)
    13.  
    14. void MainWindow::add()
    15. { // ------------------------------------ add() function scope begins. Has access to class level names (nothing specific to the constructor's scope).
    16. ui->pushButton_2->show();
    17. ui->pushButton_3->show();
    18. ui->pushButton->setEnabled(false);
    19. ui->lineEdit->setReadOnly(false);
    20. ui->textEdit->setReadOnly(false);
    21.  
    22. oldName = ui->lineEdit->text(); // attempted use of variable named 'oldName' where be it?
    23. oldAddress = ui->textEdit->toPlainText();
    24. } // ---------------------------------add() scope ends.
    To copy to clipboard, switch view to plain text mode 
    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.
    Last edited by nroberts; 30th December 2010 at 00:34.
    This rude guy who doesn't want you to answer his questions.

    Note: An "expert" here is just someone that's posted a lot.

    "The fact of where you do the encapsulation is meaningless." - Qt Certified Developer and forum moderator

Similar Threads

  1. Replies: 10
    Last Post: 22nd September 2010, 06:20
  2. Replies: 2
    Last Post: 16th July 2010, 07:17
  3. glutSolidSphere was not declared in this scope error
    By nuts_fever_007 in forum Qt Programming
    Replies: 2
    Last Post: 15th May 2009, 04:56
  4. error variable not declared
    By aj2903 in forum Qt Programming
    Replies: 1
    Last Post: 3rd December 2008, 15:58
  5. error: 'connect' was not declared in this scope ??
    By Morea in forum Qt Programming
    Replies: 2
    Last Post: 14th January 2007, 15:27

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.