Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Really basic question plz answer quickly 1

Status
Not open for further replies.

romanstandrd

Programmer
Jul 16, 2005
7
US
I tried compiling a "hello world" script but I got this error message:

Compiling...
testprgm.cpp
testprgm.cpp(2) : error C2019: expected preprocessor directive, found '<'
testprgm.cpp(6) : error C2065: 'cout' : undeclared identifier

Why doesn't it work? Am I even supposed to compile it first? Thank you.
 
Is this a c++ program, or Visual c++?

Try replacing cout with std::cout and see if that helps.
 
nope, didnt work. got this log instead:

Compiling...
testprgm.cpp
testprgm.cpp(2) : error C2019: expected preprocessor directive, found '<'
testprgm.cpp(6) : error C2653: 'std' : is not a class or namespace name
testprgm.cpp(6) : error C2065: 'cout' : undeclared identifier
 
I don't have much experience with running one within another. Didn't even know that could be done.
 
Post your actual code.
Please use the [tt][ignore]
Code:
[/ignore][/tt]
tags when posting code.

--
 
Code:
# <ostream.h>

int main()
{
	std::cout << "Hello world! (and Eric)" << '/n';
	
	return 0;
}
 
whoops i didnt mean to put the version with std:: in there. I don't even know if it's a problem with the code though... thanks for your help everyone.
 
Oh my!...
Code:
#include <iostream>
 
Just for future info romanstandrd, you see that your error was really simple, but impossible to figure out without the relevant code. Always put enough information! A four line program which can reproduce the error is great!

Vincent
 
also note that if you use
Code:
#include <iostream>
you have to say
Code:
std::cout<<"whatever"<<endl;

if you use:
Code:
#include <iostream>
//other includes
//using namespace std;
then you just say
Code:
cout<<"whatever"<<endl;

do not use #include <iostream.h> .. you can say cout<<"whatever" with that.. but it is an old header and should no longer be used. the same is true for many other common libraries.
 
Thank you very much everyone! But... AH HA! It still doesn't work. Now using the exact code:
Code:
# <iostream>

int main()
{
	std::cout<<"Hello world! (and Eric)"<<endl;
	
	return 0;
}
returns the copile error message:
Compiling...
testprgm.cpp
testprgm.cpp(2) : error C2019: expected preprocessor directive, found '<'
testprgm.cpp(6) : error C2653: 'std' : is not a class or namespace name
testprgm.cpp(6) : error C2065: 'cout' : undeclared identifier
testprgm.cpp(6) : error C2065: 'endl' : undeclared identifier
 
Do you know anything about C/C++ preprocessor directive #include? Read a book carefully before copy a text from it.
Code:
#include <iostream>
but it's absolutely senseless statement:
Code:
# <iostream>
 
the "#" part tells the compiler of a preprocessor directive (loosely speaking). you have to tell it what to do after that. "include" in your case.

also an ammendment to my previous post. i said:

also note that if you use

Code:
#include <iostream>
you have to say
std::cout<<"whatever"<<endl;

std::cout<<"whatever"<<endl;
should be
std::cout<<"whatever"<<std::endl;
 
Thanks a bunch guys for everything; it worked of course. It's always those fun little things.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top