0

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!

273K
  • 29,503
  • 10
  • 41
  • 64
Koro
  • 1
  • 2
    You need to figure out how, in the build system you use, to set up a project consisting of two source files. At the moment, you only compile and link `UnitTest.cpp`, and the linker cannot find implementation that should be provided in `TAD.cpp` – Igor Tandetnik Feb 19 '22 at 23:20
  • In VSCode, it apparently has [something to do](https://stackoverflow.com/questions/47665886/vs-code-will-not-build-c-programs-with-multiple-ccp-source-files) with a file named `tasks.json` (disclaimer: I'm not familiar with VSCode and don't really know what I'm talking about). – Igor Tandetnik Feb 19 '22 at 23:23
  • Simplifying (in linux) ... You're doing `g++ -g -o UnitTest UnitTest.cpp`. You may want: `g++ -c TAD.cpp ; g++ -g -o UnitTest UnitTest.cpp TAD.o` You have to ensure that the compiled functions from `TAD.cpp` are linked in when you build `UnitTest`. Under winX, you may need `.obj` in place of `.o` and `UnitTest.exe` for output, but the issue is the same. You could also do: `g++ -c -o UnitTest UnitTest.cpp TAD.cpp` How to do this in VSCode may require config changes like Igor is mentioning to get it to do something similar to what I've just posted – Craig Estey Feb 19 '22 at 23:26
  • Thanks so much! I was able to compile both cpp files using: g++ *.cpp -o output. Still got an unrelated memory allocation issue, but can I work around that. – Koro Feb 20 '22 at 00:57

0 Answers0