以下代码中可能导致“未定义引用”错误的原因是什么?

时间:2019-04-12 19:14:52

标签: c++ compiler-errors

最近我开始用C ++编写一个小游戏。 (我正在使用g ++(Ubuntu 7.3.0-27ubuntu1〜18.04)7.3.0和gnu-make) 但是,我遇到了一个似乎无法摆脱的错误: socket_class.cpp:未定义对tcgf :: socket的vtable的引用

该程序包含以下源文件: main.cpp,game_class.cpp,port_class.cpp,socket_class.cpp,isocket_class.cpp,ssocket_class.cpp;

除main.cpp之外,每个.cpp文件都有对应的.hpp文件。 我将使用'->'代替'include(s)',使我要写的内容对眼睛更加友好。

main.cpp-> game_class.hpp,其中-> isocket_class.hpp和ssocket_class.hpp,两者-> socket_class.hpp和port_class.hpp

此外,我已经将每个文件(不包括main.cpp)的内容转储到名为tcgf的命名空间中。 我猜有一个链接器问题,但我不知道在哪里。 任何帮助将不胜感激。

我已经在网上搜索了三个多小时,因此决定直接在这里提出问题。我尝试编辑Makefile,但是没有用。 我很确定问题的根源就在这里。 (是的,我正在阅读制造手册,但是进展缓慢:D) 我浏览了几个页眉中具有相同(或相似)错误的页面:

http://gcc.gnu.org/ml/gcc-help/2005-04/msg00233.html Undefined reference to vtable https://www.daniweb.com/programming/software-development/threads/114299/undefined-reference-to-vtable-for undefined reference to 'vtable for class' constructor c++ undefined reference to vtable

这是makefile:

CXX=g++
  2 CXXFLAGS=-g -Wall -Wextra -Werror
  3 SFML=-lsfml-system -lsfml-window -lsfml-graphics -lsfml-audio -lsfml-network
  4 OBJECTS=sources/main.o sources/game_class.o sources/socket_class.o sources/port_class.o sources/isocket_class.o sources/ssocket_class.o
  5 SOURCES=$(OBJECTS:.o=.cpp)
  6 
  7 SAO: $(OBJECTS)
  8     $Stack Overflow requires cookies for authentication -- are your browser cookies enabled for this domain?(CXX) -o SAO $(OBJECTS) $(SFML)
  9     make organize
 10 
 11 sources/main.o : sources/main.cpp
 12     $(CXX) -c $(CXXFLAGS) sources/main.cpp
 13 sources/game_class.o : sources/game_class.cpp headers/game_class.hpp
 14     $(CXX) -c $(CXXFLAGS) sources/game_class.cpp
 15 sources/isocket_class.o : sources/isocket_class.cpp headers/isocket_class.hpp
 16     $(CXX) -c $(CXXFLAGS) sources/isocket_class.cpp
 17 sources/ssocket_class.o : sources/ssocket_class.cpp headers/ssocket_class.hpp
 18     $(CXX) -c $(CXXFLAGS) sources/ssocket_class.cpp
 19 sources/socket_class.o : sources/socket_class.cpp headers/socket_class.hpp
 20     $(CXX) -c $(CXXFLAGS) sources/socket_class.cpp
 21 sources/port_class.o : sources/port_class.cpp headers/port_class.hpp
 22     $(CXX) -c $(CXXFLAGS) sources/port_class.cpp
 23 
 24 organize:
 25     mv sources/*.o binaries

这是尝试编译程序时来自make和g ++的错误消息:

g++ -c -g -Wall -Wextra -Werror sources/socket_class.cpp
g++ -o SAO sources/main.o sources/game_class.o sources/socket_class.o sources/port_class.o sources/isocket_class.o sources/ssocket_class.o -lsfml-system -lsfml-window -lsfml-graphics -lsfml-audio -lsfml-network
sources/socket_class.o: In function `tcgf::socket::socket(int)':
socket_class.cpp:(.text+0xe): undefined reference to `vtable for tcgf::socket'
sources/socket_class.o: In function `tcgf::socket::~socket()':
socket_class.cpp:(.text+0x31): undefined reference to `vtable for tcgf::socket'
sources/isocket_class.o:(.data.rel.ro._ZTIN4tcgf7isocketE[_ZTIN4tcgf7isocketE]+0x10): undefined reference to `typeinfo for tcgf::socket'
sources/ssocket_class.o:(.data.rel.ro._ZTIN4tcgf7ssocketE[_ZTIN4tcgf7ssocketE]+0x10): undefined reference to `typeinfo for tcgf::socket'
collect2: error: ld returned 1 exit status
makefile:8: recipe for target 'SAO' failed
make: *** [SAO] Error 1

这是socket_class.hpp的示例:

#ifndef _SOCKET_CLASS_HPP
  2 #define _SOCKET_CLASS_HPP
  3 #include <string>
  4 #include <fstream>
  5 namespace tcgf
  6 {
  7     class socket
  8     {
  9         protected:
 10             const int AOP; // amount of ports
 11         public:
 12             socket(int);
 13             ~socket();
 14             int size() const { return AOP; }
 15             friend std::ostream &operator<<( std::ostream& stream, socket& Socket )
 16             {
 17                 Socket.status();
 18                 return stream;
 19             }
 20             virtual int get_pnumber(std::string)=0;
 21             virtual int is_pn_valid(std::string)=0;
 22             virtual int is_pn_valid(int)=0;
 23             virtual void status()=0;
 24             virtual void pstatus(int)=0;
 25             virtual void set_pname(int, std::string)=0;
 26             virtual void set_pname(std::string, std::string)=0;
 27             virtual void set_pstate(int, std::string)=0;
 28             virtual void set_pstate(std::string, std::string)=0;
 29     };
 30 }
 31 #endif

这是socket_class.cpp:

#include "../headers/socket_class.hpp"
  2 namespace tcgf
  3 {
  4     socket::socket(int x) : AOP{x}
  5     {
  6     }
  7     socket::~socket()
  8     {
  9     }
 10 }
``````````````````````````````````````````````````````````````````````

Well (obviously), I would like for everything to compile nicely,
but unfortunately that's not the case. I just don't know anymore
what is causing this nasty error. I suspect that there might be some linker problem because the socket_class.hpp is an abstract class with virtual functions, and it is also the superclass of the i/ssocket classes.

0 个答案:

没有答案