Thread: Advanced: Calculate square root with templates

  1. #16
    Registered User
    Join Date
    Aug 2003
    Posts
    470
    I'm going to send you an improved version.

  2. #17
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Please post in this thread if you're participating!
    I am participating. I emailed you my submission, sangdrax.

    Question to the rest of you who have successfully completed the task: what compiler are you using? I cannot compile with Borland5.5, and the value works in a static array in MSVC++v6 only with a particular change to the struct.

  3. #18
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    I used VC++ 2003 .NET
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  4. #19
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I used the IDE for VC++.NET but I've updated the compiler with the free download for VC++.NET 2003.

    The other compilers before that choke on most template stuff.

    Other template stuff I have done in the past has been on Codewarrior or MingW

    Take a peek over here - http://cboard.cprogramming.com/showthread.php?t=56548. That's a free VC++ compiler that can handle templates pretty well

  5. #20
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Thanks for the link Fordy. Unfortunately I can't give it a go here at work because XP is the min OS. Sounds promising, though.

  6. #21
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    I'm on 2K and works for me

  7. #22
    Anti-Poster
    Join Date
    Feb 2002
    Posts
    1,401
    Submitted an entry.
    If I did your homework for you, then you might pass your class without learning how to write a program like this. Then you might graduate and get your degree without learning how to write a program like this. You might become a professional programmer without knowing how to write a program like this. Someday you might work on a project with me without knowing how to write a program like this. Then I would have to do you serious bodily harm. - Jack Klein

  8. #23
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Quote Originally Posted by Fordy
    I'm on 2K and works for me
    Not to split hairs, but yeah, that's what I meant. XP is the min OS for the "toolkit" and at work I've a mere 98 box...

  9. #24
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    1# First I don't get this:
    Code:
    char data[ SquareRoot<16>::v ];
    If I do something trivial like
    Code:
    template<int x> const int SquareRoot<x>::v = 2;
    I still get compiler errors, like:
    error C2057: expected constant expression
    error C2466: cannot allocate an array of constant size 0
    error C2133: 'data' : unknown size
    The template is decided at compile time... Why not the static variable?

  10. #25
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    Are you able to compile this?
    Code:
    template <int n>
    struct Test
    {
      static const int v = n;
    };
    
    char data[ Test<16>::v ];
    If not, try the link Fordy provided and download a better compiler.
    Quote Originally Posted by pianorain
    Submitted an entry.
    I haven't received anything.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  11. #26
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    I had the same problem and had to resort to change v to an enumerated type (using MSVC6).

  12. #27
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Quote Originally Posted by Sang-drax
    If not, try the link Fordy provided and download a better compiler.
    I used both the .NET compiler and my MSVC++6 compiler.. both gave me the same result...
    [edit]
    worked fine now.. probably distraction of mine
    Last edited by xErath; 09-26-2004 at 08:11 PM.

  13. #28
    Registered User
    Join Date
    Jun 2004
    Posts
    722
    Solution sent and it works perfectly!

  14. #29
    S Sang-drax's Avatar
    Join Date
    May 2002
    Location
    Göteborg, Sweden
    Posts
    2,072
    OK, I've lost every contest entry I've receieved so far.

    The reason is this:
    http://cboard.cprogramming.com/showthread.php?t=57298

    Preliminary, this contest is closed for more submissions.

    I'm unable to recive and judge more entries (I have no compiler). I know who is the winner though. If it's OK with you, I'll announce the winner and every contestant can post the submission they used.

    I've seen your updated submission xErath, but not compiled it. I'm not going to use it when I judge the competition, sorry.
    Last edited by Sang-drax; 09-29-2004 at 02:37 AM.
    Last edited by Sang-drax : Tomorrow at 02:21 AM. Reason: Time travelling

  15. #30
    &TH of undefined behavior Fordy's Avatar
    Join Date
    Aug 2001
    Posts
    5,793
    Wow that's nasty! You must be gutted...

    I'm OK with this, but I'll have to post mine tomorrow as I'm in work now and I'm not home tonight

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. program to calculate the square root
    By saszew in forum C Programming
    Replies: 7
    Last Post: 10-28-2008, 12:53 PM
  2. Forced moves trouble!!
    By Zishaan in forum Game Programming
    Replies: 0
    Last Post: 03-27-2007, 06:57 PM
  3. Bisection Method function value at root incorrect
    By mr_glass in forum C Programming
    Replies: 3
    Last Post: 11-10-2005, 09:10 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Templated Binary Tree... dear god...
    By Nakeerb in forum C++ Programming
    Replies: 15
    Last Post: 01-17-2003, 02:24 AM