‘cout’ does not name a type

Solution 1:

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

Solution 2:

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.