extern twist

This is a discussion on extern twist within the C++ Programming forums, part of the General Programming Boards category; Hi folks, I learned a great deal today from experts on this board; hopefully I'll meet those kind people again... ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    48

    extern twist

    Hi folks,

    I learned a great deal today from experts on this board; hopefully I'll meet those kind people again...
    Anyway, here's my program, spanning in three files whereas I am not sure how to properly use extern keyworld.

    The main() file:
    Code:
    #include <cstdlib>
    #include <iostream>
    #include "function.h"
    
    using namespace std;
    
    double f(double x) {
           return g(x, array1, A);
           }
    
    int main()
    {const int A=5;
    double array1[A];
    double x;
    
    for (int i=0;i<A;i++) array1[i]=i;
    
    x=3.4;
    
    cout<<"The f(3.4) is: "<<f(x)<<endl;
    
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The file "function.h":
    Code:
    extern const int A;
    extern double *array1;
    
    //declare the function g(.)
    double g(double x, double *array, int arraydim);
    The file "gfunction.cpp" that defines the g(.) function:
    Code:
    #include <cmath>
    #include "function.h"
    
    double g(double x, double *array, int arraydim) {
           double value=0;
           for (int i=0;i<arraydim;i++) {
               value=value+pow(array[i],i)+pow(x,2.0);
               }
               return value;
           }
    whereas the error message is:
    Code:
      [Linker error] undefined reference to `A' 
      [Linker error] undefined reference to `array1' 
      ld returned 1 exit status
    What's wrong with my use of extern variables? My working environment is Bloodshed Dev C++ (it's so good; I love it!)

    Thanks a lot...
    Last edited by asmileguo; 08-09-2006 at 03:04 PM.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    The problem is that the variables you're externing aren't global variables. Move A and array1 outside the scope of main() and it should work. Aside from that, I believe you have the concept down.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    When I move the declaration of array1 outside of main(), I can only use the form:
    Code:
    double *array1;
    Even so, when I run the program, a new Microsoft warnings window pops up and I didn't get anything on the command prompt line.


    Quote Originally Posted by itsme86
    The problem is that the variables you're externing aren't global variables. Move A and array1 outside the scope of main() and it should work. Aside from that, I believe you have the concept down.

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,166
    Use double *array1 then, and inside main() use new to allocate memory for 5 doubles.
    If you understand what you're doing, you're not learning anything.

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    48
    Done! I appreciate it!
    This exercise further illustrate the use of new keyword... Thank you....

    Quote Originally Posted by itsme86
    Use double *array1 then, and inside main() use new to allocate memory for 5 doubles.

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    You can create normal arrays globally. You just need to change the extern declaration to match the type. Of course, without N being a true global constant (i.e. put into a common header file) that's not really possible.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. extern const?Please help
    By huwan in forum C++ Programming
    Replies: 10
    Last Post: 08-12-2008, 04:53 AM
  2. extern classes
    By Swordsman in forum C++ Programming
    Replies: 1
    Last Post: 05-07-2008, 02:07 AM
  3. Replies: 16
    Last Post: 10-29-2006, 04:04 AM
  4. Extern Question, really confused
    By SourceCode in forum C Programming
    Replies: 10
    Last Post: 03-26-2003, 10:11 PM
  5. extern symbols, what do these mean exactly?
    By Shadow12345 in forum C++ Programming
    Replies: 8
    Last Post: 11-02-2002, 02:14 PM

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