You are not logged in.

  • Login

Dear visitor, welcome to Coder Forum. If this is your first visit here, please read the Help. It explains in detail how this page works. To use all features of this page, you should consider registering. Please use the registration form, to register here or read more information about the registration process. If you are already registered, please login here.

1

Sunday, March 11th 2007, 6:37pm

Falsche Version

Hi,

ich wundere mich gerade über meine Version von Visual C++ bzw der Compiler Version.

Denn: Schon beim einfachsten Code gibt es Fehler.

Bsp: (hellowprld.cpp)

C Quellcode

1
2
3
4
5
6
#include <iostream>
 
void main ()
{
	cout << "Hello world!" << endl;
}


Und der Fehler:

Quoted

--------------------Configuration: helloworld - Win32 Debug--------------------
Compiling...
helloworld.cpp
c:\dokumente und einstellungen\administrator\desktop\c++\helloworld.cpp(5) : error C2065: 'cout' : undeclared identifier
c:\dokumente und einstellungen\administrator\desktop\c++\helloworld.cpp(5) : error C2297: '<<' : illegal, right operand has type 'char [13]'
c:\dokumente und einstellungen\administrator\desktop\c++\helloworld.cpp(5) : error C2065: 'endl' : undeclared identifier
Error executing cl.exe.

helloworld.exe - 3 error(s), 0 warning(s)

2

Sunday, March 11th 2007, 6:42pm

standartfunktionen liegen im namespace std:


C Quellcode

1
2
3
4
5
6
#include <iostream>
 
void main ()
{
        std::cout << "Hello world!" << std::endl;
}


funktioniert.
VC war immer wieder mit den Namespaces (std::) sehr nachlässig, möglich das es mit einer alten Version funktionieren würde. Ist dann allerdings kein ISO C++. Der obige Code ist dann fast korrekt:

C Quellcode

1
2
3
4
5
6
#include <iostream>
 
int main ()
{
        std::cout << "Hello world!" << std::endl;
}


nach Standart und

C Quellcode

1
2
3
4
5
6
7
#include <iostream>
 
int main ()
{
        std::cout << "Hello world!" << std::endl;
return 0;
}

Damits auch unter VC läuft

3

Sunday, March 11th 2007, 7:18pm

Einfacher geht es:

man muss einfach das

C Quellcode

1
#include <iostream>
in

C Quellcode

1
#include <iostream.h>
ändern. Und zack, es geht.

4

Sunday, March 11th 2007, 7:25pm

ähm ja funktioniert.
Ist allerdings keine tidy Variante.
die header (ohne c am anfang und) mit .h danach sind nur aus kompatiblitätsgründen vorhanden und solten nicht verwendet werden wenn nicht nötig.

in kleineren Programmen ist es uU vertretbar in die .cpp (nicht in den Header wenn vorhanden) ein "using namespace std" schreiben. Dann funktionierts auch.

void als Rückgabewert für main() auch auf jeden Fall vermeiden!

Similar threads

Tagging

Social bookmarks