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; }
Related Posts:
- How many spaces for tab character(\t)?
- How do I build a graphical user interface in C++? [closed]
- What are the differences between a pointer variable and a reference variable in C++?
- outputting ascii table in C++
- What is the effect of extern “C” in C++?
- What exactly is the difference between “pass by reference” in C and in C++?
- What is `CString`?
- What exactly is nullptr?
- How to make a SIMPLE C++ Makefile
- finding dll for “The specified module could not be found”
- no matching function for call to ‘ ‘
- Read file line by line using ifstream in C++
- Convert an int to ASCII character
- When should I write the keyword ‘inline’ for a function/method?
- When does a process get SIGABRT (signal 6)?
- Logical XOR operator in C++?
- C++ – Decimal to binary converting
- Alternative to itoa() for converting integer to string C++?
- 1e-9 or -1e9, which one is correct?
- Returning an empty string : efficient way in c++
- invalid conversion from ‘const char*’ to ‘char*’
- Evaluate a string with a switch in C++ [duplicate]
- Expected initializer before function name
- Inheriting constructors
- Pass a vector by reference C++
- What does int & mean
- Evaluate a string with a switch in C++ [duplicate]
- “&” meaning after variable type
- How can I create objects while adding them into a vector?
- What is the printf format specifier for bool?
- libpng warning: iCCP: known incorrect sRGB profile
- In CLion, header only library: file “does not belong to any project target, code insight features might not work properly”
- Is a function definition not allowed here before a ‘{‘ token?
- terminate called after throwing an instance of ‘std::out_of_range’
- What does “missing template argument” mean?
- undefined reference to ‘std::cout’
- How can I convert const char* to string and then back to char*?
- How do I call the class’s destructor?
- Can’t resolve Error: indirection requires pointer operand (‘int’ invalid)
- Defining a struct in flex error C++
- C++ Postfix calculator using stacks
- LNK1168: cannot open debug\file.exe for writing
- what does “error : a nonstatic member reference must be relative to a specific object” mean?
- Can I output a one channel image acquired from camera into a winAppi window?
- Difference in make_shared and normal shared_ptr in C++
- VC++ fatal error LNK1168: cannot open filename.exe for writing
- Reading integers from file and store them in array C++ [closed]
- Uninitialised value was created by a stack allocation
- C++ Image Processing Libraries
- error: passing ‘const …’ as ‘this’ argument of ‘…’ discards qualifiers
- Printing an array in C++?
- Resolve build errors due to circular dependency amongst classes
- error: “initializer expression list treated as compound expression”
- The compiler is complaining about my default parameters?
- read word by word from file in C++
- How to replace all occurrences of a character in string?
- Deleting an object in C++
- Appending a vector to a vector
- C++ Expression must have pointer-to-object type
- C++ “Access violation reading location” Error
- Getting error: ISO C++ forbids declaration of with no type
- IntelliSense: the object has type qualifiers that are not compatible with the member function
- codingbat-like site for C++
- Error: C2228: left of ” must have class/struct/union
- error C2679: binary ‘<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion)
- Multi-character constant warnings
- Error: vector does not name a type
- How to convert ASCII value into char in C++?
- How to alphabetically sort strings?
- Function definition not found for a function declared inside unnamed namespace – how to resolve? (Visual Studio 2015)
- How do I check if a C++ std::string starts with a certain string, and convert a substring to an int?
- How do I get the type of a variable?
- What is the meaning of a C++ Wrapper Class?
- terminate called after throwing an instance of ‘std::out_of_range’ what(): basic_string::substr
- error C2106: ‘=’ : left operand must be l-value
- Undefined reference to class constructor, including .cpp file fixes
- Why can’t we pass arrays to function by value?
- Iterator Loop vs index loop
- OpenCV Error: Assertion failed (size.width>0 && size.height>0) simple code
- multiple definitions error in c++ and solution to solve this issue
- How can I add reflection to a C++ application?
- Too many arguments to function
- not declared in this scope’ when using strlen()
- Converting a Cubemap into Equirectangular Panorama
- Why is this vector iterator not incrementable?
- glibc detected : double free or corruption
- What is the best way to develop a C++ web application?
- How to install Visual Studio Build Tools 2010 on Visual Studio 2015 Community?
- Throwing out of range exception in C++
- Is null reference possible?
- error LNK2019: unresolved external symbol _WinMain@16 referenced in function ___tmainCRTStartup
- Displaying contents of a vector container in C++
- Converting string to unsigned int returns the wrong result
- Visual c++ can’t open include file ‘iostream’
- c++ vector bubble sort
- compare and swap vs test and set
- Weighted random numbers
- compare and swap vs test and set
- Link error “undefined reference to `__gxx_personality_v0′” and g++ [duplicate]
- How to use bitmask?