简体   繁体   English

C ++和Netbeans的未定义参考问题

[英]Undefined Reference issue with C++ and Netbeans

I have the following files: 我有以下文件:

ListaEnc.hpp ListaEnc.hpp

#include "Elemento.hpp"
#include <cstdlib>
#include <iostream>

template<typename T>
class ListaEnc {

public:
    ListaEnc();
    ~ListaEnc();
...
}

//Implementation:
template<typename T>
ListaEnc<T>::ListaEnc()
{
    head = NULL;
    size = 0;
}

template <class T>
ListaEnc<T>::~ListaEnc() {

}

main.cpp: main.cpp:

#include "ListaEnc.hpp"
using namespace std;

int main(int argc, char** argv)
{
    ListaEnc<int>* teste = new ListaEnc<int>();

    return 0;
}

Poligono.hpp Poligono.hpp

#ifndef POLIGONO_HPP
#define POLIGONO_HPP

#include "Ponto.hpp"
#include "ListaEnc.hpp"
#include <string>

using namespace std;
public:
    //Construtores
    Poligono(ListaEnc<Ponto> pontos, string nome);
    Poligono(const Poligono& orig);
    virtual ~Poligono();

    //Metodos
    string obterNome();
    void adicionarPonto(Ponto);
    ListaEnc<Ponto> obterPontos();
private:
    ListaEnc<Ponto> pontos;
    string nome;
};

#endif  /* POLIGONO_HPP */

Poligono.cpp Poligono.cpp

#include "Poligono.hpp"



* Poligono::Poligono(ListaEnc<Ponto> pontos, string nome)
{
    this->pontos = pontos;
    this->nome = nome;
}

* Poligono::Poligono(const Poligono& orig) {
}

* Poligono::~Poligono() {
}

//Metodo
string Poligono::obterNome()
{
    return this->nome;
}

ListaEnc<Ponto> Poligono::obterPontos()
{
    return this->pontos;
}

void Poligono::adicionarPonto(Ponto p)
{
    this->pontos.adiciona(p);
}

I get these compile errors on Poligono.cpp: 我在Poligono.cpp上收到以下编译错误:

/home/mariana/NetBeansProjects/TrabalhoCG/Poligono.cpp:12: undefined reference to `ListaEnc::~ListaEnc()' /home/mariana/NetBeansProjects/TrabalhoCG/Poligono.cpp:12:对`ListaEnc ::〜ListaEnc()'的未定义引用

The destructor for ListaEnc is empty, but is implemented. ListaEnc的析构函数为空,但已实现。 Does anyone know how to solve this problem? 有谁知道如何解决这个问题?

Edit: Added the implementation of constructor and destructor for ListaEnc. 编辑:添加了ListaEnc的构造函数和析构函数的实现。 The error appears in the bit of code I added for Poligono.cpp, where ListaEnc is one of the parameters. 该错误出现在我为Poligono.cpp添加的代码中,其中ListaEnc是参数之一。

Edit2: I have added an asterisk to the points in Poligono.cpp where the error appears (The first constructor, second constructor and destructor) Edit2:我向Poligono.cpp中出现错误的点添加了一个星号(第一个构造函数,第二个构造函数和析构函数)

Since you posted code that shows 由于您发布的代码显示

#ifndef POLIGONO_HPP
#define POLIGONO_HPP

in Poligono.cpp , but you don't have the same in ListaEnc.hpp , I assume that you are missing include guards in that header? Poligono.cpp ,但是您在ListaEnc.hpp没有相同的ListaEnc.hpp ,我假设您在该标头中缺少包含保护?

Depending on what other code is included in Poligono.cpp , this could result in the definition of template ListaEnc twice. 根据Poligono.cpp包含的其他代码,这可能导致两次定义ListaEnc模板。 Under C++ standard if you break the one definition rule you may just get a linking error and no other diagnostic message. 在C ++标准下,如果您违反一个定义规则,则可能只会收到链接错误,而不会收到其他诊断消息。

So, maybe make sure that there are include guards in ListaEnc.hpp ? 所以,也许要确保ListaEnc.hpp中包含include防护?

If you post more code I will update this answer :) 如果您发布更多代码,我将更新此答案:)

Edit: Another suggestion, if it is having trouble finding the dtor definition of ListaEnc , you might try moving the implementation of it into the class body? 编辑:另一项建议,如果无法找到的析构函数定义它ListaEnc ,你可以尝试移动它的实现进级的身体? This makes it less likely that a typo will cause it to be lost. 这使得错字不太可能导致它丢失。 Since all the code is in the header this really doesn't change anything otherwise. 由于所有代码都在标头中,因此实际上不会改变任何其他内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM