• Register
1 vote
2.1k views

Problem:

I've got a problem with c++ code. I am attaching the sample code snippet below. Please pull me up. Yee, of course, I am pretty new  in C++

#include <iostream>
using namespace std;

class Myclass
{
  public:
    void myfunc() {
      cout<<"Hello World"<<endl;
    }
};

int main()
{
  // A pointer to our class is created. 
  Myclass *a = new Myclass();
  // myfunc is part of the class object, rather than
  // part of the pointer to that class' object.
  a.myfunc(); 
}

 

12 7 7
15,250 points

Please log in or register to answer this question.

2 Answers

1 vote
 
Best answer

Solution:

The code snippet you attached is tried to apply the . operator on a pointer to an object, rather than on an object itself. That is the reason the code throws an "expression must have class type"​ error.

You should write this way:

 

#include <iostream>
using namespace std;

class Myclass
{
  public:
    void myfunc() {
      cout<<"Hello World"<<endl;
    }
};

int main()
{
  // A class object of Myclass is created. 
  Myclass a ;
  // The . operator is used to access the class' methods.
  a.myfunc(); 
}

I hope you get it.

13 9 6
94,260 points
0 votes

Solution:

The “expression should have class type” is an error that is thrown at the time the . operator, which is generally applied to access an object’s fields and methods, is used on pointers to objects.

At the time applied on a pointer to an object, the . operator will attempt to trace the pointer’s fields or systems, however it won’t, since they do not subsist. These fields or methods are part of an object, more than a part of the pointer to an object.

Wrong code

The code below attempts to imbed the . operator on a pointer to an object, more than on an object itself. The code throws an "expression should have class type"​ error.

using namespace std;
class Myclass
{
  public:
    void myfunc() {
      cout<<"Hello World"<<endl;
    }
};
int main()
{
  // A pointer to our class is created. 
  Myclass *a = new Myclass();
  // myfunc is part of the class object, rather than
  // part of the pointer to that class' object.
  a.myfunc(); 
}#include <iostream>

Correct code

The code below accurately embeds the . operator on an object to access the object’s member functions. Because a is now a class object, the code does not throw any error.

#include <iostream>
using namespace std;
class Myclass
{
  public:
    void myfunc() {
      cout<<"Hello World"<<endl;
    }
};
int main()
{
  // A class object of Myclass is created. 
  Myclass a ;
  // The . operator is used to access the class' methods.
  a.myfunc(); 
}

 

10 6 4
31,120 points

Related questions

0 votes
1 answer 20 views
20 views
Problem: I need your help , any solution: c++ expression must have class type
asked Mar 14 Wafa Abu Yousef 6.1k points
0 votes
1 answer 1.2K views
1.2K views
How c++ expression can have integral or unscoped type?
asked Sep 22, 2020 Daniel Anderson 4k points
0 votes
1 answer 119 views
119 views
Problem: I have't coded in c++ for some time and I got stuck when I tried to compile this simple snippet: class A { public: void f() {} }; int main() { { A a; a.f(); // works fine } { A *a = new A(); a.f(); // this doesn't } }
asked Dec 26, 2020 Mashhoodch 13k points
0 votes
1 answer 76 views
76 views asked Jan 28 khanboy 2.1k points
1 vote
1 answer 28 views
28 views
Problem: I&rsquo;m getting this error when running my program : expression must have integral or unscoped enum type Here&rsquo;s my program: #include <iostream> #include <iomanip> #include <string> #include <algorithm> #include <sstream> using namespace std; int main(){ float size; float ... array size from the user but this (float *temp = new float[size];) gives me error . How can I solve this ?
asked Apr 29 Raj Tyagi 15.6k points
0 votes
1 answer 95 views
95 views
Problem : How can I resolve this issue: Expression preceding parentheses of apparent call must have (pointer-to-) function type.
asked Mar 6 Wafa Abu Yousef 6.1k points
0 votes
1 answer 5K views
5K views
Problem : I am very new to C++ and trying to learning C++ template. Please find below my code, I am trying to define a template class and also want to call the member function in the main() method.. template <typename T> class Calculate { T _x; T ... the last line of above code : Expression preceding parentheses of apparent call must have (pointer-to-) function type How can I resolve this issue?
asked Dec 3, 2019 alecxe 7.5k points
1 vote
2 answers 551 views
551 views
Problem: Hello all, How to declare an array properly in c/c++? I am a new student of c++ and I am trying to declare two-dimensional arrays in my program. Unfortunately, my arrays are not working. Here is my sample code int row = 5; int col= 5; int [row][col]; The above codes throw an error expression must have a constant value c++. What&rsquo;s wrong with it?
asked Apr 7, 2020 Gavin 15.3k points
0 votes
1 answer 29 views
29 views
Problem: I have this following code: int M = 3; int C = 5; int match = 3; for ( int k =0; k < C; k ++ ) { match --; if ( match == 0 && k = M ) { std::cout << " equals" << std::endl; } } But it gives out an error saying: Error: expression must be a ... trying to modify "match" or "k" value here, but why this error? if I only write it like: if ( match == 0 ) it is ok. Could someone explain it to me?
asked Dec 23, 2020 Mashhoodch 13k points
1 vote
1 answer 78 views
78 views
Problem: I have a sample c++ code below that throws me back an error: &ldquo;c++ expression must be a modifiable value&rdquo; What could be the reason? int A = 3; int B = 5; int match = 3; for ( int k =0; k < B; k ++ ) { match --; if ( match == 0 && k = A ) { std::cout << " equals" << std::endl; } } Thanks for your help.
asked Mar 10, 2020 Gavin 15.3k points