calling a method from another file

This is a discussion on calling a method from another file within the C++ Programming forums, part of the General Programming Boards category; I know this is a real dumb question but my mind completely went blank. In sorts.cpp I have three methods ...

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    100

    calling a method from another file

    I know this is a real dumb question but my mind completely went blank. In sorts.cpp I have three methods - bubbleSort(int[] array, int first, int last), selectionSort(same), quickSort(same). I don't have a default constructor. In my driver program, how would I call a sort method? Is there no way to do this except to create a sorts constructor and then in my driver create a variable sorts mySort then use mySort.bubbleSort(...) ?
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  2. #2
    Registered User
    Join Date
    Sep 2007
    Posts
    100
    Update: I have 3 files - prog6.cpp, sorts.cpp, and sorts.h. sorts.h is:

    Code:
    #include <stdexcept>
    
    void selectSort(int array[], int first, int last) throw (std::range_error);
    void bubbleSort(int array[], int first, int last) throw (std::range_error);
    void quickSort(int array[], int first, int last) throw (std::range_error);
    prog6.cpp and sorts.cpp include "sorts.h". sorts.cpp is not a class, just those 3 methods listed above. My question is how do I call any one of those 3 methods in sorts.cpp from prog6.cpp? I tried just writing "selectSort(myArray, 0, 20);" in prog6.cpp but I get the error:

    Code:
      [Linker error] undefined reference to `selectSort(int*, int, int)' 
      ld returned 1 exit status 
     Makefile.win [Build Error]  [sortz.exe] Error 1
    Code:
    else if(*argv[typeIndex] == 's')
        {
            cout << "Selection sort:\n";
            selectSort(array, 0, count);
        }
    "I don't fail - I succeed at finding things that don't work"
    Website Promotion Techniques @AbstractPromotion.com

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    How are you building your executable? Seeing "Makefile.win" I'm guessing Dev-C++. If so, you need to have both of your source files in the (same) project, otherwise they won't be linked together.

    BTW: This may be my own ignorance showing, and we'll hear about it if so, but I don't hear "method" used to refer to functions that aren't part of a class (we just call them "functions").

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,657
    It's all in how you write your source and header files.

    Think of a file as a translation unit. When the compiler starts compiling, it will most likely translate one file at a time. And remember that a prototype for a function is just a declaration of the function to be defined later: it lets the compiler know what sort is intended to be in that translation unit.

    So if you write a definition for, say sort() apart from anything that used sort() or knew what sort() was, then the sort.cpp file will compile but probably not link.

    This is why header files neatly contain declarations of things, so that you can successfully link anything that you would like to use or define across several translation units.

    Knowing this, I would write your files like this:
    Code:
    // sort.hpp
    #ifndef SORT_HPP__
    #define SORT_HPP__
    void bubble( int *ar, int size );
    void select( int *ar, int size );
    void quick( int *ar, int first, int last );
    #endif
    
    // sort.cpp
    #include "sort.hpp"
    
    void quick( int *ar, int first, int last )
    {
       // implement
    }
    
    void select ( int *ar, int size )
    {
      // implement
    }
    
    void bubble( int *ar, int size )
    {
      //implement
    }
    Did I make sense?
    Last edited by whiteflags; 12-02-2007 at 07:46 PM.

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,589
    On a technical note, names with double underscores are reserved to the implementation for any use. As such, it would be safer to use a variant of citizen's example, e.g.,
    Code:
    #ifndef SORT_HPP_
    #define SORT_HPP_
    Code:
    #ifndef SORT_HPP
    #define SORT_HPP
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. File transfer- the file sometimes not full transferred
    By shu_fei86 in forum C# Programming
    Replies: 13
    Last Post: 03-13-2009, 12:44 PM
  2. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  3. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 09:46 AM
  5. System
    By drdroid in forum C++ Programming
    Replies: 3
    Last Post: 06-28-2002, 10:12 PM

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