undefined reference to cout,

I copied some code from learncpp.com and I get an error.
Here is the code from http://www.learncpp.com/cpp-tutorial/111-comprehensive-quiz/
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
int ReadNumber()
{
    using namespace std;
    cout << "Enter a number: ";
    int x;
    cin >> x;
    return x;
}

void WriteAnswer(int x)
{
    using namespace std;
    cout << "The answer is " << x << endl;
}

int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    return 0;
}


Here is the error it gives me:
~/Desktop/c++learning/quiz1> gcc -o num1 num1.cpp
/tmp/ccmz5SRA.o: In function `ReadNumber(void)':
num1.cpp:(.text+0x21): undefined reference to `cout'
num1.cpp:(.text+0x29): undefined reference to `ostream::operator<<(char const *)'
num1.cpp:(.text+0x39): undefined reference to `cin'
num1.cpp:(.text+0x41): undefined reference to `istream::operator>>(int &)'
/tmp/ccmz5SRA.o: In function `WriteAnswer(int)':
num1.cpp:(.text+0x70): undefined reference to `endl(ostream &)'
num1.cpp:(.text+0x8c): undefined reference to `cout'
num1.cpp:(.text+0x94): undefined reference to `ostream::operator<<(char const *)'
num1.cpp:(.text+0x9f): undefined reference to `ostream::operator<<(int)'
collect2: ld returned 1 exit status



if I use cout in the main method it doesn't give me errors, but if I use it in a different method it does. I am using gcc on Haiku OS.
Try getting rid of all the using namespace std; statements you have and put that statement just under the #include <iostream> line.
That's weird. Did you try putting using namespace std; just under #include <iostream> ? Did you also try doing std::cout and std::cin? And what about doing using std::cin; and using std::cout;? But, really, that code should compile.
If I take off the using namespace std, I still get the same error

If at the top I put using std::cout and using std::cin

If I take off the using namespace std and at the top put:
1
2
using namespace std::cout;
using namespace std::cin;


I get the error:
namespace 'cout' undeclared 
namespace 'cin' undeclared


If it helps, I looked at the <iostream> header and it says:

// -*- C++ -*- forwarding header.
// This file is part of the GNU C++ ANSI library

#ifndef _IOSTREAM_
#define _IOSTREAM_
#include <iostream.h>
endif


could this possibly be an error with gcc?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>
using namespace std;
int ReadNumber()
{
    cout << "Enter a number: ";
    int x;
    cin >> x;
    return x;
}

void WriteAnswer(int x)
{
   
    cout << "The answer is " << x << endl;
}

int main()
{
    int x = ReadNumber();
    int y = ReadNumber();
    WriteAnswer(x+y);
    cin.get();
    return 0;
}


Try that.
Last edited on
1
2
using namespace std::cout;
using namespace std::cin;

That's wrong. Instead, put this:

1
2
using std::cout;
using std::cin;

could this possibly be an error with gcc?

It's certainly possible. Like I said, the original code you posted should compile.
Thanks for all your help. I posted in the haiku forums and someone told me I was trying to use the c compiler, not the c++ compiler. I should use the g++ command instead of the gcc command.
Topic archived. No new replies allowed.