• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Newb C++ Question

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
 
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
 
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.
 
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.)
 
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.
 
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
 
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.
 
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.
 
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.)

 
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
 
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?
 
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" '
 
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
 
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.
 
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!
 
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.
 
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$
 
Back
Top