Thread: doubt in c++ code, urgent please

  1. #16
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    Quote Originally Posted by jimblumberg View Post
    So you get an error message, does any of your choices include an error message?



    If you get an error message can a, b, or c ever happen? Remember when you get an error message your program will not run, therefor it produces no output.

    Jim
    i believe here that static member type const double can't have in calss initializer...correct me if am wrong? so i feel answer is a?

  2. #17
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    static const double d = 2.0; isn't valid C++. it shouldn't compile at all. what do you think guys?

  3. #18
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    Quit guessing! Look at your options.

    a) x
    b) d
    c) x and d
    d) none

    Re-read and answer the question from my last post.

    Jim

  4. #19
    Registered User
    Join Date
    Feb 2003
    Posts
    596
    Read your error message.

    Why do you think that static const double d = 2.0; isn't valid? d = 2.0 is a constant expression so it's allowed in the class definition.
    [edit: forgot that this is a g++ extension. Only integral values allowed in ISO C++]


    What you can't do in the class definition is to use a function call to initialize a member, as in static const int B::x = fn();. But you can initialize that data member outside the class definition like this:
    Code:
    int fn () {return 5;}
    class B {
    private:
             static const int x;
             static const double d = 2.0;
    };
    const int B::x = fn();
    int main ()
    {
    }
    Last edited by R.Stiltskin; 03-12-2013 at 11:46 PM.

  5. #20
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    If I google "static const double initializer", the first hit is this: c++ - Why can't I have a non-integral static const member in a class? - Stack Overflow
    You should try searching for answers to questions that you have.

    If you're using an online compiler like Comeau you can get compiler error messages, but you won't be able to run the resulting code, which is really a barrier to learning. You can download environments like Code::Blocks or Cygwin or MinGW that provide the gcc compiler on Windows, if you're more familiar with gcc from Linux.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #21
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    so do you mean answer is "none" here?

  7. #22
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    What do you think and why?

    Jim

  8. #23
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    i am not sure jim, please advice me to find correct answer here.

  9. #24
    Registered User
    Join Date
    May 2010
    Posts
    4,465
    Your choices are:
    a) x
    b) d
    c) x and d
    d) none

    Of these four choices what do you think is the answer? Remember this is your error message!

    while running with online compiler, i got below error...pls help

    Line 1: error: 'int fn()' cannot appear in a constant-expression
    compilation terminated due to -Wfatal-errors.

    Then answer this question I asked in a previous post!

    If you get an error message can a, b, or c ever happen? Remember when you get an error message your program will not run, therefor it produces no output.

    The answer to your question should be obvious by now! This is not a trick question. If you want to be a programmer you need to learn to think and reason.

    Jim

  10. #25
    Registered User
    Join Date
    Nov 2008
    Posts
    177
    Quote Originally Posted by jimblumberg View Post
    So you get an error message, does any of your choices include an error message?



    If you get an error message can a, b, or c ever happen? Remember when you get an error message your program will not run, therefor it produces no output.

    Jim
    Compiler Output: A8Gb1SfZ.c:4: error: 'int fn()' cannot appear in a constant-expression
    A8Gb1SfZ.c:4: error: a function call cannot appear in a constant-expression

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Urgent help please with C code
    By Clement Museka in forum C Programming
    Replies: 5
    Last Post: 06-15-2012, 03:50 AM
  2. Explain following code,doubt written in comments
    By ackr1201 in forum C Programming
    Replies: 9
    Last Post: 07-08-2011, 04:17 PM
  3. Doubt regarding C++ code in old textbooks
    By thefeedinghand in forum C++ Programming
    Replies: 4
    Last Post: 08-30-2010, 03:43 PM
  4. Code Doubt?
    By shwetha_siddu in forum C Programming
    Replies: 1
    Last Post: 07-21-2008, 01:02 AM
  5. doubt in C--(reducing the length of code)
    By nardneran in forum C Programming
    Replies: 44
    Last Post: 10-15-2006, 02:38 PM