Vector containing object

Hi,
Trying out an online tutorial, and then changing it to suit me.
I finally got through all the syntax issues, only to be greeted with a lovely Linker error at compile time.

Can anyone see why?
The Linker seems to think it has something to do with the printVector function.
I can't for the life of me see what is wrong.

Any help would be appreciated.

main.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
  // VECOBJ - Practice at creating a vector that contains an object class.

#include <iostream>
#include <vector>
#include "vecobj.h"

using namespace std;

void fillVector(vector<Bunch>&);
// fillVector - Generate numbers
// @param - vector<Bunch> - 3 numbers per object.

void printVector(const vector<Bunch>&);
// printVector - Print out the contents of the vector
// @param - vector<Bunch>

int main()
{
    vector<Bunch> newList;
    fillVector(newList);
    printVector(newList);
}

void fillVector(vector<Bunch>& newList)
{
    unsigned int numBunches = 20;

    for(unsigned int i = 0; i < numBunches; i++)
    {
        int fnum = (rand() % 100) + (rand() % 3); 
        int Snum = (rand() % 100) + (rand() % 3);
        int Tnum = (rand() % 100) + (rand() % 3);

        Bunch quickbunch(fnum, Snum, Tnum);

        newList.push_back(quickbunch);
    }

}

void printVector(vector<Bunch>& newList)
{
    int numBunches = newList.size();

    for (int i = 0; i < numBunches; i++)
    {
        cout << newList[i].getFirst() << " " << newList[i].getSecond() << " " << newList[i].getThird() << endl;
    }
    cout << endl;
}


vecobj.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
25
26
27
28
29
30
31
#ifndef VECOBJ_H_INCLUDED
#define VECOBJ_H_INCLUDED

class Bunch
{
public:
	// Default constructor
	Bunch();

	// Overload constructor
	Bunch(int, int, int);

	// Default destructor
	~Bunch();

	// Accessor functions
	int getFirst();
	// So that the outside program can access contents of 'first'
	int getSecond();
	// So that the outside program can access contents of 'second'
	int getThird();
	// So that the outside program can access contents of 'third'

private:
	int first;
	int second;
	int third;

};
#endif // VECOBJ_H_INCLUDED


vecobj.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
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <random>
#include "vecobj.h"

using namespace std;

Bunch::Bunch()
{
	first = 0;
	second = 0;
	third = 0;
}

Bunch::Bunch(int firstNum, int secondNum, int thirdNum)
{
	first = firstNum;
	second = secondNum;
	third = thirdNum;
}

Bunch::~Bunch()
{

}

int Bunch::getFirst()
{
	return first;
}

int Bunch::getSecond()
{
	return second;
}

int Bunch::getThird()
{
	return third;
}
Last edited on
with a lovely Linker error

Can we see the exact error message?
> void printVector(const vector<Bunch>&);
...
> void printVector(vector<Bunch>& newList)
Yes, you need to make your mind up as to whether the parameter is const or not.

The prototype has const but the definition does not.
Yes, that's the ticket.

It didn't like it when I changed *both* to type const (it then complained that the type return of my getFirst/getSecond/getThird.
I removed the const from both declaration and function and it worked!

I don't understand why, thought.

Can anyone explain this?

Thanks.
Can anyone explain this?

printVector should definitely accept a const vector, but your get functions need to be const:

1
2
3
4
5
6
7
8
        // in the class definition:
	int getFirst() const;
        // ...

int Bunch::getFirst() const {
	return first;
}
//... 

Last edited on
Yes, this worked!

Many thanks all for your help!

Topic archived. No new replies allowed.