Thread: Newbie Header Question (OS X)

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    9

    Newbie Header Question (OS X)

    I've been working on a very simple exercise meant to show how to use header files. The program takes a user specified range of temperatures and displays a three column table of the tempuratures converted into two different forms, the functions that actually do the conversion are stored in a header file , I am programming using Pico through the terminal in OS X and compiling using g++. Here is the code for my header (convert.h):

    Code:
    #ifndef CONVERT_H
    #define CONVERT_H
    
    double celsius_of(int fahr);
    double ab_value_of(int fahr);
    
    #endif
    The code for the headers cpp file. "convert.cpp"

    Code:
    #include<iostream>
    #include"convert.h"
    
    using namespace std;
    
    double celsius_of(int fahr)
    {
    return (static_cast<double>(5)/9) * (fahr - 32);
    }
    
    double ab_value_of(int fahr)
    {
    return ((static_cast<double>(5)/9) * (fahr - 32)) + 273.15;
    }
    And the code for the main program:

    Code:
    #include <iostream>
    #include <iomanip>
    #include"convert.h"
    
    using namespace std;
    
    void print_prelim_message()
    {
    cout << "This program outputs a list of temperatures converted into various forms" << endl;
    }
    
    void print_message_echoing_input(int lower,int upper,int step)
    {
    cout << "A chart of temperatures between " << lower << " and " << upper << " in the increment of " << step << " is as follows:" << endl;
    }
    
    int print_table(int lower,int upper,int step)
    {
    cout << setw(10) << "Fahrenheit" << setw(10) << "Celsius" << setw(10) << "Kelvin" << endl;
    for (; lower <= upper; lower = lower+step)
    {
    cout << setw(10) << lower << setw(10) << celsius_of(lower) << setw(10) << ab_value_of(lower) << endl ;
    }
    }
    
    
    
    int main ()
    {
    
    int lower = 0;
    int upper = 0;
    int step = 1;
    
    print_prelim_message();
    
    cout << "Please enter a minimum temp." << endl;
    cin >> lower;
    cout << "Please enter a maximum temp." << endl;
    cin >> upper;
    cout << "Please enter an increment." << endl;
    cin >> step;
    
    print_message_echoing_input(lower, upper, step);
    print_table(lower, upper, step);
    return 0;
    }
    I've gone over the syntax about a million times, yet when I try to compile it g++ spits out the following error:

    /usr/bin/ld: Undefined Symbols:
    celsius_of(int)
    ab_value_of(int)
    collect2: ld returned 1 exit status

    I have no idea what's going wrong.

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    > using namespace std;
    You need to not use this anymore (at least not in global scope). As soon as you start splitting up files, this becomes difficult to use correctly and causes trouble for the linker, because things that don't belong to that namespace can become implicitly resolved with std::

  3. #3
    carry on JaWiB's Avatar
    Join Date
    Feb 2003
    Location
    Seattle, WA
    Posts
    1,972
    How are you calling g++? Something like this should work:
    Code:
    g++ main.cpp convert.cpp -o convert
    "Think not but that I know these things; or think
    I know them not: not therefore am I short
    Of knowing what I ought."
    -John Milton, Paradise Regained (1671)

    "Work hard and it might happen."
    -XSquared

  4. #4
    Registered User
    Join Date
    Jul 2006
    Posts
    9

    Aha

    Thanks for the help, turns out the problem was in the way I was using g++.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Total Newbie Question
    By Kid A in forum C++ Programming
    Replies: 4
    Last Post: 06-22-2006, 05:36 AM
  2. Newbie Question - fflush(stdin) & fpurge(stdin) on Mac and PC
    By tvsinesperanto in forum C Programming
    Replies: 34
    Last Post: 03-11-2006, 12:13 PM
  3. Novice question on header files
    By hern in forum C++ Programming
    Replies: 1
    Last Post: 07-30-2005, 10:11 AM
  4. C header file question?
    By correlcj in forum C Programming
    Replies: 3
    Last Post: 08-13-2002, 07:59 AM
  5. global variable and header question
    By Unregistered in forum C++ Programming
    Replies: 2
    Last Post: 08-05-2002, 11:38 PM