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?

Recommended Answers

All 3 Replies

What are you naming the class? passArr <whats my name yo>

In otherwords...
passArry Obj1 (x);
Obj1.giveNumber();


Just like an when you create an int...

Type name
int counter

Well I feel stupid... Thank you so much, I think that I just stared at the code to long and missed that.

np dude, sometimes you just need another set of eyes.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.