Constructors

Hi,

I need some help understanding the basics of constructors, however, I cannot find any material concerning them on this site. Can someone please teach me the basics of constructors and how you use them? Thank you very much for all the help in advance!!!
or better yet, what does this error mean:

[Linker error] undefined reference to `CounterType::CounterType()'


Thanks for the help!
Constructors are like member functions for any class.You can find out if the member function is constructor by looking its name.The name of the constructor should be exactly as the name of the class.The other thing about constructor is that it should not return anything.
Last edited on
The linker error means that you have declared a constructor but you didn't give it a body:
1
2
3
4
5
class CounterType
{
    public:
        CounterType();//declaration but not body
};


If you go to http://www.cplusplus.com/doc/tutorial/classes/ and scroll down, you will see a section called 'constructors and destructors'
Last edited on
alright, well i kind of get it. I have made a program that compiles fine, but then it outputs weird answers...take a look:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <iostream>
#include <cstdlib>
using namespace std;

//Class for a counter:
class CounterType
{
public:
    CounterType(int count); //Allows the user to input their own value    
    CounterType(); //Sets the counter to zero
    int count_up(); //Adds one to the count
    int count_down(); //Subtracts one from the count
    void display(ostream& show); //Displays results to the user
private:
    int count;
};

int main( )
{
    CounterType counter1(2), counter2;
    
    
    cout << "counter1 initialized as follows: " << endl;
    counter1.display(cout);
    cout << "counter2 initialized as follows: " << endl;
    counter2.display(cout); 
    
    cin.get();
    return 0;
}

int CounterType::count_up()
{
    if (count < 0)
    {
        cout << "Illegal values for counter.";
        exit(1);
    }
    count += count;
}

CounterType::CounterType(): count(0)
{ /* Left blank intentionally to reset value to 0*/ }

int CounterType::count_down()
{
    if (count < 0)
    {
        cout << "Illegal values for counter.";
        exit(1);
    }
    count -= count;
}
CounterType::CounterType(int count)
{
     if (count < 0)
     {
        cout << "Illegal values for counter.";
        exit(1);
     }
     count = count;
}
void CounterType::display(ostream& show)
{
     show << "Current value of the counter is: " << count << endl;
}


I want this program to use every single member function and output to user, can someone help me out please?! Thank you!
You have the parameter of a constructor with the same name as a member ( count ).
This may cause unexpected results
Last edited on
line 15, you declare the member varialbe as 'count'. Then in all of your member functions, you also declare the passed parameter as 'count'. For example, see line 61:

count = count;

This basically does nothing. You're telling it to set 'count' to itself.

The problem is that there are multiple variables named 'count', so the compiler defaults to the most local version of 'count'. Here, this means you're changing the passed value 'count', and not the member variable 'count'.

The solution to this is to rename your member variable to something else. Some people like to put 'm_' before member variables to show that they're member vars. For example instead of calling your member var 'count' you could call it 'm_count', and then do:

m_count = count;

Alternatively, to distinguish between local and member var versions of count, you can use the 'this' pointer to indicate you want to use the member var:

this->count = count; // this would work

However that's a less prefereable solution, imo.
I changed everywhere you see "counter" to "term" instead, and that would not be the problem....

OK, so I'm initializing a value when in the first line of my main() function, when I say:

CounterType counter1(2), counter2; right? So why do I get a number in the 2 billions, rather than 2 like I initialized? Thanks again for the help!
Alright Disch, I changed all of those member variables to m_count, but I'm still getting a bad answer. What else could it be?
Nothing else looks wrong to me. Post updated code?
sure:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <iostream>
#include <cstdlib>
using namespace std;

class CounterType
{
public:
    CounterType(int count); //Allows the user to change initial starting count in main() function.    
    CounterType(); //Sets the counter to zero
    int count_up(); //Adds one to the count
    int count_down(); //Subtracts one from the count
    void display(ostream& show); //Displays results to the user
private:
    int count, m_count;
};

int main( )
{
    CounterType count1(2), count2;
    
    cout << "counter1 initialized as follows: " << endl;
    count1.display(cout);
    cout << "counter2 initialized as follows: " << endl;
    count2.display(cout); 
    
    cin.get();
    return 0;
}

int CounterType::count_up()
{
    if (count < 0)
    {
        cout << "Illegal values for counter.";
        exit(1);
    }
    m_count = count++;
}

CounterType::CounterType(): count(0)
{ /* Left blank intentionally to reset value to 0*/ }

int CounterType::count_down()
{
    if (count < 0)
    {
        cout << "Illegal values for counter.";
        exit(1);
    }
    m_count = count--;
}
CounterType::CounterType(int count)
{
     if (count < 0)
     {
        cout << "Illegal values for counter.";
        exit(1);
     }
     m_count = count;
}
void CounterType::display(ostream& show)
{
     show << "Current value of the counter is: " << count << endl;
}


Thanks again for the help!
I am assuming you are talking about bad value of count.
You are getting bad answer because in CounterType count1(2), count2 ;
For count1(2), Parameterized constructor is called. i.e. it calls

1
2
3
4
5
6
7
8
9
10
 
CounterType::CounterType(int count)
{
     if (count < 0)
     {
        cout << "Illegal values for counter.";
        exit(1);
     }
     m_count = count;
}


And in this constructor you are initializing m_count and not count. So count still has garbage value in it when you output count in display function.
right --- don't have count and m_count in your class. Just have one (probably keep m_count to avoid confusion with the local 'count's).

You're initializing m_count, but then you're printing count.
Last edited on
Topic archived. No new replies allowed.