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 unproperunproper.cpp:15:23: warning: deprecated conversion from string constant to ‘char*’ [-Wwrite-strings]unproper.cpp:16:1: error: ‘cout’ does not name a typeunproper.cpp:17:7: error: expected constructor, destructor, or type conversion before ‘(’ tokenunproper.cpp:18:1: error: ‘node2’ does not name a typeunproper.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:)

7

Best Answer


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;}

The error 'cout does not name a type' is a common error in C++. It occurs when the 'cout' object is not recognized as a type. This error is often caused by forgetting to include the <iostream> header or by missing the 'std' namespace.

In C++, 'cout' is the standard output stream object that is used to display output on the console. To use 'cout', you need to include the <iostream> header at the beginning of your program. The <iostream> header contains the declarations for the 'cout' object and other input/output related functions and objects.

Additionally, in C++, the 'cout' object is defined in the 'std' namespace. If you forget to add the 'std' namespace before using 'cout', the compiler will not recognize 'cout' as a valid type and will produce the error 'cout does not name a type'.

To fix this error, make sure to include the <iostream> header at the beginning of your program and use the 'std' namespace before using 'cout'. Here's an example:

#include <iostream>int main() {std::cout << "Hello, World!" << std::endl;return 0;}

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.

main() function is missed.There should be a main() function in C++ ,and you should put cout into a function.

If you want to use cout outside the function you can do it by collecting the value returned by cout in boolean.see the below example

#include<iostream>using namespace std;bool b=cout<<"1";int main(){return 0;}

output:

error prog.cpp:4:14: error: cannot convert 'std::basic_ostream<char>' to 'bool' in initializationbool b=cout<<"1";

Including:

int main(){ //code return 0;}

will help you. This problem usually occurs to those who are learning from book in which they usually don't use main function after a few chapters.

for Class Error : you Should Use the cout inside any method of the class.You cannot use cout openly in any class.

class result:public exam{public:display_result(){float percentage;percentage=(physics + maths) / 2;get_roll_number();getmarks();cout << "The Final Percentage of the Student is =" <<percentage<< "%" << endl;}};