Menu

i have another problem with dev-c++ on vista

codor kim
2008-04-25
2012-09-26
  • codor kim

    codor kim - 2008-04-25

    hello everyone, i ran into another problem with my dev c++ on vista.

    i followed my book in the creation of this sample program with 3 files:

    1. main:

    include <cstdlib>

    include <iostream>

    include "sharealike.h"

    using namespace std;

    int main(int argc, char *argv[])
    {
    int DoubleCheeseburgers = 40;
    EatAtJoes();
    system("pause");
    return exit_success;
    }


    2. sharealike.h:

    include <iostream>

    include <cstdlib>

    using namespace std;

    extern int DoubleCheeseburgers;
    void EatAtJoes();


    3. sharealike.cpp

    include <cstdlib>

    include <iostream>

    include "sharealike.h"

    using namespace std;

    int DoubleCheeseburgers;
    void EatAtJoes() {
    cout << "how many cheeseburgers today?" << endl;
    cout << DoubleCheeseburgers << endl;
    }


    supposely when i compile and run the program i should get

    how many cheeseburgers today?
    40


    printed on the console. but instead i got

    how many cheeseburgers today?
    0


    0? what went wrong? and i can assign 20 or any number to "int DoubleCheeseburgers" in the main file and still get 0. can you guys help me?

     
    • codor kim

      codor kim - 2008-04-27

      no, the purpose of the author in this example is not to show about overriding. how then can you get the function to print the number of cheeseburgers as specified in the main?

      thanks for the reply, clif.

       
    • codor kim

      codor kim - 2008-04-25

      sorry i didn't mean to post my question twice. a mistake.

       
    • cpns

      cpns - 2008-04-26

      It is all to do with "scope". DoubleCheeseburgers in main() is local to the main function so is not visible outside of main().

      DoubleCheeseburgers in sharealike.cpp is global. It is a different variable. In main() anthough the global DoubleCheeseburgers would be visible, the compiler always uses the one with the narrowest scope when faced with two variables of the same name at different scope. So the local variable overrides (or shadows) the global one.

      If you copied this code from a book, then it is likely that the intention was exactly to demonstrate the C++ scope rules. YOu should read the text to check rather than just copy the code (if that is what you did).

      If you add the compiler option -Wshadow, the compiler will issue a warning. However while this is useful in C, it can be less useful in C++ where overriding is often intentional, so you may get warnings caused by the standard library code.

      Here is a good article on C and C++ scope regions: http://www.embedded.com/columns/programmingpointers/202600398 . It is on embedded.com but is applicable generally, not just in embedded systems.

      Clifford

       

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.