‘cout’ does not name a type

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

Leave a Comment