Uczyłem się książki Adama Drozdka "Struktury danych i algorytmy w C++", cóż, wpisałem kod na stronie 15 w moim vimie i skompilowałem go na terminalu mojego Ubuntu 11.10.'cout' nie wymienia typu
#include <iostream>
#include <cstring>
using namespace std;
struct Node{
char *name;
int age;
Node(char *n = "", int a = 0){
name = new char[strlen(n) + 1];
strcpy(name, n);
age = a;
}
};
Node node1("Roger", 20), node2(node1);
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
strcpy(node2.name, "Wendy");
node2.name = 30;
cout << node1.name << ' ' << node1.age << ' ' << node2.name << ' ' << node2.age;
Ale jest jakiś błąd:
[email protected]:~$ g++ unproper.cpp -o unproper
unproper.cpp:15:23: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]
unproper.cpp:16:1: error: ‘cout’ does not name a type
unproper.cpp:17:7: error: expected constructor, destructor, or type conversion before ‘(’ token
unproper.cpp:18:1: error: ‘node2’ does not name a type
unproper.cpp:19:1: error: ‘cout’ does not name a type
Przeszukałem this, this, this i this, ale nie mogę znaleźć odpowiedzi.
Każda pomoc będzie mile widziane :)
Gdzie jest 'main()'? – Makoto
Tęsknisz za swoją główną. Kod znajduje się poza funkcją i jest uznawany przez kompilator za deklarację zmiennych, klas, struktur lub innych takich poleceń. Po prostu umieść cały dolny kod w int main() –