0

I am trying to compile a project that contains several .h and .cpp files on Ubuntu using VSCode. However, when I try to build it I receive the error: Udifeind reference to only 2 of my classes that have been defined in one of my .cpp files. I should mention that this project works fine in VS on windows. Is there something wrong with my class definitions for ubuntu?

Important note: I don't think the problem is with linking, because even I run it through the terminal, it gives the same errors.

Could anyone help me with it? Thank you.

These are my classes:

template<typename T>
void Signal::bufferGet(T* valueAddr) {

*valueAddr = static_cast<T*>(buffer)[outPosition];

bufferFull = false;

outPosition = (outPosition + 1) % bufferLength;

bufferEmpty = outPosition == inPosition;

return;
};


template<typename T>                            
void Signal::bufferPut(T value) 
{
(static_cast<T *>(buffer))[inPosition] = value;

inPosition = (inPosition + 1) % bufferLength;

bufferEmpty = false;
bufferFull = inPosition == outPosition;

if(inPosition == 0)
{
    if (saveSignal)
    {
        if (!headerWritten) writeHeader();

        if (firstValueToBeSaved <= bufferLength)
        {
            char *ptr = (char *)buffer;
            ptr = ptr + (firstValueToBeSaved - 1) * sizeof(T);
            std::ofstream fileHandler{ "./" + folderName + "/" + 
    fileName, std::ios::out | std::ios::binary | std::ios::app };
            fileHandler.write(ptr, (bufferLength - 
    (firstValueToBeSaved - 1)) * sizeof(T));
            fileHandler.close();
            firstValueToBeSaved = 1;
        }
        else
        {
            firstValueToBeSaved = firstValueToBeSaved - 
      bufferLength;
        }
     }
     }
     }

And this is a part of code that call these classes:

if (process == 0) return false;

for (auto i = 0; i < process; i++)
{
    t_binary randomBits;
    inputSignals[0]->bufferGet(&randomBits);
    outputSignals[0]->bufferPut(randomBits);
    outputSignals[1]->bufferPut(randomBits);
    outputSignals[2]->bufferPut((t_binary)1);
}

std::cout << process <<" oblivious keys are generated"<< std::endl;
    return true;
}

And this what i wrote in terminal (I put all the .cpp and .h in the same place as main.cpp):

g++ -std=c++17 quantum_oblivious_key_distribution_emulator_sdf.cpp 
Alice_QOKD_20190816.cpp Bob_QOKD_20190816.cpp 
ip_tunnel_linux_20190816.cpp binary_source_20190816.cpp 
sink_20190816.cpp netxpto_20190816.cpp -o main

Screenshot of errors

Zeinab Rahmani
  • 77
  • 3
  • 10
  • You're not linking with all the required libraries. See the duplicate question for more information. – Sam Varshavchik Jun 07 '20 at 15:08
  • @Sam Varshavchik The errors are only related to specific classes. I ran the program through the terminal and I have same errors. could it be still linking problem??!!! – Zeinab Rahmani Jun 10 '20 at 15:11
  • @ZeinabRahmani Have you defined the bufferPut and bufferGet functions in a .cpp file? – john Jun 10 '20 at 15:20
  • @john Yes they are defined in one of my .cpp files. – Zeinab Rahmani Jun 10 '20 at 15:24
  • @ZeinabRahmani Then that is one mistake, template code should be defined in header files only., https://stackoverflow.com/questions/495021/why-can-templates-only-be-implemented-in-the-header-file – john Jun 10 '20 at 15:25
  • @john Thank you a thousand times. It solved the problem. As you said the implementation of a template class or function must be in the same file as its declaration. – Zeinab Rahmani Jun 10 '20 at 16:33

0 Answers0