0

So I have quite a straightforward implementation written in C++ that uses templates to to some operations to a std::vector. The problem is that whenever I try to call the method that does something on a vector, compilation gives me this error:

basavyr@Roberts-MacBook-Pro src % g++ -std=c++17 -I../include/ energyExpressions.cc pr135.cc data.cc main.cc -
O3
Undefined symbols for architecture x86_64:
  "void EnergyFormula::kevTOmev<double>(std::__1::vector<double, std::__1::allocator<double> >&)", referenced 
from:
      _main in main-9a2141.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1

Is this really related to my CPU architecture? And why/how can one avoid such situation? Is there another efficient/smart way of solving this?

Here is the code I have written:

in the include file:

#ifndef ENERGYEXPRESSIONS_HH
#define ENERGYEXPRESSIONS_HH

#include <vector>

class EnergyFormula
{
public:
    //N=0 wobbling band
    double yrastBand(double params);

    //N=1 wobbling band
    double wobblingBand(double params);

    template <typename T>
    void kevTOmev(std::vector<T> &);
};

#endif // ENERGYEXPRESSIONS_HH

in the expression.cc file:

#include "../include/energyExpressions.hh"

template <typename T>
void EnergyFormula::kevTOmev(std::vector<T> &vec) //using reference because I don't want to copy the vector, I just want to modify it directly
{
    auto mev = 1000;
    for (int i = 0; i < vec.size(); ++i)
    {
        vec.at(i) = static_cast<T>(vec.at(i) / mev);
    }
}

now in my main.cpp:

#include <iostream>
#include <vector>
#include <string>
#include <ctime>

#include "../include/pr135.hh" //some other stuff
#include "../include/data.hh" //some other stuff
#include "../include/energyExpressions.hh"

int main()
{
 EnergyFormula *formulas = new EnergyFormula;
 formulas->kevTOmev<double>(test3);
    for (auto &&n : test3)
    {
        std::cout << n << " ";
    }

    delete nucleu;
    v1.clear();
}

macOS Catalina latest update with latest CLANG by the way.

0 Answers0