I'm sorry if the question sounds confusing. I'm kind of new to C++ with some intermediate knowledge in C# and Java. What I'm trying to achieve is the following:
I have a C .h header file that works as an interface and its methods are implemented on another .cpp file. I've been requested to use/include the header on another .cpp file which is going to be the unit test and is meant to make use of the actual methods.
These are the header and implementation code:
//TAD.h
#pragma once
using namespace std;
class TAD{
private:
string *E;
int Dim; /*Dimension*/
void copyData(string *acpy, int start, int end, short int paralel);
public:
TAD();
~TAD();
void Add(string x);
string Remove();
//overload
string Remove(int position);
string Search(string x);
};
//TAD.cpp
#include "TAD.h"
#include <string>
using namespace std;
TAD::TAD(){
this->Dim = 0;
}
TAD::~TAD(){
delete E;
}
void TAD::Add(string x){
// for each Add/Remove/Search, E needs to be copied
string *acopy = E;
Dim++;
this->E = new string[Dim];
copyData(acopy, 0,Dim-1, 0);
this->E[Dim - 1] = x; // new Add in position n-1
}
void TAD::copyData(string *acpy, int start, int end, short int paralel){
for (int k = start; k < Dim; k++)
this->E[k] = acpy[k+paralel];
}
string TAD::Remove(){
string *acopy = E;
string removed = acopy[Dim - 1]; // removes last one
Dim--;
this->E = new string[Dim];
copyData(acopy, 0, Dim,0);
return removed;
}
string TAD::Remove(int position){
if (position < 0 || position > Dim - 1)
return NULL;
string *acopy = E;
string removed = acopy[position];
Dim--;
this->E = new string[Dim];
copyData(acopy, 0, position, 0);
copyData(acopy, position+1, Dim+1, 1);
return removed;
}
string TAD::Search(string delta){
string found = NULL;
for (int k = 0; k < Dim; k++)
if (E[k] == delta)
found = E[k];
return found;
}
So, this is what I have for the unit test this far which is simple since I'm trying to see how to make it work before proceeding with anything else:
//UnitTest.cpp
#include <iostream>
#include "TAD.h"
//using namespace std;
int main(){
TAD fruits;
fruits.Add("Mango");
fruits.Add("Apple");
fruits.Add("Tomato")
std::cout << fruits.Remove() << std::endl;
}
When compiling, this is what I receive:
**> Executing task: C/C++: g++.exe build active file <
Starting build...
C:/msys64/mingw64/bin/g++.exe -fdiagnostics-color=always -g "C:\Users\javid\OneDrive\Escritorio\Semestre 2022 - 1\INF 526\Codigo\UnitTest.cpp" -o "C:\Users\javid\OneDrive\Escritorio\Semestre 2022 - 1\INF 526\Codigo\UnitTest.exe"
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\javid\AppData\Local\Temp\ccnS2aI7.o:C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:6: undefined reference to `TAD::TAD()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\javid\AppData\Local\Temp\ccnS2aI7.o: in function `main':
C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:7: undefined reference to `TAD::Add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:8: undefined reference to `TAD::Add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:9: undefined reference to `TAD::Add(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:11: undefined reference to `TAD::Remove[abi:cxx11]()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:14: undefined reference to `TAD::~TAD()'
C:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/11.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:/Users/javid/OneDrive/Escritorio/Semestre 2022 - 1/INF 526/Codigo/UnitTest.cpp:14: undefined reference to `TAD::~TAD()'
collect2.exe: error: ld returned 1 exit status
Build finished with error(s).
Terminal will be reused by tasks, press any key to close it.**
Since I'm getting a lot of undefined reference errors, I'm assuming the UnitTest.cpp file is not using the implemented versions of the methods that are on the other .cpp file.
I'm using VSCode on Windows 10 and g++ as the compiler. Any kind of help will be greatly appreciated!