Simple Question About Extern

This is a discussion on Simple Question About Extern within the C++ Programming forums, part of the General Programming Boards category; Hi, I'm trying to use extern for the first time, and it's not working. Here's the code: test1.cpp: ------------------------------------ #include ...

  1. #1
    Unregistered
    Guest

    Question Simple Question About Extern

    Hi, I'm trying to use extern for the first time, and it's not working. Here's the code:

    test1.cpp:
    ------------------------------------
    #include "test2.cpp"

    int main(void)
    {
    hi();

    return 0;
    }
    ------------------------------------



    and test2.cpp
    ------------------------------------
    #include<iostream.h>

    extern void hi(void)
    {
    cout << "hi" << endl;
    }
    ------------------------------------

    Thanks!

  2. #2
    Unregistered
    Guest

    extern

    I have never seen extern used with functions. Usually, if you have a function declared in another file, you just use a prototype and thats the end of it. extern is usually when you have a variable declared in another file, but you want to use it somewhere else. For example:

    test.cpp

    void hello(void); // prototype

    int my_extern_int = 0;

    int main()
    {
    my_extern_int++;

    hello();

    return 0;
    }

    // test2.cpp

    extern int my_extern_int; // now we can use it in this file too

    void hello(void)
    {
    my_extern_int--;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Simple class question
    By 99atlantic in forum C++ Programming
    Replies: 6
    Last Post: 04-21-2005, 12:41 AM
  2. Simple question about pausing program
    By Noid in forum C Programming
    Replies: 14
    Last Post: 04-02-2005, 09:46 AM
  3. simple question.
    By InvariantLoop in forum Windows Programming
    Replies: 4
    Last Post: 01-31-2005, 12:15 PM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  5. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM

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