我无法编译这个简单的c ++脚本。有任何想法吗?

时间:2016-08-03 20:16:29

标签: c++ compiler-errors

我正在学习c ++而且我是一个菜鸟,任何帮助都会很好。 :) 请    这是一个基于文本的RPG,我搞砸了。制作RPG是学习语言的好方法。但任何帮助将不胜感激。

#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;
}

当我运行时,任何想法都会出现很长的错误?

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

2 个答案:

答案 0 :(得分:3)

您注释掉了标准命名空间...更改

//using namespace std;

using namespace std;

编辑:你的编译器正在告诉你究竟是什么错误......它无法找到对std命名空间函数的引用,因为你还没有声明它。但请注意,它是bad practice to be declaring it globally。当您更熟悉该语言时,您应该使用范围结果::运算符手动声明命名空间。

答案 1 :(得分:3)

显然,您评论了using namespace std;,但仍然使用了一些引用(coutcin)而未明确说明它们来自哪个命名空间(eq std::cout)。

解决方案:

std::添加到您的所有coutcin,因为using “using namespace std” in C++ is considered bad practice