• Register
1 vote
1.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

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,240 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 216 views
216 views
How c++ expression can have integral or unscoped type?
asked Sep 22 Daniel Anderson 4k points
0 votes
1 answer 2.2K views
2.2K 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 135 views
135 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 Gavin 15.3k points
1 vote
1 answer 38 views
38 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 Gavin 15.3k points
0 votes
1 answer 25 views
25 views
Problem: I have been working on classes in C++ for about a few days. I am learning constructors with parameters. I was trying to run the following program in which I have declared two constructors, one without parameters and other with one parameter. The problem ... The program works perfectly when I comment out the error-generating statement. I have no idea what I am doing wrong. Please Help.
asked Nov 9 Sheeza 3.5k points