@komers_s

Как исправить ошибку: «LNK 2019 ссылка на неразрешенный символ» c++?

Доброго времени суток, столкнулся с такой проблемой:61d844cc35309652458026.png

Использую пока всего три файла:
1.RandomAccess.cpp - главный файл с функцией main
2.unit.h - заголовочный файл шаблонного класса unit
3.unit.cpp - исполняемый файл шаблонного класса unit

Структура RandomAccess.cpp
#include <iostream>
#include "unit.h"

using namespace std;

void main() {
	setlocale(0, "");

	unit <int> nBlock;


}


Структура unit.h:
#pragma once

#include <iostream>
using namespace std;

template <typename type>
class unit{

	template <typename type>
	friend istream& operator >>(istream&, unit<type>&);
	template <typename type>
	friend ostream& operator <<(ostream&, unit<type>&);

public:

	unit();
	unit(int);
	int size();

	type& operator[] (int);

private:
	int s;
	type* ptr;

	void to_null();
};


Исполняемый файл unit.cpp:
#include "unit.h"
#include <iostream>
#include <iomanip>

using namespace std;

#include <cstdlib>

// constructors
template <typename type>
unit<type>::unit() {
	s = 16;
	ptr = new type[16];
	to_null();
}

template <typename type>
unit<type>::unit(int cap) {
	s = (cap > 0) ? cap : 16;
	ptr = new type[s];
	to_null();
}

// operators
template <typename type>
type& unit<type>::operator[](int ix) {
	if (ix < 0 || ix > s) {
		cerr << "\n Ошибка индексации: индекс(" << ix << ") не пренадлежит этому юниту..";
		exit(1);
	}
	else return ptr[ix];
}

template<typename type>
istream& operator>>(istream& input, unit<type>& obj) {
	for (int i = 0; i < obj.size(); i++) {
		input >> obj[i];
	}
	return input;
}

template <typename type>
ostream& operator <<(ostream& output, unit<type>& obj) {
	output << "[";
	int edge = obj.size();
	for (int ix = 0; ix < edge; ix++) {
		output << obj[ix];
		if (ix + 1 != edge) output << setw(5);
	}
	output << "]";
	return output;
}

// public functions
template <typename type>
int unit<type>::size() {
	return s;
}

// private functions
template <typename type>
void unit<type>::to_null() {
	for (int ix = 0; ix < s; ix++) {
		ptr[ix] = 0;
	}
}
  • Вопрос задан
  • 113 просмотров
Решения вопроса 1
Реализация шаблонного класса должна быть в *.h файле.
Ответ написан
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы