Newb C++ Question

Transition

Banned
Sep 8, 2001
2,615
0
0
Ok newb to C++ here.. I downloaded Cygwin so i could use GCC as my compiler. Here's what i tried to compile..

#include <iostream.h>

int main()
{
cout << "WORK DAMNIT\n";

return 0;
}

Here's what Cygwin tells me...

cortex@void ~
$ gcc test.c
test.c:1:20: iostream: No such file or directory
test.c: In function `main':
test.c:5: `cout' undeclared (first use in this function)
test.c:5: (Each undeclared identifier is reported only once
test.c:5: for each function it appears in.)

What am i doing wrong? TIA
 

Viper GTS

Lifer
Oct 13, 1999
38,107
433
136
Originally posted by: Transition
Ok newb to C++ here.. I downloaded Cygwin so i could use GCC as my compiler. Here's what i tried to compile..

#include <iostream.h>

int main()
{
cout << "WORK DAMNIT\n";

return 0;
}

Here's what Cygwin tells me...

cortex@void ~
$ gcc test.c
test.c:1:20: iostream: No such file or directory
test.c: In function `main':
test.c:5: `cout' undeclared (first use in this function)
test.c:5: (Each undeclared identifier is reported only once
test.c:5: for each function it appears in.)

What am i doing wrong? TIA

Sounds like it's not finding iostream.h.

Since that contains the declarations for cin, cout, etc. none of those will work.

Viper GTS
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: Viper GTS
Originally posted by: Transition
Ok newb to C++ here.. I downloaded Cygwin so i could use GCC as my compiler. Here's what i tried to compile..

#include <iostream.h>

close, but not quite. Either do it:

#include<iostream>

or

#include 'iostream.h'

BTW, can't find iostream was one of your errors.
 

Transition

Banned
Sep 8, 2001
2,615
0
0
I removed the .h from iostream.h and still got this..

cortex@void ~
$ gcc test.C
test.C: In function `int main()':
test.C:5: `cout' undeclared (first use this function)
test.C:5: (Each undeclared identifier is reported only once for each function
it appears in.)
 

MacBaine

Banned
Aug 23, 2001
9,999
0
0
Originally posted by: Transition
I removed the .h from iostream.h and still got this..

cortex@void ~
$ gcc test.C
test.C: In function `int main()':
test.C:5: `cout' undeclared (first use this function)
test.C:5: (Each undeclared identifier is reported only once for each function
it appears in.)

Ok, then what everyone else is saying is probably right.
 

Viper GTS

Lifer
Oct 13, 1999
38,107
433
136
Originally posted by: Transition
I removed the .h from iostream.h and still got this..

cortex@void ~
$ gcc test.C
test.C: In function `int main()':
test.C:5: `cout' undeclared (first use this function)
test.C:5: (Each undeclared identifier is reported only once for each function
it appears in.)

Same basic problem, you're calling undeclared functions.

Viper GTS
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Try this:

#include <iostream>

int main()
{
std::cout << "Does this work?";
return 0;
}
 

dejitaru

Banned
Sep 29, 2002
627
0
0
Think of it this way: should you have iostream? Probably not. It generally is not installed. What OS are you running?

try this in a new file:

int main()
{

print ("some text");

}

That doesn't use iostream, so it shoud compile.
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Better idea: search for iostream.h. When (if) you find it, include it in the file by doing this:

#include 'c:\full\path\to\iostream.h'

If that works, you'll have to figure out why your include folder isn't in your PATH.
 

Transition

Banned
Sep 8, 2001
2,615
0
0
Originally posted by: dejitaru
Think of it this way: should you have iostream? Probably not. It generally is not installed. What OS are you running?

try this in a new file:

int main()
{

print ("some text");

}

That doesn't use iostream, so it shoud compile.

nope..

crap.C: In function `int main()':
crap.C:4: `print' undeclared (first use this function)
crap.C:4: (Each undeclared identifier is reported only once for each functio
it appears in.)

 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: dejitaru
Think of it this way: should you have iostream? Probably not. It generally is not installed. What OS are you running?

try this in a new file:

int main()
{

print ("some text");

}

That doesn't use iostream, so it shoud compile.

It's printf, not print
 

Transition

Banned
Sep 8, 2001
2,615
0
0
I'm using cygwin right now. Am i supposed to be compiling this from a specific directory? And if so, how do i search in *nix for a file?
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: Transition
I'm using cygwin right now. Am i supposed to be compiling this from a specific directory? And if so, how do i search in *nix for a file?

I thought cygwin was win32...

In unix, try 'find / -name "iostream.h" '
 

glugglug

Diamond Member
Jun 9, 2002
5,340
1
81
try this:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << "yada yada\n";
}
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: glugglug
try this:

#include <iostream>

using namespace std;

int main(int argc, char *argv[])
{
cout << "yada yada\n";
}

Damn you! I always forget that part. And I'm in CompSci 3, which is really pathetic
 

manly

Lifer
Jan 25, 2000
13,286
4,060
136
The answer is rather simple:

The C++ compiler is g++, not gcc.

You can use gcc to compile C++ source, but you have to pass in a few command-line args that most people are not familiar with.
 

gwlam12

Diamond Member
Apr 4, 2001
6,946
1
71
Originally posted by: manly
The answer is rather simple:

The C++ compiler is g++, not gcc.

You can use gcc to compile C++ source, but you have to pass in a few command-line args that most people are not familiar with.

ah...interesting thought!
 

Transition

Banned
Sep 8, 2001
2,615
0
0
Same problem using g++ instead of gcc.. Can anyone recommend a good windows compiler? I'm already sick of Cygwin.. lol
 

TerryMathews

Lifer
Oct 9, 1999
11,464
2
0
Originally posted by: gwlam12
what is using namespace std? i have never seen that in my life

ANSI C++ allows you to defind namespaces beyond the standard (or std) namespace. Useful for overloading.

Technically, cout is std::cout for example. The using namespace std; line tells the compiler to use std::cout as opposed to some other namespace.
 

Descartes

Lifer
Oct 10, 1999
13,968
2
0
I rarely use gcc/g++ under Cygwin as I use VS(.NET), but... this works for me:

C:\dev\cpp>bash
bash-2.05a$ cat atotfoo.cpp
#include <iostream>

using namespace std;

int main(void)
{
cout << "foo" << endl;

return 0;
}bash-2.05a$ g++ atotfoo.cpp -o atotfoo.exe
bash-2.05a$ ./atotfoo.exe
foo
bash-2.05a$