0

My c++ program was giving e errors when I tried to compile so I fixed them all, but then it gave me a completely different set of errors about undefined references. Here's the code for armyGame.cpp:

#include <iostream>

int main(int argc, char **argv)
{
    int players;

std::cout << "Please enter the number of players, between 2 and 6:";

std::cin >> players;

switch (players) {
    case 1:
    std::cout << "Invalid input. Try again.";
    return 18;

    case 2:
    int p12, p22;

    case 3:
    int p13, p23, p33;

    case 4:
    int p14, p24, p34, p44;

        case 5:
        int p15, p25, p35, p45, p55;

        case 6:
        int p16, p26, p36, p46, p56, p66;
        }


    }

Here's my input to gcc:

gcc armyGame.cpp

And here's the error message:

/usr/bin/ld: /tmp/cc3SlY1r.o: in function `main':
armyGame.cpp:(.text+0x15): undefined reference to `std::cout'
/usr/bin/ld: armyGame.cpp:(.text+0x1a): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: armyGame.cpp:(.text+0x26): undefined reference to `std::cin'
/usr/bin/ld: armyGame.cpp:(.text+0x2b): undefined reference to `std::istream::operator>>(int&)'
/usr/bin/ld: armyGame.cpp:(.text+0x3d): undefined reference to `std::cout'
/usr/bin/ld: armyGame.cpp:(.text+0x42): undefined reference to `std::basic_ostream<char, std::char_traits<char> >& std::operator<< <std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*)'
/usr/bin/ld: /tmp/cc3SlY1r.o: in function `__static_initialization_and_destruction_0(int, int)':
armyGame.cpp:(.text+0x77): undefined reference to `std::ios_base::Init::Init()'
/usr/bin/ld: armyGame.cpp:(.text+0x86): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

Any guesses?

Brian61354270
  • 8,690
  • 4
  • 21
  • 43
  • 1
    Try compiling with `g++` instead of `gcc`? – cigien Apr 23 '21 at 20:33
  • 1
    What happened when you tried putting `c++ what is an undefined reference` into a search engine? How about `c++ undefined reference to std::cin`? – Karl Knechtel Apr 23 '21 at 20:35
  • Irrelevant: the `pXX` declarations in your switch block are also invalid. See [Why can't variables be declared in a switch statement?](https://stackoverflow.com/questions/92396/why-cant-variables-be-declared-in-a-switch-statement?rq=1) – Brian61354270 Apr 23 '21 at 20:36
  • Thanks! That worked. – Lem0n_C0der Apr 23 '21 at 20:36
  • 1
    The definitions in the case statement are valid, but useless. When the program starts using them things could get ugly, but for now they're sitting there collecting dust and probably being optimized out. – user4581301 Apr 23 '21 at 20:52
  • 1
    Side note: Any time you find yourself with sequentially named or numbered variables, odds are very good your program will be a lot easier to write with an array or [a library container](https://en.cppreference.com/w/cpp/container). – user4581301 Apr 23 '21 at 20:54

0 Answers0