Hello, I'm new to c++ and we're just starting classes and objects in class. I've done a bit of programming in Java and I'm struggling to initialize an object using a variable. I must be missing something fundamental but I can't for the life of me figure it out. Here's the code giving me troubles.
Header File
class passArr
{
public:
passArr(int number);
int giveNumber();
};
Class
#include "passArr.h"
int number;
passArr::passArr(int num)
{
number = num;
}
int passArr::giveNumber()
{
return number;
}
Client
#include "passArr.h";
#include <iostream>
using namespace std;
int main()
{
int x = 1;
passArr(x);
cout << passArr.giveNumber();
}
It's telling me that no default constructor exists, but as far as I know I shouldn't need a default constructor because I'm using the constructor I provided. If I were to replace passArr(x) with passArr(1) everything works fine. Is there a way to pass variables to the constructor?