Strange error with C++ code
DiggBlinkRedditDeliciousTechnorati
question by Steven | Moderate
.
include <iostream> //**required for cout and cerr
using namespace std;
int main() {
cout << "Hello class";
return 0;
}
When I try to build it I get the following
1>------ Build started: Project: quicksort, Configuration: Debug Win32 ------
1>Compiling...
1>quicksort.cpp
1>c:\documents and settings\jason byrd\my documents\visual studio 2005\projects\quicksort\quicksort\quicksort.cpp(1) : error C2143: syntax error : missing ';' before '<'
1>c:\documents and settings\jason byrd\my documents\visual studio 2005\projects\quicksort\quicksort\quicksort.cpp(1) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
1>c:\documents and settings\jason byrd\my documents\visual studio 2005\projects\quicksort\quicksort\quicksort.cpp(11) : error C2065: 'cout' : undeclared identifier
1>Build log was saved at "file://c:\Documents and Settings\Jason Byrd\My Documents\Visual Studio 2005\Projects\quicksort\quicksort\Debug\BuildLog.htm"
1>quicksort - 3 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
Can anyone tell me what is going wrong?
Post reply
Subscriptions
Re: some c++ problemreply by Abhishek Chatterjee Firstly, you need to #include<iostream.h> not just iostream.
Visual C++ doesn't assume header extension.
Also you are forgetting '# (read Hash)'before the include statement.
Hope now it should run :-)
Post reply
Subscriptions
Re: Strange error with C++ codereply by Srirangan Steven, please change:
include <iostream>
to:
#include <iostream>
>> Firstly, you need to #include<iostream.h> not just iostream.
Hmm, he did mention the line: using namespace std; so <iostream> is correct.
Post reply
Subscriptions
Re: Strange error with C++ codereply by mastercomputers The problem is missing the # for the preprocessor include, e.g. #include <iostream>
There's a difference between using <iostream.h> and <iostream> and the preferred method is without the extension. VC++ should be able to understand <iostream> so I don't know why it was suggested that it should include the older/backwards compatible means of the header file.
Those three errors, especially the last one should have pinpointed the problem for you, 'cout': undeclared identifier, the only reason it'd be undeclared is if you did not include the relevant header files, you assumed you had but because of incorrect syntax you got the first two errors which did not understand how to compile include <iostream>.
A lot of programmers debate over the use of using namespace std, For me, I don't mind if people use it or not but some alternatives is just using what you need, e.g. using std::cout; using std::endl; instead of using namespace std;
It's also a good idea to do cout << "Hello class" << endl; because endl not only gives a new line but flushes the output buffer too, although if you did not want a newline, then you can ignore that.
Post reply
Subscriptions
Got a C++ Question?
Just Sign Up and ask the top C++ experts!
|