I was learning Adam Drozdek's book "Data Structures and Algorithms in C++", well, I typed the code in page 15 in my vim and compiled it in terminal of my Ubuntu 11.10.
#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;
But there's some error:
oo@oo:~$ 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
I have searched this,this,this and this, but I can't find the answer.
Any help would be appreciated:)
The "error does not name a type" in C/C++ is defined as the when user declares outside of the function or does not include it properly in the main file this error will through.
C++ code. Use CamelCase for all names. Start types (such as classes, structs, and typedefs) with a capital letter, other names (functions, variables) with a lowercase letter.
This may happen because std::cout is writing to output buffer which is waiting to be flushed. If no flushing occurs nothing will print. So you may have to flush the buffer manually by doing the following: std::cout.
cout in C++ The cout object in C++ is an object of class iostream. It is defined in iostream header file. It is used to display the output to the standard output device i.e. monitor. It is associated with the standard C output stream stdout.
The problem is that the code you have that does the printing is outside of any function. Statements that aren't declarations in C++ need to be inside a function. For example:
#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; } }; int main() { 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; }
You are missing the function declaration around your program code. The following should solve your error:
#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; } }; int main() { 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; }
The error you then get (something like "invalid conversion from int to char*") is because you try to set an integer value (30) to a string attribute (name) with
node2.name=30;
I think
node2.age=30;
would be correct.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With