thesonyman101
thesonyman101

Reputation: 821

I can't get this simple c++ script to compile. Any ideas?

Im learning c++ and am a noob at it any help would be nice. :) please This is a text based rpg that im messing with. Making a rpg is a great way to learn a language. But any help would be appreciated.

#include <iostream>

using namespace std;

int main()
{
    int health, armor, damage;
    health = 100;
    armor = 0;
    damage = 5;// This is a dynamic var for desision making
    int loopcon = 1;
    int decision = 1; 

    cout << "You enter the forest and have no idea what has happened and your head hurts.\n";
    cout << "You are trying to remember whaT happened to you and how you ended up here but you cant.\n";
    cout << "You see a town would you like to explore it?";
    cout << "What is your name?";
    while (loopcon == 1) {
        cout << "1/0";
        cin >> decision;
        if (decision == 1) 
            cout << "You enter the city";
        if (decision == 0)
            cout << "Theres also a forsest want to export that?";

        else
            cout << "try useing caps";

    }    


    return 0;
}

when I run this is get a big long error any ideas?

sudo gcc maingame.cpp
/tmp/cca53Qhv.o: In function `main':
maingame.cpp:(.text+0x31): undefined reference to `std::cout'
maingame.cpp:(.text+0x36): 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*)'
maingame.cpp:(.text+0x40): undefined reference to `std::cout'
maingame.cpp:(.text+0x45): 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*)'
maingame.cpp:(.text+0x4f): undefined reference to `std::cout'
maingame.cpp:(.text+0x54): 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*)'
maingame.cpp:(.text+0x5e): undefined reference to `std::cout'
maingame.cpp:(.text+0x63): 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*)'
maingame.cpp:(.text+0x6f): undefined reference to `std::cout'
maingame.cpp:(.text+0x74): 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*)'
maingame.cpp:(.text+0x80): undefined reference to `std::cin'
maingame.cpp:(.text+0x85): undefined reference to `std::istream::operator>>(int&)'
maingame.cpp:(.text+0x97): undefined reference to `std::cout'
maingame.cpp:(.text+0x9c): 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*)'
maingame.cpp:(.text+0xad): undefined reference to `std::cout'
maingame.cpp:(.text+0xb2): 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*)'
maingame.cpp:(.text+0xbe): undefined reference to `std::cout'
maingame.cpp:(.text+0xc3): 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*)'
/tmp/cca53Qhv.o: In function `__static_initialization_and_destruction_0(int, int)':
maingame.cpp:(.text+0xf7): undefined reference to `std::ios_base::Init::Init()'
maingame.cpp:(.text+0x106): undefined reference to `std::ios_base::Init::~Init()'
collect2: error: ld returned 1 exit status

Upvotes: 1

Views: 112

Answers (2)

ifma
ifma

Reputation: 3818

You commented out the standard namespace... Change

//using namespace std;

to

using namespace std;

Edit: your compiler is telling you exactly what's wrong... It can't find a reference to the std namespace functions because you haven't declared it. But note that it is bad practice to be declaring it globally. When you gain more familiarity with the language you should be declaring the namespace manually using the scope resultion :: operator.

Upvotes: 3

Khalil Khalaf
Khalil Khalaf

Reputation: 9397

Obviously you commented using namespace std; but kept using some of its references (cout and cin) without explicitly stating which namespace they come from (eq std::cout).

Solution:

Add std:: to all your couts and cins because using “using namespace std” in C++ is considered bad practice

Upvotes: 3

Related Questions