The expression must be of class type.

I haven't been coding in c ++ for some time and got stuck when I tried to compile this simple snippet:

class A { public: void f() {} }; int main() { A a1; a1.f(); // works fine A *a2 = new A(); a2.f(); // this doesn't } 
+66
c ++ new-operator class
Jul 01 '11 at 11:52
source share
4 answers

This is a pointer, so try instead:

 a->f(); 

Mostly an operator . (used to access the fields and methods of an object) is used for objects and references, therefore:

 A a; af(); A& ref = a; ref.f(); 

If you have a pointer type, you must first dereference it to get a link:

 A* ptr = new A(); (*ptr).f(); ptr->f(); 

a->b usually short for (*a).b .

Smart Pointers Note

operator-> can be overloaded, which is especially used by smart pointers. When you use smart pointers , you also use -> to reference the specified object:

 auto ptr = make_unique<A>(); ptr->f(); 
+136
Jul 01 2018-11-11T00:
source share

Allow analysis.

 #include <iostream> // not #include "iostream" using namespace std; // in this case okay, but never do that in header files class A { public: void f() { cout<<"f()\n"; } }; int main() { /* // A a; //this works A *a = new A(); //this doesn't af(); // "f has not been declared" */ // below // system("pause"); <-- Don't do this. It is non-portable code. I guess your // teacher told you this? // Better: In your IDE there is prolly an option somewhere // to not close the terminal/console-window. // If you compile on a CLI, it is not needed at all. } 

As a general tip:

 0) Prefer automatic variables int a; MyClass myInstance; std::vector<int> myIntVector; 1) If you need data sharing on big objects down the call hierarchy, prefer references: void foo (std::vector<int> const &input) {...} void bar () { std::vector<int> something; ... foo (something); } 2) If you need data sharing up the call hierarchy, prefer smart-pointers that automatically manage deletion and reference counting. 3) If you need an array, use std::vector<> instead in most cases. std::vector<> is ought to be the one default container. 4) I've yet to find a good reason for blank pointers. -> Hard to get right exception safe class Foo { Foo () : a(new int[512]), b(new int[512]) {} ~Foo() { delete [] b; delete [] a; } }; -> if the second new[] fails, Foo leaks memory, because the destructor is never called. Avoid this easily by using one of the standard containers, like std::vector, or smart-pointers. 

As a rule: if you need to manage your memory yourself, there is usually a supervisor manager or alternative that already exists according to the RAII principle.

+13
Jul 01 '11 at 15:36
source share

Summary : instead of af(); must be a->f();

In the main, you defined A as a pointer to an object , so that you can access functions using the -> operator.

An alternative but less readable way is (*a).f()

af() could be used to access f () if a were declared as: A a;

+8
Jul 01 2018-11-11T00:
source share

a is a pointer. You need to use -> , not .

+6
Jul 01 2018-11-11T00:
source share



All Articles