fehler beim kompilieren
-
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.
-
<off>nu is aber gut mit standard
</off>
-
finde ich auch
aber trotzdem interessant denke ich
werd jetzt immer
int main()
oder
int main(int zahl,*array[])
oder vlt doch
int main(int zahl,**array)
mal schauen vlt auch
int main(int zahl, array[][])
naja auf keine fall void main()
schreiben
-
es kann auch sein das dein prof mal vor jahren c++ gelernt hat und sich jetzt nicht mehr über "Neuerungen" informiert.
-
kann schon sein ja
aber mein wissen reicht mittlerweile ueber das was wir in der vorlesung hatten hinaus und mehr c++ vorlesungen werden wohl auch nicht mehr kommen
daher moegen solche sachen wie standartkonform irgendwie normal erscheinen
aber fuern blutigen anfaenger sicherlich unbedeutend
deswegen koennte ich meinen prof verstehen dass er sowas nicht einbezieht
waere aber auch moeglich dass er sich mit solchen dingen nicht beschaeftigt da komplexitaetstheorie ja die praxis weitraeumig umgeht was sein schwerpunktthema ist
und ich denke wenn er nicht will muss er solche sachen auch nicht wissen
darueberhinaus sind viele studenten auch froh nicht so viel c krams zu lernen
-
#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; }
habe mal dieses Programm genutzt und es gibt immer noch den fehler aus :
"unable to run this programm"kann das sein, dass man das programm an einem bestimmten Ort abspeichern muss oder so ???
-
#include <iostream> using namespace std; int main() { cout << "Hello, World!" << endl; }
sollte funzen
edit:
wenn du windows hast ist visual studio zu empfehlen da kommen dann entschluesselbare errors bei raus
-
lookias schrieb:
wenn du windows hast ist visual studio zu empfehlen da kommen dann entschluesselbare errors bei raus
man sollte die fehler jeden compilerst verstehen lernen.
dazu gibt es das compilerfaq. lesen.im übrigens sind die fehlermeldungen des studios nicht immer sonderlich logisch.
also teste erstmal andere compiler, bevor du sie verdammst
-
lookias schrieb:
...
wenn du windows hast ist visual studio zu empfehlen...schon komisch, genau _den_ versuche ich wo es nur geht zu vermeiden