Problem z kompilacją programu (error LINK)

0

Proszę o pomoc. Błąd jaki się pojawia po kompilacji kodu. Czy mógłby ktoś zerknąć na kod i podpowiedzieć co może być nie tak ??

1>  Main.cpp
1>C:\Users\48537\Desktop\JiPP II\Projekt\Main\Main.obj : error LNK2019: nierozpoznany symbol zewnętrzny "public: __thiscall Employee::Employee(void)" (??0Employee@@QAE@XZ) przywołany w funkcji _main
1>C:\Users\48537\Desktop\JiPP II\Projekt\Main\Main.obj : error LNK2019: nierozpoznany symbol zewnętrzny "public: __thiscall Employee::~Employee(void)" (??1Employee@@QAE@XZ) przywołany w funkcji _main
1>C:\Users\48537\Desktop\JiPP II\Projekt\Main\Debug\Main.exe : fatal error LNK1120: liczba nierozpoznanych elementów zewnętrznych: 2

Tutaj przesyłam kod:

JiPPVector.h

#ifndef  VECTOR_H
#define VECTOR_H

#include <iostream>

template <typename T>
class JippVector
{
	T* array;
	size_t size;
	T choosen_value;
	size_t capacity;


public:

	JippVector();

	JippVector(size_t);

	~JippVector();

	JippVector& operator=(const JippVector& rhs) = delete;

	JippVector(const JippVector& ob);

	void print(int);

	int get_capacity();

	int get_size();

	void reserve(int);

	void fill(T);

	int fill(int, int, T);

	T& operator[](int);

	int pushBack(T);

	int pushFront(T);

	int insert(int, T);

	int erase(int);

	int erase(int, int);

	bool is_empty();

	void shrink_to_fill();

};


#endif // ! VECTOR_H

JippVector.cpp

#include "JippVector.h"

using namespace std;

template class JippVector<int>;
template class JippVector<double>;
template class JippVector<string>;
template class JippVector<char>;

template<typename T>
JippVector<T>::JippVector()
{
	size = 0;
	capacity = 0;
	array = NULL;
}

template<typename T>
JippVector<T>::JippVector(size_t capacity)
{
	size = 0;
	this->capacity = capacity;
	array = new T[capacity];
}

template<typename T>
JippVector<T>::~JippVector()
{
	if (array != NULL)
	{
		delete[] array;
	}
}


template<typename T>
JippVector<T>::JippVector(const JippVector& ob):
	size(ob.size),
	capacity(ob.capacity)
{
	array = new T[capacity];
	for (int i = 0; i < size; i++)
	{
		array[i] = ob.array[i];
	}
}

template<typename T>
void JippVector<T>::print(int i)
{
	cout << array[i]<< "\t";
}

template<typename T>
int JippVector<T>::get_capacity()
{
	return capacity;
}

template<typename T>
int JippVector<T>::get_size()
{
	return size;
}

template<typename T>
void JippVector<T>::reserve(int new_capacity)
{
	if (new_capacity > capacity)
	{
		T* temp_array;
		temp_array = new T[new_capacity];
		for (int i = 0; i < capacity; i++)
		{
			temp_array[i] = array[i];
		}

		
		delete[] array;
		array = new T[new_capacity];

		for (int i = 0; i < new_capacity; i++)
		{
			array[i] = temp_array[i];
		}

		capacity = new_capacity;
	}
}

template<typename T>
void JippVector<T>::fill(T value_to_fill)
{
	for (int i = 0; i < capacity; i++)
	{
		array[i] = value_to_fill;
	}
	size = capacity;
}

template<typename T>
int JippVector<T>::fill(int start, int len, T value_to_fill)
{
	if ((start + len) <= capacity)
	{
		for (int i = start; i < (start + len); i++)
		{
			array[i] = value_to_fill;
		}

		if (size < start + len)
		{
			size = start + len;
		}
		return 0;
	}
	else
	{
		return 1; // brak capacity
	}
}

template<typename T>
T& JippVector<T>::operator[](int index)
{
	if (index < 0 || index >= size)
	{
		throw out_of_range("Index out of range");
	}
	return array[index];
}

template<typename T>
int JippVector<T>::pushBack(T value_to_add)
{
	if (size < capacity)
	{
		array[size] = value_to_add;
		size++;
		return 0;
	}
	else
	{
		return 1; // brak capacity
	}
}


template<typename T>
int JippVector<T>::pushFront(T value_to_add)
{
	if (size < capacity)
	{
		for (int i = size; i > 0; i--)
		{
			array[i] = array[i-1];
		}
		array[0] = value_to_add;
		size++;
		return 0;
	}
	else
	{
		return 1; // brak capacity
	}
}

template<typename T>
int JippVector<T>::insert(int index, T value_to_add)
{
	if (size < capacity)
	{
		if (index <= size && index >= 0)
		{
			for (int i = size; i > index; i--)
			{
				array[i] = array[i - 1];
			}
			array[index] = value_to_add;
			size++;
			return 0;
		}
		else
		{
			return 2; //index poza zasięgiem
		}
	}
	else
	{
		return 1; //brak capacity
	}
}

template<typename T>
int JippVector<T>::erase(int index)
{
	if (index <= size && index >= 0)
	{
		for (int i = index; i < size; i++)
		{
			array[i] = array[i + 1];
		}
		size--;
	}
	else
	{
		return 2; //index poza zasięgiem
	}
}

template<typename T>
int JippVector<T>::erase(int index, int lenght)
{
	if (index + lenght <= size && index >= 0)
	{
		for (int i = index; i < size; i++)
		{
			array[i] = array[i + lenght];
		}
		size = size - lenght;
	}
	else
	{
		return 2; //index poza zasięgiem
	}
}

template<typename T>
bool JippVector<T>::is_empty()
{
	if (size != 0)
	{
		return true;
	}
	else
	{
		return false;
	}
}

template<typename T>
void JippVector<T>::shrink_to_fill()
{
	capacity = size;
	T* temp_array;
	temp_array = new T[size];

	for (int i = 0; i < size; i++)
	{
		temp_array[i] = array[i];
	}

	delete[] array;

	array = new T[capacity];

	for (int i = 0; i < capacity; i++)
	{
		array[i] = temp_array[i];
	}

	delete[] temp_array;
}

Employee.h

#pragma once
#include <iostream>
#include "JippVector.h"
#include <string>
#include <iomanip>

using namespace std;

class Employee
{
	JippVector<string> name;
	JippVector<string> surrname;
	JippVector<int> age;
	JippVector<double> salary;


public:
	Employee();

	~Employee();

	void set_capacity(int);

	void add(string, string, int, double);

	void add(int, string, string, int, double);

	void del(int);

	void del(int, int);

	void edit(int, string, string, int, double);

	void print_size();

	void print();
};

Employee.cpp

#include "Employee.h"

Employee::Employee()
{
	name.reserve(0);
	surrname.reserve(0);
	age.reserve(0);
	salary.reserve(0);
}

Employee::~Employee() {}

void Employee::set_capacity(int capacity)
{
	name.reserve(capacity);
	surrname.reserve(capacity);
	age.reserve(capacity);
	salary.reserve(capacity);
}

void Employee::add(string name, string surrname, int age, double salary)
{
	this->name.pushBack(name);
	this->surrname.pushBack(surrname);
	this->age.pushBack(age);
	this->salary.pushBack(salary);
}

void Employee::add(int index, string name, string surrname, int age, double salary)
{
	this->name.insert(index, name);
	this->surrname.insert(index, surrname);
	this->age.insert(index, age);
	this->salary.insert(index, salary);
}

void Employee::del(int index)
{
	name.erase(index);
	surrname.erase(index);
	age.erase(index);
	salary.erase(index);
}

void Employee::del(int index, int lenght)
{
	name.erase(index, lenght);
	surrname.erase(index, lenght);
	age.erase(index, lenght);
	salary.erase(index, lenght);
}

void Employee::edit(int index, string name, string surrname, int age, double salary)
{
	this->name[index] = name;
	this->surrname[index] = surrname;
	this->age[index] = age;
	this->salary[index] = salary;
}

void Employee::print_size()
{
	cout << "Liczba elemntów w kontenerze: " << name.get_size() << endl;
}

void Employee::print()
{
	int column_width = 20;

	cout << setw(column_width) << "LP";
	cout << setw(column_width) << "IMIĘ";
	cout << setw(column_width) << "NAZWISKO";
	cout << setw(column_width) << "WIEK";
	cout << setw(column_width) << "PENSJA";
}


Main.cpp

#include <iostream>
#include "Employee.h"
#include "JippVector.h"

using namespace std;



int main()
{
	Employee e1;

	return 0;
}
3

to nie jest problem z kodem C++ tylko z tym ze w momencie linkowania finalnej aplikacji nie masz skompilowanych wszystkich elementów składowych

Main.exe = Main.obj + JippVector.obj + Employee.obj

najlepiej zacznij uzywac cmake

0

Ok dzięki, spróbuję. A tak z ciekawości czy da się rozwiązać ten problem w aplikacji konsolowej ? — grze0203 2024-01-08 22:28

To zalezy, czy chcesz,
a) dostać poradę "trzecia wajcha od lewej"
b) zrozumieć co tu zachodzi i nauczyć się rozwiązywać problem (i wyłącz te poslkie komunikaty, angielskie wrzucisz w googla i "coś" uzyskasz)

0

Kompilować się kompiluje, ale dużo warningów jest do poprawienia.
Jak korzystasz z GNU to taki skrypt.

g++ -Wall -Wextra -Wpedantic Main.cpp JippVector.cpp Employee.cpp -o main.o

Musisz podać wszystkie pliki źródłowe czyli pliki cpp.

1

@Autysta: OP ma problem z Visual Studio C++, pewnie dodał pliki w folderze, ale "nie poklikał" reszty.

1

Tak jak wyżej kolega napisał, coś nakręciłeś przy tworzeniu projektu. Odpal poradnik na YT z nowym projektem w cpp w Microsoft visual studio (czy co tam używasz), zrób projekt, dodaj nowe pliki i wklej swój kod, pewnie zadziała

1

Dzięki za odpowiedzi. Udało mi się rozwiązać problem.

1 użytkowników online, w tym zalogowanych: 0, gości: 1