getName
is not const, getScore
is not const, but print
is. Make the first two const like print
. You cannot call a non-const method with a const object. Since your Person objects are direct members of your other class and since you are in a const method they are considered const.
In general you should consider every method you write and declare it const if that’s what it is. Simple getters like getScore
and getName
should always be const.
Related Posts:
- push_back vs emplace_back
- C++ Fatal Error LNK1120: 1 unresolved externals
- Visual Studio debugger error: Unable to start program Specified file cannot be found
- push_back vs emplace_back
- General way of solving Error: Stack around the variable ‘x’ was corrupted
- Already defined in .obj – no double inclusions
- Error “system” is ambiguous?
- What is the return type of sizeof operator?
- error C2065: ‘cout’ : undeclared identifier
- How to install Visual Studio Build Tools 2010 on Visual Studio 2015 Community?
- error C2039: ‘string’ : is not a member of ‘std’, header file problem
- system(“pause”); – Why is it wrong?
- What is the best way to use a HashMap in C++?
- Sleep for milliseconds
- What is an undefined reference/unresolved external symbol error and how do I fix it?
- How to throw a C++ exception
- How to convert a char array to a string?
- How to fix a “invalid operands to binary expression” error?
- Is it possible to decompile a C++ executable file
- Difference between `constexpr` and `const`
- C++ Singleton design pattern
- How to correctly use Boolean functions?
- C++ Error: “Expression must have integral or enum type” [duplicate]
- c++ compile error: ISO C++ forbids comparison between pointer and integer
- How do I declare a 2d array in C++ using new?
- What does “#include
” do? - How to convert an instance of std::string to lower case
- Error: No viable overloaded =
- Random number between 1 to 10 using C++
- Expected initializer before namespace
- A proper way to create a matrix in c++
- No matching member function for call to ‘push_back’ error
- Use of “this” keyword in C++
- Why am I getting “undefined reference to main”
- Compiling C++11 with g++
- Setting an int to Infinity in C++
- What exactly is std::atomic?
- Separating class code into a header and cpp file
- Deep copy vs Shallow Copy
- Pointer to incomplete class type is not allowed
- C++ error: “Array must be initialized with a brace enclosed initializer”
- Why am I getting string does not name a type Error?
- Sorting Characters Of A C++ String
- Sentinel while loop for C++
- How to ensure that a std::map is ordered?
- How to write C++ getters and setters
- std::out_of_range error?
- Checking cin input stream produces an integer
- Where do “pure virtual function call” crashes come from?
- C++ wait for user input
- non-standard syntax; use ‘&’ to create a pointer to member
- How to shuffle a std::vector?
- Understanding glm::lookAt()
- How to get current timestamp in milliseconds since 1970 just the way Java gets
- What is the best open XML parser for C++?
- *** No rule to make target ‘class.cpp’, needed by `build/….x86/class.o` Stop. error in Ubuntu
- What’s the difference between opening a file with ios::binary or ios::out or both?
- Hash function for a string
- CMake error at CMakeLists.txt:30 (project): No CMAKE_C_COMPILER could be found
- How to run valgrind with basic c example?
- initial value of reference to non-const must be an lvalue
- Typedef function pointer?
- cc1plus: error: unrecognized command line option “-std=c++11” with g++
- cannot declare variable ‘’ to be of abstract type ‘’
- error: strcpy was not declared in this scope
- What is the most popular general purpose C++ framework?
- Adding message to assert
- c++ error: invalid types ‘int[int]’ for array subscript
- What are the different versions of exec used for in C and C++?
- C++ – include unistd.h: why not cunistd?
- Dynamically allocated string array, then change it’s value?
- Use the auto keyword in C++ STL
- c++ –
- reading from stdin in c++
- Difference between pic Vs pie
- C++ Templates – LinkedList
- In C++ can constructor and destructor be inline functions?
- What’s the meaning of exception code “EXC_I386_GPFLT”?
- how to convert from int to char*?
- When is it safe to call this-> in constructor and destructor
- Stray ‘\342’ in C++ program
- How to automatically convert strongly typed enum into int?
- Function cannot be referenced as it is a deleted function
- Difference between
and - How to get size of dynamic array in C++ [duplicate]
- What is a nested name specifier?
- ASCII art in C++
- How do you append an int to a string in C++?
- C++ correct way to return pointer to array from function
- Understanding error “terminate called after throwing an instance of ‘std::length_error’ what(): basic_string::_S_create Aborted (core dumped)”
- how to find 2d array size in c++
- Getting error “array bound is not an integer constant before ‘]’ token”
- error: expected unqualified-id before ‘while’|
- cmath vs math.h (And similar c-prefixed vs .h extension headers)
- Proper way to pass dynamic arrays to other functions
- How to fix ‘std::logic_error’ what(): basic_string::_M_construct null not valid error?
- Unexpected end of file error
- virtual memory exhausted: Cannot allocate memory
- How to find and replace string?
- allocating an object of abstract class type error