how to compile this?

i feel so dumb right now. what does it mean set() definition.

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
// array5.cpp - friend function
// This program will compile, but it won't link without the set() definition

#include <iostream>

using namespace std;

#define SIZE 10

class intArray {
private:
    int a[SIZE];
public:
    void setVal(int index, int value);
    friend void print(intArray);
};

void print(intArray x) {
    int i;
    for(i = 0; i < SIZE; i++)
        cout << "a[" << i << "] = " << x.a[i] << endl;
}

int main() {
    intArray num;
    int i;
    
    for(i = 0; i < SIZE; i++)
        num.setVal(i,i);
    print(num);
    return 0;
}
Hi,

You didn't specify what the setVal function does. You should do that in a file named intArray.cpp


Also, avoid using #define like you do on line 8, make it a const variable inside main instead. And pass it to what ever function needs it. So that means making a constructor for your class.
Last edited on
Topic archived. No new replies allowed.