【问题标题】:ERROR: undefined reference to... what is wrong? [duplicate]错误:未定义对...的引用有什么问题? [复制]
【发布时间】:2019-05-27 05:10:28
【问题描述】:

我在尝试编译代码时收到未定义的引用错误。我只是用来测试 Grafo 的类功能

grafo.h:

#ifndef GRAFO_H
#define GRAFO_H

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>


/*  Classe Grafo - Matriz de adjecencia  */
class Grafo
{
    public:
        //  Atributos
        int num_vertices;
        int *vertices; 
        int **arestas;
        char tipo;

        //  assinaturas dos metodos
        Grafo(int nv,char t);
        void printGrafo();

};
#endif

grafo.cpp

#include "grafo.h"

Grafo::Grafo(int nv,char t);  
Grafo::void printGrafo();

Grafo::Grafo(int nv,char t){
    num_vertices = nv;
    vertices = new int[num_vertices];
    tipo = t;

    //criar matriz
    arestas = new int *[num_vertices];
    for(int i = 0; i < num_vertices;i++){ arestas[i] = new int[num_vertices];}

    // inicializar valores da matriz
    for(int i = 0; i < num_vertices;i++){
        vertices[i] = 0;
        for(int j = 0; j < num_vertices;j++){
            arestas[i][j] = 0;
        }
    }

}

void Grafo::printGrafo(){
    std::cout << " | ";
    for(int i = 0; i < num_vertices;i++){
        std::cout << i << " ";
    }
    std::cout << std::endl;
        for(int i = -3; i < num_vertices;i++){
        std::cout << "_";
    }
    std::cout << std::endl;
    for(int i = 0; i < num_vertices;i++){
        std::cout << i << " | ";
        for(int j = 0; j < num_vertices;j++){
            std::cout << arestas[i][j] << " ";
        }
        std::cout << std::endl;
    }
}

main.cpp

#include "grafo.h"

#include <iostream>
#include <cstdlib>
#include <ctime>
#include <vector>

int main(){
  Grafo G = Grafo(5,'D');
  G.printGrafo();
}

当我尝试使用命令 g++ main.cpp -o main.exe 进行编译时。我收到以下错误消息:

/tmp/ccPxPLjS.o: 在函数main': main.cpp:(.text+0x29): undefined reference toGrafo::Grafo(int, char)' main.cpp:(.text+0x35): 未定义对“Grafo::printGrafo()”collect2 的引用:错误:ld 返回 1 个退出状态

有人可以帮我做这份工作吗? >.

【问题讨论】:

  • 你还没有定义 Grafo(int, char)Grafo::printGrafo()
  • 你已经定义了参数化构造函数
  • 关于构造函数,Grafo::Grafo(int, char):缺少实现(在grafo.hgrafo.cpp 中)。关于Grafo::printGrafo(),您可能忘记在对象列表中注明grafo.o 以链接您的应用程序。
  • 抱歉,我忘记在 Grafo.cpp 中包含 Grafo::Grafo

标签: c++ class header g++


【解决方案1】:

首先从Grafo.cpp 文件中删除第一行:

Grafo::Grafo(int nv,char t);  
Grafo::void printGrafo();

这些会导致编译错误 (GCC):

grafo.cpp:3:27: error: declaration of 'Grafo::Grafo(int, char)' outside of class is not definition [-fpermissive]
 Grafo::Grafo(int nv,char t);
                           ^
grafo.cpp:4:8: error: expected unqualified-id before 'void'
 Grafo::void printGrafo();
        ^~~~

然后将所有源文件包含到编译调用中:

g++ -o test test.cpp grafo.cpp

如果这样做,这将导致源代码正确编译:

g++ -o test test.cpp

它会导致您的问题中描述的类似错误:

ccVmPgnr.o:test.cpp:(.text+0x20): undefined reference to `Grafo::Grafo(int, char)'
ccVmPgnr.o:test.cpp:(.text+0x2c): undefined reference to `Grafo::printGrafo()'

【讨论】:

  • 工作正常。感谢您的帮助! :)
猜你喜欢
  • 2012-06-17
  • 2021-11-12
  • 2011-12-22
  • 1970-01-01
  • 1970-01-01
  • 2020-09-17
  • 1970-01-01
  • 2019-09-20
  • 2019-05-17
相关资源
最近更新 更多