0

I made a class in a .cpp and a .h file with in the same project, and then I tried to make a calculating code. I made an object for it, with the name "co". I then tried to build it and run the file, and it showed nothing. Why is that happening and how can I try to fix it? The code:

main.cpp

#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;

int main()
{
  calculatorClass co ();

}



calculatorClass.cpp

#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
calculatorClass :: calculatorClass ()
{

int x = 25;
int y = 37;
int z = 51;
int a = 14;
int b = 63;
int c = 75;
cout << x * y * z * a * b * c ;

}

calculatorClass.h

#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H


class calculatorClass
{
    public:
        calculatorClass (int hello);

        calculatorClass();

    protected:

    private:
};

#endif // CALCULATORCLASS_H

Thanks!

Geno C
  • 1,401
  • 3
  • 11
  • 26
  • 6
    `calculatorClass co ();` is a declaration of a function named `co` that takes no parameters and returns a `calculatorClass`. No instance of `calculatorClass` is created in your program. Make it `calculatorClass co;` – Igor Tandetnik Jul 12 '20 at 03:55
  • 2
    Related: [https://www.fluentcpp.com/2018/01/30/most-vexing-parse/](https://www.fluentcpp.com/2018/01/30/most-vexing-parse/) – drescherjm Jul 12 '20 at 04:01
  • 1
    Your code uses class contructors as if they are functions. Just write a function if that is what you want, there is no point in using a class in the way that you are. – john Jul 12 '20 at 05:35
  • @john, I changed it a bit maybe he is just trying to understand constructors better. – Geno C Jul 12 '20 at 05:40

3 Answers3

0

It looks like you are trying to create a constructor.

So the first thing to do is look at your calculatorClass.h. You need to change it to look like this because a constructor is a member function that has the same name as the class.

#ifndef CALCULATORCLASS_H
#define CALCULATORCLASS_H


class calculatorClass
{
public:
    calculatorClass(); //Constructor.

protected:

private:
};
#endif // CALCULATORCLASS_H

Then in your calculatorClass.cpp you will need to change it to this.The function header of a constructor's external definitions takes a form like this:

Classname::Classname(Parameters)

#include "calculatorClass.h"
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
   calculatorClass::calculatorClass() // Changed.
{

    int x = 25;
    int y = 37;
    int z = 51;
    int a = 14;
    int b = 63;
    int c = 75;
    cout << x * y * z * a * b * c;

}

Then in your main.cpp file you are going to define an instance of the class calculatorClass which I did and named it calc. Now, the function calculatorClass is automatically called in main().

#include <iostream>
#include <fstream>
#include <string>
#include "CalculatorClass.h"
using namespace std;

int main()
{
    calculatorClass Calc; // Define a calculatorClass object
    return 0;
}

Just a heads up, when you use ints they only hold 32 bits. So with all these large numbers you are trying to multiplying will roll over to a large negative value and you will not get the correct answer.

You might want to use long long int.

underscore_d
  • 6,309
  • 3
  • 38
  • 64
Geno C
  • 1,401
  • 3
  • 11
  • 26
  • 1
    You should include explanations of what you changed and why those changes were needed. – underscore_d Jul 12 '20 at 09:16
  • @Genoc You don't necessarily invoke the ctor by `calculatorClass Calc;` If you run valgrind on such a code, the object may appear uninitialized! – Kerek Jul 19 '20 at 15:48
0

change calculatorClass co (); to calculatorClass co. This will work. The explanation is as the following question - C++: warning: C4930: prototyped function not called (was a variable definition intended?).

By calling calculatorClass co (); you are not creating an object; you are declaring a function.

0

The main issue in the code, that the following line is interpreted differently than you think:

calculatorClass calc();

This line is interpreted as a declaration of a function prototype instead of creating a new object of calculatorClass. This is known as the most vexing parse as noted in the comments.

There is a way to declare and initialize an object in a similar fashion:

calculatorClass calc{};

Notice that I have used {} and not () ! This was introduced in order to avoid this vexing parsing of the code.

In addition, I want to comment about some other bad habits in the code:

  1. Using using namespace std; is a bad habit, which causes name pollution and may cause bugs with name ambiguity, and in the worst case related to unexpected functions being called!
  2. Class names always start with a capital letter - calculatorClass should be Calculator (we know that it is a class, thus class shouldn't appear in the name!).
  3. There is no need in the protected and private modifiers here.
Kerek
  • 1,106
  • 1
  • 8
  • 18