fehler beim kompilieren
-
hallo
ich habe gerade angefangen c++ zu proggen und kenne mich noch überhaupt nicht aus...
ich habe mal ein kleines Programm zum testen geschrieben:int main(void) { cout << "Hello World"; }
Als ich es dann kompilieren wollte gab es einen Fehler aus:
"unable to run programm"
woher kommt das ?
als Programm habe ich dev-c++ von www.bloodshed.net/devcpp.html
heruntergeladenthx im voraus
-
forensuche "unable run program" --> andere compiler
und du wirst fündig
-
int main(void) { cout << "Hello World"; }
Ist das dein ganzes Programm?
Schau mal hier nach:
http://www.volkard.de/C++/Tutorial/Grundlagen/Hello,_world!/index.html
-
ertmal uebernimmt main keine variablen
das schreibt man dann so
int main()
dann gibt main hier auch nix zurueck also
void main()
um cout zu benutznen musst du am anfang die iostream.h klasse einbinden
#include<iostream.h>
dann laeuft das
wenn man vor ne funktion zb main einen typ schreibt dann muss der diesen auch mit return zurueckliefern
(nicht in allen faellen aber es muss mindestens einmal drin stehen dass das nun dazu kommt ist nicht notwendig)das kann man bei main zb machen wenn man unter einer bestimmten vorraussetzung
das programm abbrechen willdann schreibt man einfach
return 0;
ansonsten laeuft das programm einfach weiter und endet dann bei der letzten }
klammersowas sollte man besonders bei iterativen rekrusionen immer beachten
-
lookias schrieb:
ertmal uebernimmt main keine variablen
Doch, kann sein.
dann gibt main hier auch nix zurueck
Doch.
void main()
Falsch.
um cout zu benutznen musst du am anfang die iostream.h klasse einbinden
Schlecht. Nimm <iostream>
dann laeuft das
... nicht standardkonform.
wenn man vor ne funktion zb main einen typ schreibt dann muss der diesen auch mit return zurueckliefern
Bei main nicht.
-
@lookias: Lange nichts mehr mit C++ gemacht?
int main(void) falsch, aber void main() nicht? :p
-
Strogij: Falls du auf C anspielst: Da ist void main() auch falsch. Wenn nicht, dann hast du recht.
-
Der C++-Standard definiert zwei mögliche Formen für main:
int main(); int main(int argc, char *argv[]);
letzteres kann auch als
int main(int argc, char **argv);
geschrieben werden. Alles andere ist falsch. Ich weiß, dass die Ansicht "Ich klick da drauf, dann macht das was, das gibt doch nichts zurück" sehr verbreitet ist, aber wer schonmal ein Shellskript geschrieben hat, weiß, dass Programme sehr wohl einen Wert an die Shell zurückgeben.
Was das kleine Programm angeht:
#include <iostream> //für cin, cout usw. using namespace std; // Nicht zwingend nötig, ohne dass hier muss halt std::cout statt cout und std::endl statt endl geschrieben werden. int main() { cout << "Hello, World!" << endl; // main gibt per default 0 zurück. Wer ausführlich sein will, schreibt noch return 0; }
-
oh lanmg nichts mehr c++ gemacht is net ganz richtig
bin eher noch net sehr lang dabei
muss aber sagen dass wir an der uni das auch so gelernt haben void main()
sry wenn da was falsch war
ist <iostream> irgendwie der neue standard?
weil in jesse liberty c++ in 21 tagen
wird das net benutzt
@michael: wenn void vor der funktion steht dann heisst das doch dass nix zurueckgeben wird oda?
ich mein nix kann man ja auch nix zurueckgeben
trotzdem danke fuer die kritik
-
kann es sein das ein professor ein bisschen zurückgeblieben ist?
-
na der jung hat als sein hauptgebiet
realisierung von algorithmen auf leistungsschwachen rechnern
-
lookias schrieb:
@michael: wenn void vor der funktion steht dann heisst das doch dass nix zurueckgeben wird oda?
Richtig. main ist eine besondere Funktion. Im Standard steht folgendes:
§3.6.1.5 schrieb:
A return statement in main has the effect of leaving the main function (destroying any objects with automatic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
-
0xdeadbeef schrieb:
Alles andere ist falsch.
Nein, ist es nicht. Da wäre zum einen noch 'int main(void)' was in C++ ja das gleiche wie 'int main()' ist. Und zum anderen schreibt der Standard nur den Rückgabetyp int vor.
Die zwei von dir angesprochenen main Funktionen werden vom Standard lediglich garantiert. Alles andere ist implementationsspezifisch, aber nicht falsch.lookias schrieb:
ist <iostream> irgendwie der neue standard?
Neu?
Der aktuelle Standard wurde immerhin 1998 verabschiedet. 2003 gabs 'ne Überarbeitung. Wenn ihr an der Uni immer noch solch grundlegende Sachen wie <iostream> nicht standardkonform macht (und an der Nichtverfügbarkeit aktueller Compiler kanns ja auch nicht liegen), würde ich meinen Prof durchaus mal darauf ansprechen.
-
ich denk mal einfach dass mein prof darauf verzichtet um fuer den anfaenger den code moeglichst klein zu halten
weil wohl namespace nicht gelehrt wird und dann auch nicht muss
schlimmer finde ich dasds viele unis den mathe studenten noch c beibringen
dies ganze printf und inputf schreckt den anfaenger ja wohl total ab
printf "%20.10d\n" 200
finde ich total haesslich
-
lookias schrieb:
schlimmer finde ich dasds viele unis den mathe studenten noch c beibringen
dies ganze printf und inputf schreckt den anfaenger ja wohl total ab
printf "%20.10d\n" 200
wieso "noch"?
du kannst entweder c lernen oder c++. da gibt es nicht eine "noch" beziehung.
beides steht nebeneinander.und in mathe ist c nicht unsinnig
so long
-
naja habe da nicht so den ueberblick aber ist c nicht schon schlechter weil es keine elementmethoden in struct hat?
-
lookias schrieb:
naja habe da nicht so den ueberblick aber ist c nicht schon schlechter weil es keine elementmethoden in struct hat?
gibt nix schlechter ... nur anders.
übrigens gibt es für c ein eigenea unterforum hier im forum
-
lookias schrieb:
ich denk mal einfach dass mein prof darauf verzichtet um fuer den anfaenger den code moeglichst klein zu halten
weil wohl namespace nicht gelehrt wird und dann auch nicht muss
Das kann doch wohl kein Argument sein. Namensräume sind ja nicht ein so komplexes Thema, dass man es nicht mal nebenbei mit abhandeln kann.
lookias schrieb:
schlimmer finde ich dasds viele unis den mathe studenten noch c beibringen
Wie elise schon gesagt hat, C und C++ sind zwei völlig unabhängige Sprachen. Auch wenn beide einen gemeinsamen Ursprung haben und syntaktisch nahezu identisch.
lookias schrieb:
naja habe da nicht so den ueberblick aber ist c nicht schon schlechter weil es keine elementmethoden in struct hat?
Nein. Klar, C ist keine OO Sprache. Aber ein
foo.bar(...)
ist ja letztendlich auch nicht mehr als
bar(&foo, ...)
-
Uebrigens, der Grund, warum main() nicht funktioniert, ist folgender:
main() { }
war im alten K&R C (vor 1990) eine "incomplete function declaration", mit dem Rueckgabewert "int" (Default) und den Parametern "beliebig".
In C++ ist das natuerlich verboten, da C++ mindestens dem ANSI C Standard von 1990 entsprechen muss (fuer C Code).
Auch hier gilt: Laesst man "return" weg, wird trotzdem ein Integer zurueckgegeben, mit dem momentanen Wert des Rueckgaberegisters (auf Intel-Prozessoren i.d.R. 32-bittig EAX, unter 16-bit AX).
Die Unsitte
void main( void ); /* oder */ void main( int argc, char** argv );
mit "void" als Rueckgabetyp, war in einigen frueheren C-Standards durchaus erlaubt, setzt allerdings voraus, dass Symbole im C-Programm eine Typsignatur haben, damit der Linker die verschiedenen Faelle unterscheiden kann. Das war aber meist nicht der Fall.
Die C/C++-Standard-Library ruft main meist so auf:
; ... mov eax,__argv push eax mov eax,__argc push eax call _main add esp,8 ; eax enthaelt rueckgabewert push eax jmp _exit
Dadurch ist die Typsignatur von main egal, aber auch, welche Parameter oder Rueckgabewerte es liefert. Dem Linker ist es also i.d.R. wurscht, ob main() korrekt deklariert ist. In den meisten Faellen wird direkt nach main() exit() aufgerufen, mit dem Rueckgabewert von main() als Parameter.
Das fuehrt tatsaechlich dazu, dass z.B. Shell-Skripte, die ein Programm aufrufen, den zufaelligen Rueckgabewert des Programms auswerten muessen. Bei manchen Betriebssystemen fuehren seltsame Rueckgabewerte von main() zu seltsamen Fehlermeldungen.
Z.B. sagen dann manche Betriebssysteme "Programm nicht gefunden", obwohl es ausgefuehrt wurde, und solche Sachen.
Die einzig richtige Deklaration von main() ist, wie bereits erwaehnt wurde:
int main( int argc, char** argv ); /* oder */ int main( int argc, char* argv[] );
Es lohnt sich gelegentlich einen Blick in den Quelltext der C-Standardlibrary zu werfen, sofern vorhanden (meist CRT genannt). Die meisten Compiler liefern den Assembler oder C-Source-Code des Startup-Codes mit, sodass man genau nachvollziehen kann, was vor bzw. nach dem Aufruf von main() passiert.
-
Ums mal endgültig aufzuklären, zitier ich den C99 FCD:
5.1.2.2.1 Program startup [#1] The function called at program startup is named main. The implementation declares no prototype for this function. It shall be defined with a return type of int and with no parameters: int main(void) { /* ... */ } or with two parameters (referred to here as argc and argv, though any names may be used, as they are local to the function in which they are declared): int main(int argc, char *argv[]) { /* ... */ } or equivalent;8) or in some other implementation-defined manner. [#2] If they are declared, the parameters to the main function shall obey the following constraints: -- The value of argc shall be nonnegative. -- argv[argc] shall be a null pointer. -- If the value of argc is greater than zero, the array members argv[0] through argv[argc-1] inclusive shall contain pointers to strings, which are given implementation-defined values by the host environment prior to program startup. The intent is to supply to the program information determined prior to program startup from elsewhere in the hosted environment. If the host environment is not capable of supplying strings with letters in both uppercase and lowercase, the implementation shall ensure that the strings are received in lowercase. -- If the value of argc is greater than zero, the string pointed to by argv[0] represents the program name; argv[0][0] shall be the null character if the program name is not available from the host environment. If the value of argc is greater than one, the strings pointed to by argv[1] through argv[argc-1] represent the program parameters. -- The parameters argc and argv and the strings pointed to by the argv array shall be modifiable by the program, and retain their last-stored values between program startup and program termination. ____________________ 8) Thus, int can be replaced by a typedef name defined as int, or the type of argv can be written as char ** argv, and so on.
Entnommen aus http://www.open-std.org/jtc1/sc22/open/n2794/n2794.txt
Der C++98 CD2 dagegen sagt:
3.6.1 Main function [basic.start.main] 1 A program shall contain a global function called main, which is the designated start of the program. It is implementation-defined whether a program in a freestanding environment is required to define a main function. [Note: in a freestanding environment, start-up and termination is implementation-defined; start- up contains the execution of constructors for objects of namespace scope with static storage duration; termi- nation contains the execution of destructors for objects with static storage duration. ] 2 An implementation shall not predefine the main function. This function shall not be overloaded. It shall have a return type of type int, but otherwise its type is implementation-defined. All implementations shall allow both of the following definitions of main: int main() { /* ... */ } and int main(int argc, char* argv[]) { /* ... */ In the latter form argc shall be the number of arguments passed to the program from the environment in which the program is run. If argc is nonzero these arguments shall be supplied in argv[0] through argv[argc-1] as pointers to the initial characters of null-terminated multibyte strings (NTMBSs) (17.2.2.1.3.2) and argv[0] shall be the pointer to the initial character of a NTMBS that represents the name used to invoke the program or "". The value of argc shall be nonnegative. The value of argv[argc] shall be 0. [Note: it is recommended that any further (optional) parameters be added after argv. ] 3 The function main shall not be called from within a program. The linkage (3.5) of main is implementation-defined. A program that takes the address of main, or declares it inline or static is ill-formed. The name main is not otherwise reserved. [Example: member functions, classes, and enumer- ations can be called main, as can entities in other namespaces. ] 4 Calling the function void exit(int); declared in <cstdlib> (18.3) terminates the program without leaving the current block and hence with- out destroying any objects with automatic storage duration (12.4). If exit is called to end a program dur- ing the destruction of an object with static storage duration, the program has undefined behavior. 5 A return statement in main has the effect of leaving the main function (destroying any objects with auto- matic storage duration) and calling exit with the return value as the argument. If control reaches the end of main without encountering a return statement, the effect is that of executing return 0;
Nachzulesen unter ftp://ftp.research.att.com/dist/c++std/WP/CD2/
Das sind zwar beides nicht die offiziellen Standards (die gibts nicht für lau), aber ich nehme stark an, dass sich zwischen den committee drafts und den Standards an dieser Sektion nichts bzw. nichts wesentliches geändert haben dürfte.