Page 1 of 4 123 ... LastLast
Results 1 to 10 of 40

Thread: Square root algorithm

  1. #1
    Join Date
    Mar 2014
    Beans
    110

    Square root algorithm

    Hello!

    I wrote function like this in text editor example was in C++ book



    double sgrt(double arg)
    {
    // code for calculating a square root
    }

    void f()
    {
    double root2 = sqrt (2) ;
    // ...
    }


    Then I typed as follows:

    Code:
     g++ Documents/squareroot.cpp
    Documents/squareroot.cpp: In function ‘void f()’:
    Documents/squareroot.cpp:8:29: error: ‘sqrt’ was not declared in this scope
    double root2 = sqrt (2) ;
    ^


    I wrote something incorrectly? Anyone could explain that function to me (I don`t fully understand it).

    Curly braces, { }, express grouping in C++. Here, they indicate the start and end of the function
    bodies. The double slash, / /, begins a comment that extends to the end of the line. The keyword
    void indicates that a function does not return a value.
    I don`t understand. Integer variable does return a value?

    Here is declared the function (told the compiler what the type of its parameters and result are, which is all it needs to know to invoke the function at run time). For this function what are the parameters and results?

  2. #2
    Join Date
    Aug 2010
    Location
    Lancs, United Kingdom
    Beans
    1,264
    Distro
    Ubuntu Mate 16.04 Xenial Xerus

    Re: Square root algorithm

    When you declare (and define) the function you spell it sgrt. When you use it, you spell it sqrt. These are not the same: g is not q.

  3. #3
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    Yep, I changed sgrt to sqrt. The is still some syntax errors in the code?

    Code:
    g++ Documents/squareroot.cpp
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 0 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 1 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 2 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 3 has invalid symbol index 2
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 4 has invalid symbol index 11
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 5 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 6 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 7 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 8 has invalid symbol index 12
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 9 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 10 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 11 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 12 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 13 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 14 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 15 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 16 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 17 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 18 has invalid symbol index 13
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_info): relocation 19 has invalid symbol index 21
    /usr/bin/ld: /usr/lib/debug/usr/lib/x86_64-linux-gnu/crt1.o(.debug_line): relocation 0 has invalid symbol index 2
    /usr/lib/gcc/x86_64-linux-gnu/4.8/../../../x86_64-linux-gnu/crt1.o: In function `_start':
    (.text+0x20): undefined reference to `main'
    collect2: error: ld returned 1 exit status

  4. #4
    Join Date
    Apr 2012
    Beans
    6,754

    Re: Square root algorithm

    As indicated by the error message

    Code:
     (.text+0x20): undefined reference to `main'
    your code doesn't have an entry point (main function) so you can't create an executable program from it as-is. You can either compile it to an object file to be linked at some later point to create an actual executable program by adding the -c switch e.g.

    Code:
    g++ -c squareroot.cpp -o squareroot.o
    or you can add a main function to your file

    Code:
    int main()
    {
        // do stuff
    
        return 0;
    }

  5. #5
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    I got error message as follows:

    g++ Documents/squareroot.cpp
    Documents/squareroot.cpp: In function ‘int main()’:
    Documents/squareroot.cpp:6:1: error: a function-definition is not allowed here before ‘{’ token
    {
    ^
    Documents/squareroot.cpp:11:1: error: a function-definition is not allowed here before ‘{’ token
    {
    ^
    Documents/squareroot.cpp:16:1: error: expected ‘}’ at end of input
    }
    ^


    My actual code looks like this:

    Code:
    int main()
    {
        //
    
    double sqrt(double arg)
    {
          // code for calculating a square root
    }
    
    void f()
    {
          double root2 = sqrt (2) ;
          // ...
          
        return 0;
    }

  6. #6
    Join Date
    Apr 2012
    Beans
    6,754

    Re: Square root algorithm

    where's the closing brace } for your main() ?

  7. #7
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    Now I have as follows:

    Code:
    int main()
    {
        //
    
    double sqrt(double arg)
    {
          // code for calculating a square root
    }
    
    void f()
    {
          double root2 = sqrt (2) ;
          // ...
    }
          
        return 0;
    }

    g++ Documents/squareroot.cpp
    Documents/squareroot.cpp: In function ‘int main()’:
    Documents/squareroot.cpp:6:1: error: a function-definition is not allowed here before ‘{’ token
    {
    ^
    Documents/squareroot.cpp:11:1: error: a function-definition is not allowed here before ‘{’ token
    {
    ^

  8. #8
    Join Date
    Apr 2012
    Beans
    6,754

    Re: Square root algorithm

    AFAIK C++ does not support function nesting - you need to define your subfunctions outside of main() { ... }

    http://en.wikipedia.org/wiki/Nested_function#Languages

  9. #9
    Join Date
    Jul 2007
    Location
    Poland
    Beans
    4,499
    Distro
    Ubuntu 14.04 Trusty Tahr

    Re: Square root algorithm

    nested functions are not C/C++ feature.
    edit too late to the party
    if your question is answered, mark the thread as [SOLVED]. Thx.
    To post code or command output, use [code] tags.
    Check your bash script here // BashFAQ // BashPitfalls

  10. #10
    Join Date
    Mar 2014
    Beans
    110

    Re: Square root algorithm

    g++ Documents/squareroot.cpp


    worked correctly after I changed the code.


    ./Documents/squareroot.cpp
    bash: ./Documents/squareroot.cpp: Permission denied

    Something wrong again??
    Last edited by spacerocket; May 1st, 2014 at 08:38 PM.

Page 1 of 4 123 ... LastLast

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
  •