0

I've written a function that turns a string into a vector. Now I want to make a template of the types, because I also want to use wstrings and wchar_t. Because they are in a csv file. Here are my files ...

main.cpp

#include <iostream>
#include <fstream>
#include "trans.h"
#include "vector_to_string.cpp"

using namespace std;

int main() {
    wifstream tstream("translation.csv");
    wstring s;

    while (getline(tstream, s)){
        vector<wstring> vec = vector_from_string(s, ',');
        wstring ws = vec[0];
        Trans<wstring> t(ws);
        wcout << t.translate(Spanish) << endl;

    }
}

vector_to_string.cpp

template <class T, typename U>
std::vector<T> vector_from_string(T to_vec, U delimit) {
    int pos = 0, len = 0;
    std::vector<T> vec;
    for (int i = 0; i < to_vec.length(); i++) {
        U c = to_vec.at(i);
        if (c == delimit) {
            vec.push_back(to_vec.substr(pos, len));
            len = 0;
            pos = i + 1;
        } else if (i == to_vec.length() - 1) {
            int end = to_vec.length() - pos;
            vec.push_back(to_vec.substr(pos, end));
        } else {
            len++;
        }
    }
    return vec;
}

translation.csv

# English , French, German, Spanish
a,une,ein,UNE
aa,aa,aa,Automóvil club británico
aaa,aaa,aaa,aaa
aaron,aaron,aaron,Aaron
ab,un B,ab,Un B
abandoned,abandonné,verlassen,abandonné
abc,abc,ABC,a B C
aberdeen,aberdeen,aberdeen,Aberdeen
abilities,capacités,Fähigkeiten,capacités
ability,aptitude,Fähigkeit,aptitud
able,capable,imstande,capaz
aboriginal,Aborigène,Ureinwohner,aborigène
abortion,Avortement,Abtreibung,avortement
about,à propos,Über,Al respecto
above,au dessus de,über,au dessus de
abraham,abraham,abraham,Abrahán
abroad,à l'étranger,im Ausland,en el Extranjero
abs,abdos,Abs,abdos
...

The problem is in vector_to_string () but I can't find the problem, and I get this syntax error ...

undefined reference to `std::vector<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, std::allocator<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> > > > demonic::vector_from_string<std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, wchar_t>(std::__cxx11::basic_string<wchar_t, std::char_traits<wchar_t>, std::allocator<wchar_t> >, wchar_t)'

What is the problem and how can I best solve it?

BSlegt
  • 11
  • 1
  • At first you should put your `vector_from_string` function into a header, not a cpp file. – florestan Jan 26 '21 at 16:53
  • So... I had the knee jerk reaction of closing against https://stackoverflow.com/q/495021/817643 - But you include the CPP file... which is very idiosyncratic. Considering the fact that build tools rely on extensions to do intelligent things with source files... this will end up causing you headaches. It may even be causing you a headache now. – StoryTeller - Unslander Monica Jan 26 '21 at 17:27
  • Also, `demonic::vector_from_string` and an unqualified `vector_from_string` may not at all be referring to the same thing. – StoryTeller - Unslander Monica Jan 26 '21 at 17:30

0 Answers0