Thread: Link problem with Lesson 5: Switch case

  1. #1
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62

    Question Link problem with Lesson 5: Switch case

    I have this piece of code, from Lesson 5: Switch case.

    When I compile it, I get this from VS 2005:
    Code:
    1>------ Build started: Project: test, Configuration: Release Win32 ------
    1>Compiling...
    1>test.cpp
    1>Linking...
    1>test.obj : error LNK2001: unresolved external symbol "void __cdecl playmultiplayer(void)" (?playmultiplayer@@YAXXZ)
    1>test.obj : error LNK2001: unresolved external symbol "void __cdecl playgame(void)" (?playgame@@YAXXZ)
    1>test.obj : error LNK2001: unresolved external symbol "void __cdecl loadgame(void)" (?loadgame@@YAXXZ)
    1>test - 4 error(s), 0 warning(s)
    ========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
    any suggestions ?
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  2. #2
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    where are those three functions declared? Are they in a library that you failed to include in the project or in another *.cpp file that you didn't add to the project. You have to find out where those functions are and either add the *.cpp or the library. Books often contain code snippets that will not compile and link by themselves.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    This program will compile, but cannot be run until the undefined functions are given bodies, but it serves as a model (albeit simple) for processing input.
    To link and run you need to fill in the code for those functions.

  4. #4
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Hi Ancient Dragon !

    It say's in the text:
    Below is a sample program, in which not all of the proper functions are actually declared, but which shows how one would use switch in a program.
    Code:
    #include <iostream>
    
    using namespace std;
    
    void playgame();
    void loadgame();
    void playmultiplayer();
    	
    int main()
    {
      int input;
      
      cout<<"1. Play game\n";
      cout<<"2. Load game\n";
      cout<<"3. Play multiplayer\n";
      cout<<"4. Exit\n";
      cout<<"Selection: ";
      cin>> input;
      switch ( input ) {
      case 1:            // Note the colon, not a semicolon
        playgame();
        break;
      case 2:            // Note the colon, not a semicolon
        loadgame();
        break;
      case 3:            // Note the colon, not a semicolon
        playmultiplayer();
        break;
      case 4:            // Note the colon, not a semicolon
        cout<<"Thank you for playing!\n";
        break;
      default:            // Note the colon, not a semicolon
        cout<<"Error, bad input, quitting\n";
        break;
      }
      cin.get();
    }
    Studying programming languages,
    you'll ALWAYS be a student ;-)

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > in which not all of the proper functions are actually declared
    This is the key phrase - the program isn't complete.

    > void playgame();
    For these three functions, you need to provide
    Code:
    void playgame() {
      cout << "playgame called\n";
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    C++ SharK The SharK's Avatar
    Join Date
    Mar 2004
    Location
    Denmark
    Posts
    62
    Hi Salem


    Yep, that did the trick ;-)

    No body, no function !


    regards,

    The SharK
    Studying programming languages,
    you'll ALWAYS be a student ;-)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Help problem in winsock code
    By lolguy in forum C Programming
    Replies: 8
    Last Post: 02-12-2009, 07:33 PM
  3. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  5. Request for comments
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 01-02-2004, 10:33 AM