Multiple Definition Error with Operator[] ??

So i'm messing around with the operator[] since prof wasn't very clear on it but I get a build error for multiple definitions. I code mainly on cLion, I tested it on the latest Eclipse too same error. TO BE CLEAR! I get this error only when the function def is outside with the prototype inside the class, so if I define the function inside the class I don't get the error. My class code is as follows.




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

#ifndef UNTITLEDSCHOOOL_MYCLASS_H
#define UNTITLEDSCHOOOL_MYCLASS_H

#include <iostream>

using namespace std;


int const SIZE = 10;

class myClass {

private:

    int values[SIZE];
    int dummy;

public:

    myClass() {

        for (int i = 0; i < SIZE; i++) {
            values[i] = 0;
        }
        dummy = 0;

    }


    int operator[](int index) const {

        if (index < 0 || index >= SIZE) {

            return 0;
        }

        return values[index]; // returns the requested value;

    } // end of getter Operator; // getter

    int &operator[](int index) {

        if (index < 0) {

            return dummy;

        } else if (index >= 10) {
            return dummy;

        } else {
            return values[index];
        }
    }
};

#endif //UNTITLEDSCHOOOL_MYCLASS_H


> I get this error only when the function def is outside with the prototype inside the class,
> so if I define the function inside the class I don't get the error.

When the function definition is outside the class, make sure that you have not omitted the const qualifier.
int myClass::operator[](int index) const { ...
If you define myClass::operator[] outside the class body in a header file and then include that header in two or more source files then you will get a Multiple Definition Error if you have forgotten to mark the method as inline. That is:

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
#ifndef UNTITLEDSCHOOOL_MYCLASS_H
#define UNTITLEDSCHOOOL_MYCLASS_H

#include <iostream>

using namespace std; // NOTE very bad practice to use using in a header!
// Esp when it's not even needed!?

int const SIZE = 10;

class myClass {
private:
    int values[SIZE];
    int dummy;

public:
    myClass() {
        for (int i = 0; i < SIZE; i++) {
            values[i] = 0;
        }
        dummy = 0;
    }

    int operator[](int index) const;
    int &operator[](int index);
};

inline int myClass::operator[](int index) const {
    if (index < 0 || index >= SIZE) {
        return 0;
    }
    return values[index]; // returns the requested value;
} // end of getter Operator; // getter

inline int &myClass::operator[](int index) {
    if (index < 0) {
        return dummy;
    } else if (index >= 10) {
        return dummy;
    } else {
        return values[index];
    }
}

#endif //UNTITLEDSCHOOOL_MYCLASS_H 


Alternatively define the functions in a single cpp file (but inline is better in the case, as the methods are tiny.)

Andy

PS methods defined within the class body are implicitly inline.



Last edited on
Topic archived. No new replies allowed.