@md5inj
Junior Frontend, Delphi, c++

Как исправить ошибку «неразрешенных внешних элементов»?

1>lab15.obj : error LNK2019: ссылка на неразрешенный внешний символ "public: __thiscall Vector<double>::Vector<double>(int)" (??0?$Vector@N@@QAE@H@Z) в функции _main
1>F:\study\CPPL\lab15\Debug\lab15.exe : fatal error LNK1120: неразрешенных внешних элементов: 1

Помогите пожалуйста. Я нагуглил, что эта ошибка из-за того, что нету реализации конструктора, но конструктор реализован.

Vector.h
#pragma once
#include "stdafx.h"
#include <iostream>

using namespace std;

template <typename T>
class Vector
{
private:
	int len;
	int maxLen;
	T *arr;
public:
	Vector();
	Vector(int maxLen);
	~Vector();
	void add(T item);
	void printArray();
	void operator -= (T a);
	double &operator [] (int i);
	void input(int count);
	friend Vector operator - (Vector &vct, double a);
};


Vector.cpp
#include "Vector.h"

template <typename T>
Vector<T>::Vector()
{
	this->arr = NULL;
	this->len = 0;
	this->maxLen = 0;
}

template <typename T>
Vector<T>::Vector(int maxLen)
{
	this->maxLen = maxLen;
	this->arr = new T[maxLen];
	this->len = 0;
}

template <typename T>
Vector<T>::~Vector()
{
}

template <typename T>
void Vector<T>::add(T item)
{
	if (this->len >= this->maxLen) return;
	this->arr[this->len] = item;
	this->len++;
}

template <typename T>
void Vector<T>::input(int count)
{
	T a = 0;
	for (int i = 0; i < count; i++)
	{
		cout << "Input the number: ";
		cin >> a;
		this->add(a);
	}
}

template <typename T>
void Vector<T>::printArray()
{
	for (int i = 0; i < this->len; i++)
	{
		cout << i << ": " << this->arr[i] << endl;
	}
}

template <typename T>
void Vector<T>::operator -= (T a)
{
	for (int i = 0; i < this->len; i++)
	{
		this->arr[i] = this->arr[i] - a;
	}
}

template <typename T>
double &Vector<T>::operator [] (int n)
{
	return this->arr[n];
}

template <typename T>
Vector<T> operator - (Vector<T>& vct, double a)
{
	Vector<T> res(vct.maxLen);
	res.len = vct.len;
	for (int i = 0; i < vct.len; i++)
	{
		res.arr[i] = vct.arr[i] - a;
	}
	return res;
}


lab15.cpp
#include "Vector.h"

using namespace std;

int main()
{
	Vector <double> *my = new Vector <double> (10);
	system("pause");
    return 0;
}
  • Вопрос задан
  • 4511 просмотров
Пригласить эксперта
Ответы на вопрос 1
zagayevskiy
@zagayevskiy
Android developer at Yandex
Шаблоны нельзя разносить по h/cpp файлам. Всё должно быть в h.
Ответ написан
Комментировать
Ваш ответ на вопрос

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

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