Expression must have class type
It's a pointer, so instead try:
a->f();
Basically the operator .
(used to access an object's fields and methods) is used on objects and references, so:
A a;
a.f();
A& ref = a;
ref.f();
If you have a pointer type, you have to dereference it first to obtain a reference:
A* ptr = new A();
(*ptr).f();
ptr->f();
The a->b
notation is usually just a shorthand for (*a).b
.
A note on smart pointers
The operator->
can be overloaded, which is notably used by smart pointers. When you're using smart pointers, then you also use ->
to refer to the pointed object:
auto ptr = make_unique<A>();
ptr->f();
Why am I getting expression must have class type error?
You are accessing the pointer incorrectly. Accessing inner variables from a pointer require ->
instead of the .
operator.
Switch your code:
Student* student_ptr = new Student;
student_ptr->name = "Tom";
student_ptr->id = 1;
Alternatively, if you really want to use the .
operator, you can do:
Student* student_ptr = new Student;
(*student_ptr).name = "Tom";
(*student_ptr).id = 1; // the more you know
error: expression must have a class type
Most vexing parse :
This line:
Worker myWorker();
declares a function taking no parameters and returning a Worker
.
Simply declare your object with :
Worker myWorker;
What is Expression must have class type
It's probably because this
is a pointer and to try to access it using .
.
Moreover malloc returns a void*
so you must cast
Try writing this instead :
this->ptr = static_cast<int *>(std::malloc(4));
Error - expression must have a class type - c++
The line
LinkedList<Animal> aL();
is not creating an instance of a LinkedList<Animal>
, but is declaring a function aL
with no arguments and LinkedList<Animal>
as its return type.
To create an object using the default constructor, just don't use any braces:
LinkedList<Animal> aL;
Alternatively, if you wish to be that explicit, you can use curly braces from C++11
LinkedList<Animal> aL{};
Or, if you like function call syntax that much, you can
LinkedList<Animal> aL = LinkedList<Animal>();
or even
auto aL = LinkedList<Animal>();
Expression must have a class type - C++/CLI
The error message is a bit misleading, in a way, because it implies that the problem is with the actual listviewX
and listviewY
variables (which, as you have pointed out, are 'hatted' class types, or handles).
However, the SubItems
member of the ListViewItem
class is an array of class handles (not raw classes), so you need the ->
operator on the two occurrences of that, too (rather than the .
operator).
This is what you need the code to be:
compareResult = ObjectCompare->Compare(listviewX->SubItems[ColumnToSort]->Text,
listviewY->SubItems[ColumnToSort]->Text);
Expression must have class type but it has type *shape
If input_shape_pointer
is a pointer to a shape
then use ->
instead of .
. For example, the expression
input_shape_pointer.get_dimensions()
should be replaced by:
input_shape_pointer->get_dimensions()
Note the use of ->
instead of .
in the above expression
The expression must have a class type
Since C++17 (and to a lesser extent 14) we can use std::filesystem
(std::experimental::filesystem
in C++14) to manipulate files and create directories.
For example in your case:
...
std::filesystem::path DDDirectory("C:\\dnGames\\DuelistsDance");
try {
std::filesystem::create_directories(DDDirectory); // Creates all the directories needed, like mkdir -p on linux
// Success here
} catch(std::filesystem::filesystem_error& e) {
// Handle errors here
}
This will make handling of your errors cleaner and your code cross-platform (although you will have to change the path, but std::filesystem::path
turns /
into \\
on windows anyway). It also makes your code easier to read and as you can see, much much shorter.
Related Topics
Passing a C++ Complex Array to C
Error: 'I' Does Not Name a Type with Auto
How to Guarantee Fast Shutdown of My Win32 App
#Define Sqr(X) X*X. Unexpected Answer
Protocol Buffer Over Socket in C++
Generate Dependencies for a Makefile for a Project in C/C++
Error When Using In-Class Initialization of Non-Static Data Member and Nested Class Constructor
Read Func Interp of a Z3 Array from the Z3 Model
Is There a Proper 'Ownership-In-A-Package' for 'Handles' Available
Why Is the Data Type Needed in Pointer Declarations
What Are the Rules Governing C++ Single and Double Precision Mixed Calculations
How to Know the Right Max Size of Vector? Max_Size()? But No
The Difference Between C and C++ Regarding the ++ Operator