c++ - G++ 链接器错误 : undefined reference to

标签 c++ linker g++

<分区>

我正在尝试用 C++ 编译程序,但编译器返回此错误:

operadoraarquivos.o: In function `split(std::string, int&)':
operadoraarquivos.cpp:(.text+0x0): multiple definition of `split(std::string, int&)'
main.o:main.cpp:(.text+0x0): first defined here
Hash.o: In function `HashTable::HashTable(int)':
Hash.cpp:(.text+0x65): undefined reference to `Lista<Palavra>::Lista()'
Hash.cpp:(.text+0xba): undefined reference to `Lista<Palavra>::~Lista()'
Hash.o: In function `HashTable::~HashTable()':
Hash.cpp:(.text+0x134): undefined reference to `Lista<Palavra>::~Lista()'
Hash.o: In function `HashTable::inserir(std::string, int)':
Hash.cpp:(.text+0x2d0): undefined reference to `Lista<Palavra>::vazia()'
Hash.cpp:(.text+0x31b): undefined reference to `Lista<Palavra>::buscar(Palavra)'
Hash.cpp:(.text+0x34d): undefined reference to `Lista<int>::Lista()'
Hash.cpp:(.text+0x365): undefined reference to `Lista<int>::inserir(int)'
Hash.cpp:(.text+0x3a5): undefined reference to `Lista<Palavra>::inserir(Palavra)'
Hash.cpp:(.text+0x3ce): undefined reference to `Lista<int>::inserir(int)'
Hash.cpp:(.text+0x3e7): undefined reference to `Lista<int>::Lista()'
Hash.cpp:(.text+0x3ff): undefined reference to `Lista<int>::inserir(int)'
Hash.cpp:(.text+0x43f): undefined reference to `Lista<Palavra>::inserir(Palavra)'
Hash.o: In function `HashTable::buscar(std::string)':
Hash.cpp:(.text+0x590): undefined reference to `Lista<Palavra>::buscar(Palavra)'
Hash.o: In function `HashTable::imprimir(std::string)':
Hash.cpp:(.text+0x6d5): undefined reference to `Lista<int>::imprimir()'
collect2: error: ld returned 1 exit status*****

命令:g++ -o busca main.o operadoraarquivos.o Lista.o Hash.o

如何解决这个错误?谢谢

main.cpp

#include "operadoraarquivos.hpp"

int main(int argc, char* argv[]) {
    OperadoraArquivos opArq(argv[1], argv[2], argv[3]);
    opArq.imprimirBusca();
    opArq.~OperadoraArquivos();
}

operadoraarquivos.cpp

#include "operadoraarquivos.hpp"

OperadoraArquivos::OperadoraArquivos(string livro, string sw, string buscas) {
    this->livro.open(livro.c_str(), ios_base::in);
    this->sw.open(sw.c_str(), ios_base::in);
    this->buscas.open(buscas.c_str(), ios_base::in);
    this->resultados.open(ARQ_RESULTADOS, ios_base::out);
    this->ht = new HashTable(8388593); // 8388593 < 2^23

    stopwords = new string[335];

   // while (!sw.eof())
   //      getline(sw, stopwords[i++]);
    int i = 0;
   while (getline(this->sw, stopwords[i++]));
    string linha;
    for ( i = 1; !this->livro.eof(); i++) {
        getline(this->livro, linha);
        int n;
        string *palavras = split(linha, n);
        for (int j = 0; j < n; j++) {
            if (deveGuardar(palavras[j]))
                ht->inserir(palavras[j], i);
        }
    }
}

OperadoraArquivos::~OperadoraArquivos() {
    this->livro.close();
    this->buscas.close();
    this->resultados.close();
    this->sw.close();
}

bool OperadoraArquivos::deveGuardar(string str) {
    int baixo = 0, alto = 334, meio;
    while (baixo <= alto) {
        meio = (baixo + alto) / 2;
        if (str == stopwords[meio])
            return true;
        else {
            if (str > stopwords[meio])
                baixo = meio;
            else
                alto = meio;
        }
    }
    return false;
}

string OperadoraArquivos::paraMaiuscula(string str) {
    string result = "";
    for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
        result += toupper(*it);
    return result;
}

void OperadoraArquivos::imprimirBusca() {
    string temp;
    while (getline(buscas, temp)) {
        ht->imprimir(temp);
    }
}

operadoraarquivos.hpp

#ifndef OPERADORAARQUIVOS_H
#define OPERADORAARQUIVOS_H

#include <iostream>
#include <fstream>
#include <cctype>
#include "Hash.hpp"

#define ARQ_RESULTADOS "resultados.txt"

string* split(string str, int &n) {
    int contador = 0;
    for ( string::iterator it=str.begin(); it!=str.end(); ++it) {
        if (*it == ' ')
            contador++;
    }
    string *tokens = new string[contador+1]();
    int i = 0;
    for (string::iterator it=str.begin(); it!=str.end(); ++it) {
        if (*it == ' ')
            i++;
        tokens[i] += *it;
    }
    n = i + 1;
    return tokens;
}

class OperadoraArquivos
{
private:
    string* stopwords;
    ifstream livro;
    ifstream sw;
    ifstream buscas;
    ofstream resultados;
    bool deveGuardar (string str);
    string paraMaiuscula (string str);
    HashTable* ht;
public:
    OperadoraArquivos(string livro, string sw, string buscas);

    ~OperadoraArquivos();

    HashTable* getHashTable();

    void imprimirBusca();

};

#endif // OPERADORAARQUIVOS_H

哈希.cpp

#include "Hash.hpp"
#include "Lista.hpp"

HashTable::HashTable(int max) {
    vetorListas = new Lista<Palavra>[max]();
    this->max = max;
}

HashTable::~HashTable() {
    delete[] vetorListas;
}

int HashTable::hash (string str) {
    Posicao indice = 0;
    int expoente = 0;
    for ( std::string::iterator it=str.begin(); it!=str.end(); ++it)
        indice += (*it) * pow(128,expoente++);
    indice %= max;
}

void HashTable::inserir(string texto, int linha) {
    Palavra palavra;
    Posicao indice = hash(texto);
    palavra.chave = indice;
    palavra.texto = texto;

    if (!vetorListas[indice].vazia())
    {
        No<Palavra>* no = vetorListas[indice].buscar(palavra);

        if (!no) {
            palavra.lista = new Lista<int>();
            palavra.lista->inserir(linha);
            vetorListas[palavra.chave].inserir(palavra);
        }
        else {
            no->item.lista->inserir(linha);
        }

    }
    else {
        palavra.lista = new Lista<int>();
        palavra.lista->inserir(linha);
        vetorListas[indice].inserir(palavra);
    }
}

No<Palavra>* HashTable::buscar (string texto) {
    Palavra palavra;
    palavra.texto = texto;
    palavra.chave = hash(texto);
    return vetorListas[palavra.chave].buscar(palavra);
}

No<Palavra>* HashTable::operator[] (string texto) {
    return buscar(texto);
}

void HashTable::imprimir(string texto) {
    No<Palavra>* no = this->buscar(texto);
    cout << texto << " ";
    no->item.lista->imprimir();
}

哈希.hpp

#ifndef LISTA_H
#define LISTA_H

#include <iostream>

using namespace std;
typedef int chave;
typedef int Posicao;

template <class T>

class No {
public:
 T item;
 No<T> *proximo;
};

template <class T>
class Lista {
private:
    No<T> *prim, *ult;
    void flVazia();
public:
    Lista();
    ~Lista();
    bool vazia();
    void inserir(T x);
    void retira(No<T> *p);
    //No<T>* buscar(Posicao pos);
    No<T>* buscar(T item);
    void imprimir();
    //void imprimeChave(Item item);
};

#endif

Lista.cpp

#include "Lista.hpp"
template <class T>
Lista<T>::Lista() {
    flVazia();
}
template <class T>
Lista<T>::~Lista() {
    while (!vazia())
        retira(ult);
}
template <class T>
void Lista<T>::flVazia() {
    prim = new No<T>();
    //prim->item = {0,0};
    ult = prim;
    ult->proximo = NULL;
}
template <class T>
bool Lista<T>::vazia(){
    return prim == ult;
}
template <class T>
void Lista<T>::inserir(T x) {
    ult->prox = new No<T>();
    ult = ult->prox;
    ult->item = x;
    ult->proximo = NULL;
}
template <class T>
void Lista<T>::retira(No<T> *p) {
    if (vazia() || p == NULL || p->prox == NULL) {
        cout << "Erro: Retirada impossivel" << endl;
    }
    else {
        No<T> *q = p->prox;
        p->prox = q->prox;
        if (p->prox == NULL) ult = p;
        delete(q);
    }
}
/*No<T>* Lista<T>::buscar(Posicao pos) {
    int i=0;
    for (No *p = prim; p!=NULL; p=p->prox) {
        if (pos == i)
            return p;
        ++i;
    }
    return NULL;
}*/
template <class T>
No<T>* Lista<T>::buscar(T item) {
    for (No<T> *p = prim; p!=NULL; p=p->prox) {
            if (p->item == item)
                return p;
        }
        return NULL;
}
/*template <class T>
No<T>* Lista<T>::operator[](Posicao pos)
{
    return buscar(pos);
}*/
template <class T>
void Lista<T>::imprimir() {
    for (No<T>* p = prim; p!=NULL; p=p->prox) {
        cout << p->item;
        if (p->prox!=NULL)
            cout << " ";
    }
    cout << endl;
}

Lista.hpp

#ifndef LISTA_H
#define LISTA_H

#include <iostream>

using namespace std;
typedef int chave;
typedef int Posicao;

template <class T>

class No {
public:
 T item;
 No<T> *proximo;
};

template <class T>
class Lista {
private:
    No<T> *prim, *ult;
    void flVazia();
public:
    Lista();
    ~Lista();
    bool vazia();
    void inserir(T x);
    void retira(No<T> *p);
    //No<T>* buscar(Posicao pos);
    No<T>* buscar(T item);
    void imprimir();
    //void imprimeChave(Item item);
};

#endif

最佳答案

两个问题:

  • 您在头文件中定义split。为避免多个定义错误,请将定义移至源文件(仅在 header 中保留声明),或将其标记为内联

  • 您正在源文件中实现模板HashLista。模板通常需要在 header 中实现,包含在每个需要使用该模板的源文件中。

参见 this question了解第二点的血淋淋的细节。

关于c++ - G++ 链接器错误 : undefined reference to,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26871603/

相关文章:

c++ - 静态和共享库链接器错误

c - gcc 和 GNU ld 链接器 - 生成具有强制固定地址/偏移量的静态链接代码

c - 如何避免c文件的重新编译?

c++ - 无法编译 cpp-netlib 示例

c++ - 通过引用传递数组

C++:自动 vector 重新分配调用复制构造函数?为什么?

c++ - fstream、ofstream、传递文档名称、C++

c++ - 如何在 Linux 中启用 C++11

ubuntu - "Undefined reference to"使用g++和ncurses编译时

c++ - 类模板中没有成员函数声明