Thread: undefined reference error when compiling

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    undefined reference error when compiling

    I am getting the above error on the lines marked here in the code. I have included my functions declarations as well. Can someon please tell what is wrong and what can I do to remedy it?
    Code:
    StackCities(StartCity, EndCity, DistanceMatrix,
                      PredecessorMatrix, st, Reverse);//<--------HERE
    
          // Display the route - Start city to End city
          //==========================================================
          DispRoute(Reverse, st);     // For testing
    
          // Display the Intervening cities and the distances between
          // them including the cumulative distances for the route
          //==========================================================
          DispItinerary(st, StartCity, EndCity, DistanceMatrix, Reverse);//<-----HERE
    The function declarations:
    Code:
    void PrintPredChart(int cities, StringTable &st, TwoDIntArray &PredecessorArray);
    void DispItinerary(StringTable &st, string StartCity,  string EndCity, TwoDIntArray &DistanceMatrix, Stack &Reverse);
    void DispRoute(Stack &Reverse, StringTable &st);
    void InitMatrices(TwoDIntArray &DistanceMatrix, TwoDIntArray &PredecessorMatrix, int cities);
    void getMapData(ifstream &in, StringTable &st, char * buf, TwoDIntArray &DistanceMatrix, TwoDIntArray &PredecessorMatrix);
    void FloydWarshall(TwoDIntArray &DistanceMatrix, TwoDIntArray &PredecessorMatrix, int cities);
    void StackCities(string StartCity, string EndCity, TwoDIntArray &DistanceMatrix, TwoDIntArray &PredecessorMatrix, StringTable &st, Stack &Reverse);
    just in case:
    Code:
    int menuChoice;
        string StartCity;
        string EndCity;
        string fileName;
        //ifstream in;
    
        cout<<"Welcome to J.A.C. P2 Assignment\n"
          "\n"
          "This program will find the shortest path\n"
          "from One city to all other cities if there\n"
          "is a connecting node, find the shortest path\n"
          "between two cities or find the shortest\n"
          "between three or more cities.\n"<<endl;
    
        cout<<"Please make a choice of what you would like to do:\n"<<endl;
    
        cout<<"  1------> Shortest Path between 2 cities.\n"
              "  2------> Shortest Path between 3 or more cities.\n"
              "  3------> Shortest Path from 1 city to all.\n"
              "  9------> Take your ball and go home!\n"<<endl;
        cout<<"Waiting on you: "; cin>>menuChoice;
    
        switch (menuChoice) {
            case 1:
                cout<<"Enter the starting city: ";
                cin>>StartCity;
                cout<<"\nEnter the ending city: ";
                cin>> EndCity;
                cout<<"\nEnter the name of the file: ";
                cin>> fileName;
    
            break;
    Thank You.
    Code:
    obj\Debug\main.o||In function `main':|
    C:\Users\Documents\C++ Files\short\main.cpp|139|undefined reference to `StackCities(std::string, std::string, TwoDIntArray&, TwoDIntArray&, StringTable&, Stack&)'|
    C:\Users\Documents\C++ Files\short\main.cpp|148|undefined reference to `DispItinerary(StringTable&, std::string, std::string, TwoDIntArray&, Stack&)'|
    ||=== Build finished: 2 errors, 0 warnings ===|
    Last edited by csharp100; 05-07-2012 at 04:12 PM. Reason: added actual "build messages"

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    FYI: You are NOT getting "undefined reference error" when compiling.
    These happen during the linking stage NOT the compiling stage.

    Do the linking correctly to fix the problem.
    Link to the needed object file/library.

    NOTE: If you want more help post the errors!

    Tim S.
    Last edited by stahta01; 05-07-2012 at 04:11 PM.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  3. #3
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    It's also a good idea to paste the errors verbatim in your post.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by stahta01 View Post
    FYI: You are NOT get "undefined reference error when compiling".
    These happen during the linking stage NOT the compiling stage.

    Tim S.
    Hehe, nice drive-by, but thank you my mistake.

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Quote Originally Posted by csharp100 View Post
    Hehe, nice drive-by, but thank you my mistake.
    You are welcome.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  6. #6
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Undefined references have nothing to do with declarations.

    Where are these functions defined? The object file compiled from that source is the file that will be needed which stahta01 (Tim) is talking about linking.

    Soma

  7. #7
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by phantomotap View Post
    Undefined references have nothing to do with declarations.

    Where are these functions defined? The object file compiled from that source is the file that will be needed which stahta01 (Tim) is talking about linking.

    Soma
    Found it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. error: undefined reference
    By 9erNumber16 in forum C++ Programming
    Replies: 1
    Last Post: 04-25-2012, 07:27 PM
  2. undefined reference error, please help me
    By leo2008 in forum C Programming
    Replies: 8
    Last Post: 03-27-2012, 03:50 AM
  3. Undefined Reference Error
    By NuNn in forum C Programming
    Replies: 12
    Last Post: 01-15-2009, 01:34 PM
  4. undefined reference error
    By gcctest in forum C Programming
    Replies: 3
    Last Post: 12-19-2008, 08:17 AM
  5. Undefined Reference Compiling Error
    By AlakaAlaki in forum C++ Programming
    Replies: 1
    Last Post: 06-27-2008, 11:45 AM