I have this class:
class CContact {
public:
CTimeStamp m_Stamp;
int m_Num1;
int m_Num2;
CContact(CTimeStamp mStamp, int i, int i1) {
m_Stamp = mStamp;
m_Num1 = i;
m_Num2 = i1;
}
};
I get the following error:
Constructor for 'CContact' must explicitly initialize the member 'm_Stamp' which does not have a default constructor
I want to be able to use it this way:
test.addContact(CContact(CTimeStamp(1, 2, 3, 4, 5, 6), 999999999, 777777777));
What does it mean, and how can I fix it?
The error is self-explantory. Your CContact constructor is not passing any values to m_Stamp's constructor, so the compiler has to default-construct m_Stamp, but it can't because CTimeStamp does not have a default constructor.
You need to initialize CContact's members (or at least m_Stamp) in the CContact constructor's member initialization list rather than in the constructor's body, eg:
CContact(CTimeStamp mStamp, int i, int i1) :
m_Stamp(mStamp),
m_Num1(i),
m_Num2(i1)
{
}
This will invoke the copy constructor for m_Stamp rather than the default constructor.
You original code was effective equivalent to this, which is why it failed:
CContact(CTimeStamp mStamp, int i, int i1)
: m_Stamp() // <-- here
{
m_Stamp = mStamp;
m_Num1 = i;
m_Num2 = i1;
}
You have to define a default constructor for class CTimeStamp. For example:
CTimeStamp(int aa=0, int bb=0, int cc=0)
:a{aa},b{bb},c{cc}{}
Related
How to initialize the array-like member variable?
The visual studio code says:
no matching function for call to 'Node::Node()' gcc line 12 col 9
const int N = 100;
struct Node {
int val, ch[2];
/**
void init(int _val) {
this->val = _val, this->ch[0] = this->ch[1] = 0;
}*/
Node (int _val): val(_val) {
this->ch[0] = this->ch[1] = 0;
}
} tree[N]; // <--------- this is line 12
int main() {
int a;
cin >> a;
tree[0] = Node(a);
}
The problem is that when you wrote tree[N] you're creating an array whose elements will be default constructed but since there is no default constructor for your class Node, we get the mentioned error.
Also, Node doesn't have a default constructor because you've provided a converting constructor Node::Node(int) so that the compiler will not automatically synthesize the default ctor Note::Node().
To solve this you can add a default ctor Node::Node() for your class.
How to initialize variable(s) dynamically i.e. being not as constant, in class declaration which will be invoked/used without need of object instantiation such as a constructor for member function call immediately gets returned in a function ?
There must, side by side of that variable(s) in that class, be a constant member.
enum Value :int { NO,LOW,MED,HIGH };
template<Value V> class valuation {
public:
valuation(const ints) : pos(s) {};
valuation(int a, int b, int c) : j(a), k(b), l(c) {};
value() {
if (!V)
return (j+k+l) * pos;
}
private:
const int pos;
int j,k,l;
};
int a_function(int s) {
return valuation<NO>(s).value(); // no need object at all
}
int main() {
// somehow in run-time j, k, l has been given 1,2,3, or other valid values
int v = a_function(7) // e.g.
}
Thanks in advance
I think you want to declare your value method -- or at least a version of it -- as a static member. This will allow you to call the method without instantiating an object from your class.
My constructor is suppose to only take one variable. But I'm curious if you ca initialize other variables that arent in the constructor defintion.
class WordAnalysis{
private:
int timesDoubled;
word *words;
int wordCount;
int index;
void doubleArrayAndAdd(string);
bool checkIfCommonWord(string);
void sortData();
public:
bool readDataFile(char*); //returns an error if file not opened
int getWordCount();
int getUniqueWordCount();
int getArrayDoubling();
void printCommonWords(int);
void printResult(int);
WordAnalysis(int);
~WordAnalysis();
};
Example: Would any instance of WordAnalysis now have timesdoubled as 0. and would a getter function be able to get this information without a setter?
WordAnalysis::WordAnalysis(int arrSize){
wordCount = arrSize;
int timesDoubled = 0;
int index = 0;
}
Well yes, you can initialize other member variables in the constructor,
even if it doesn't take the corresponding arguments.
However, in the example you gave above:
WordAnalysis::WordAnalysis(int arrSize){
wordCount = arrSize;
int timesDoubled = 0;
int index = 0;
}
You aren't actually initializing the timesDoubled member variables, because you wrote "int" before it, which is declaring a new variable and setting that to 0.
If you want to set the classes timesDoubled variable you have to write:
timesDoubled = 0;
Or if you want to be more explicit about it, you can even write:
WordAnalysis::timesDoubled = 0;
Yes. You can. But, you can do in-class initialization of your data members on declaration. You should use initializer list with constructor to initialize your required data members. All the data members are visible inside the constructor. You can assign their values in it. Technically, using initializer list is initialization and inside the ctor it is assignment when the assignment operator (=) is used.
Here's is the snippet of your code with comments:
class WordAnalysis
{
private:
// Data member initialization on declaration
int timesDoubled { 0 };
word* words { nullptr };
int wordCount { 0 };
int index { 0 };
public:
// Initializing timesDoubled using initializer list
WordAnalysis( const int t ) : timesDoubled{ t }
{
// Assign default values here if need be
index = 10; // assignment
}
// ...
};
Your compiler should be at least C++11 compliant to allow the in-class initializations of data members.
I suggest defining a default constructor such as:
WordAnalysis()
{
timesDoubled = 0;
words[0] = '\0'; //assuming it's an array of char
wordCount = 0;
index = 0;
}
That way all instances of the class would be initialized.
In the following code
class someClassB;
class someClassA
{
public:
someClassA(int x, int y);
private:
someClassB* B;
};
class someClassB
{
public:
someClassB(int x, int y);
private:
int x;
int y;
someClassA A;
};
someClassA::someClassA(int i, int j)
{
B->x = i;
B->y = j;
}
someClassB::someClassB(int i, int j)
{
x = i;
y = j;
A = new someClassA(i, j);
}
why do I get an error saying 'Constructor for 'someClassB' must explicitly initialize the member 'A' which does not have a default constructor'?
Am I not initializing 'A' in someClassB's constructor?
someClassA does not have a default constructor. Currently, someClassB needs to default initialize it, resulting in the compilation error you quote. .You need to explicitly initialize the someClassA data member using one of its available constructors. For example,
someClassB::someClassB(int i, int j) : x(i), y(j), A(i, j)
{
}
Here, x and y are also initialized in the constructor initialization list, as opposed to default initialized and then assigned values to, as in your code.
Also note that, in general, this makes no sense in C++:
A = new someClassA(i, j);
new returns a pointer.
Given that you are using A with new, I suspect you meant to declare it as a pointer:
someClassA* A;
If not, then you need to initialize it in someClassB's initializer list.
someClassA::someClassA(int i, int j)
{
B->x = i;
B->y = j;
}
create a new instance of class someClassB as B is a pointer and you didn't allocated memory for this.
A = new someClassA(i, j);
A is not a pointer this is an instance of someClassA
I have a class and I want to have some bit masks with values 0,1,3,7,15,...
So essentially i want to declare an array of constant int's such as:
class A{
const int masks[] = {0,1,3,5,7,....}
}
but the compiler will always complain.
I tried:
static const int masks[] = {0,1...}
static const int masks[9]; // then initializing inside the constructor
Any idea on how this can be done?
Thanks!
class A {
static const int masks[];
};
const int A::masks[] = { 1, 2, 3, 4, ... };
You may want to fixate the array within the class definition already, but you don't have to. The array will have a complete type at the point of definition (which is to keep within the .cpp file, not in the header) where it can deduce the size from the initializer.
// in the .h file
class A {
static int const masks[];
};
// in the .cpp file
int const A::masks[] = {0,1,3,5,7};
enum Masks {A=0,B=1,c=3,d=5,e=7};
you can initialize variables only in the constructor or other methods.
'static' variables must be initialized out of the class definition.
You can do this:
class A {
static const int masks[];
};
const int A::masks[] = { 1, 2, 3, 4, .... };
Well, This is because you can't initialize a private member without calling a method.
I always use Member Initialization Lists to do so for const and static data members.
If you don't know what Member Initializer Lists are ,They are just what you want.
Look at this code:
class foo
{
int const b[2];
int a;
foo(): b{2,3}, a(5) //initializes Data Member
{
//Other Code
}
}
Also GCC has this cool extension:
const int a[] = { [0] = 1, [5] = 5 }; // initializes element 0 to 1, and element 5 to 5. Every other elements to 0.