So I've been trying to make a test program that shows the difference between passing an object by reference, and by value.
I'd like the constructor of ClassB to take in two arguments: a reference to ClassA, and the value of ClassA.
I've been able to run the program when the constructor only takes in the reference. However, when I added the second parameter (for value of ClassA), I get the error "no matching function for call to 'ClassA::ClassA()'".
#include <iostream>
using namespace std;
class ClassA{
int num;
public:
ClassA(int _num){
num = _num;
}
int getNum(void){
return num;
}
void addToNum(int value){
num += value;
}
};
class ClassB {
ClassA * classPointer;
ClassA classValue;
public:
ClassB(ClassA& referencedClass, ClassA value){ // HERE I get the error
classPointer = &referencedClass;
classValue = value;
}
int getPointedNum(void){
return classPointer->getNum();
}
int getValueNum(void){
return classValue.getNum();
}
};
int main(int argc, char *argv[]) {
ClassA classA (20);
ClassB classB (classA, classA);
classA.addToNum(5);
cout << "classB.getPointedNum(): " << classB.getPointedNum() << endl;
cout << "classB.getValueNum(): " << classB.getValueNum() << endl;
return 0;
}
Does anybody have some idea of what's going on here?
Thanks.