complie time error.

When I declare a pointer to card (to test) in main
 
card *testtype


the below code compiles with no error.

but when I declare in main
 
card testtype


i get a compile time error.
I hope the mistake I am making is obvious.
I am going back to OOD programming off the cuff, and not following tutorials,
as I want to understand how to code in OOD better. Can someone break down the mistake I am making. E.g why it works with pointer, vs not.

Thanks in advance.


main
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
#include <iostream>

using namespace std;

#include "card.h"


int main(int argc, const char * argv[])
{
    // just creating a class instance for testing.
    // Not sure why it is failing.
    card test;

    for (int i = 0; i < 4; i++) {
        for (int k = 0; k < 13; k++)
        {
            switch (i+1) {
                case 1: cout << "c";
                    break;
                case 2: cout << "d";
                    break;
                case 3: cout << "h";
                    break;
                case 4: cout << "s";
                    break;
                    
                default:
                    break;
            }
             cout << " ";
            switch (k+1) {
                case 1: cout << "A" << endl;
                    break;
                case 11: cout << "J" << endl;
                    break;
                case 12: cout << "K" << endl;
                    break; 
                case 13: cout << "Q" << endl;
                    break; 
                default:
                    cout << k+1<< endl;
                    break;
            }
        }

    }

    
    return 0;
}


card.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#ifndef __ch1Ex4__card__
#define __ch1Ex4__card__

#include <iostream>


class card
{
public:
	card();
	~card();
    int getcard();
    bool setcard(int card);
    bool setsuit(char suit);
    char getsuit();

private:

    int card_value;
    char suit_type;
    
};

#endif /* defined(__ch1Ex4__card__) */ 


card.cpp
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
#include "card.h"

card::card()
{
    card_value = 0;
    suit_type = NULL;

}

int card::getcard()
{
   return card_value;
}
bool card::setcard(int card)
{
    card_value = card;
    return true;
}
bool card::setsuit(char suit)
{

    suit_type = suit;

    return true;
}
char card::getsuit()
{

    return suit_type;
}
http://ideone.com/iq2uUU

There are no compile-time errors. There are, however, link-time errors. You never defined your destructor.
Thank you for both corrections. Do you know why (without a destructor defined) why a pointer would compile?
Because you never called delete on the pointer, thus the compiler never looks for or cares about the destructor. If you never called new either, the compiler could care less about the constructor too.
Last edited on
Topic archived. No new replies allowed.