C++ - static vector of pointers causes undefined reference

I'm trying to create a function below in my CreateReport class called load() that copies all the records (data) from my graduate.dat file into my static vector of Record pointers called primaryCollection. I created a Record class with variables that make up each Record, and in my `load()` function in `createReport.cc` I attempted to read each line in the file, create a Record object with each line, add it to my vector, and then print everything in primaryCollection.

The problem is every time I attempt to use primaryCollection, I keep getting the error:
1
2
3
4
	CreateReport.o: In function `CreateReport::load()':
	CreateReport.cc:(.text+0x2ac): undefined reference to `CreateReport::primaryCollection'
	CreateReport.cc:(.text+0x31d): undefined reference to `CreateReport::primaryCollection'
	CreateReport.cc:(.text+0x32f): undefined reference to `CreateReport::primaryCollection'

I get 3 undefined references for the 3 times I mention primaryCollection in createReport.cc. I'm not sure what's causing these undefined references. I don't know if this is relevant to my problem, but CreateReport is also an abstract class and has a few subclasses called ReportOne, ReportTwo, etc.

primaryCollection is supposed to be a static vector of Record pointers and I'm also not allowed to use std::map for this task.

I would appreciate any help with this issue.

My graduate.dat file is formatted like below in the format < year province degree >

2000 AB Bachelor's
2005 AB Bachelor's
2005 MB College

Each line basically represents a Record. So the first record here is 2000 AB Bachelor's

Record.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
	#ifndef RECORD_H
	#define RECORD_H

		#include <iostream>
		#include <string>

		class Record{
		  public:
				Record(int = 0, string = "", string = "");
				~Record();
			
			private:
				int year;
				string province;
				string degree;
		};
		#endif 

Record.cc
1
2
3
4
5
6
7
8
	#include <iostream>
	#include <string>
	using namespace std;
	#include "Record.h"

	Record::Record(int i1, string s1, string s2) : year(i1), province(s1), degree(s2){}

	Record::~Record(){}

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

	#include <iostream>
	#include <fstream>
	#include <string>
	#include <vector>
	#include <iterator>
	#include <algorithm>
	#include <cstdlib>

	#include "Record.h"

	class CreateReport{
	  public:
		CreateReport();
		static void load();
	  
	  protected:
		static vector<Record*> primaryCollection; //STL vector of record pointers
	  
	};
	#endif 

CreateReport.cc
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 <iostream>
	using namespace std;
	#include <string>

	#include "CreateReport.h"

	CreateReport::CreateReport(){ 
	}

	void CreateReport::load(){
		int year;
		string province, degree;

		ostream_iterator<Record*> outItr(cout);

		ifstream infile("graduate.dat", ios::in); 

		if (!infile) {
			cout << "Error: could not open file" << endl;
			exit(1);
		}

		while (infile >> year >> province >> degree) { //as long as were not at end of file
			Record* record = new Record(year, province, degree); //create Record object with this data
			primaryCollection->push_back(record); //undefined reference
		}
	  
		cout<<endl<<"List of Records:"<<endl;
		copy(primaryCollection->begin(), primaryCollection->end(), outItr); //2 undefined references
	}[
Last edited on
I tried adding the line vector<Record*> CreateReport::primaryCollection; above my constructor, outside of the functions in CreateReport.cc but that changed the errors to:
1
2
3
4
5
6
7
8
9
	CreateReport.cc:13:34: error: conflicting declaration ‘std::vector<Record*> CreateReport::primaryCollection’
	 vector<Record*> CreateReport::primaryCollection;
									  ^~~~~~~~~~~~~~~~~
	In file included from CreateReport.cc:5:0:
	CreateReport.h:23:33: note: previous declaration as ‘std::vector<Record*>* CreateReport::primaryCollection’
		static std::vector<Record*>* primaryCollection; //STL vector of record pointers
									 ^~~~~~~~~~~~~~~~~
	CreateReport.cc:13:34: error: declaration of ‘std::vector<Record*>* CreateReport::primaryCollection’ outside of class is not definition [-fpermissive]
	 vector<Record*> CreateReport::primaryCollection;


Again, I would really appreciate any help with this problem. It's really frustrating as I need to test my code but can't due to this one issue with using primaryCollection.
Last edited on
Why do you want primaryCollection? It seems there is no good reason.

However you need to define (not declare) a static variable outside the class:
1
2
3
4
5
6
	#include "CreateReport.h"

	vector<Record*> CreateReport::primaryCollection; //STL vector of record pointers

	CreateReport::CreateReport(){ 
	}
Topic archived. No new replies allowed.