Calling a user-defined function in C++

This is a discussion on Calling a user-defined function in C++ within the C++ Programming forums, part of the General Programming Boards category; I am trying to call a function that I created and placed in a .h file. When the function is ...

  1. #1
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66

    Calling a user-defined function in C++

    I am trying to call a function that I created and placed in a .h file. When the function is in my program, it works great. However, when I place the function in the .h file and call to it, I get "undefined reference" errors in the compile. I'm not certain of the syntax for defining a function in the .h file, which is probably the problem.

    I am looking for an example of a function call to a user-defined function and .h file.

    I am using TextPad to write the code and GNU to complile on a 2000 machine.

    Attached is a zip file with the program (cgrade.cpp), the .h file (myfuncts.h) and the cpp file for the .h file (myfuncts.cpp).

    Any help or suggestions would be greatly appreciated.

    Thanks,

    Brian
    Attached Files Attached Files

  2. #2
    RoD
    RoD is offline
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    example, in example.h u have callme () function.

    so in your main .cpp:

    #include <example.h>


    in the code where u want it


    callme ();


    Thats what i do, in VC++

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,708
    replace the top of your program file with:


    #include <iostream>
    #include "myfuncts.h"
    #include "myfuncts.cpp"
    #include <string>

    //double weighted_avg(double, double, double);
    //string letter_grade(double);

    The reason is that unless you compile "myfuncts.cpp" and then include the ".o" file in the build, you won't be able to reference the functions. Also, you can put the functions in the ".h" file instead, if you'd like.
    Code:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 0;
    }

  4. #4
    Registered User brianptodd's Avatar
    Join Date
    Oct 2002
    Posts
    66
    Sebastiani,

    Thank you very much. It seems like such a simple solution now, but thank you. It's working just fine now.

    Brian

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  2. Change this program so it uses function??
    By stormfront in forum C Programming
    Replies: 8
    Last Post: 11-01-2005, 07:55 AM
  3. errors initializing D3D
    By Vacation Guy in forum C++ Programming
    Replies: 3
    Last Post: 08-07-2005, 12:20 PM
  4. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 10:03 AM
  5. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21