1

recently I started writing a little game in c++. (I'm using g++ (Ubuntu 7.3.0-27ubuntu1~18.04) 7.3.0 and gnu-make) However, I've encountered an error that I cannot seem to get rid of: socket_class.cpp: undefined reference to `vtable for tcgf::socket'

The program consists of the following source files: main.cpp, game_class.cpp, port_class.cpp socket_class.cpp, isocket_class.cpp, ssocket_class.cpp;

Every .cpp file, except for the main.cpp, has a corresponding .hpp file. I will use the '->' to substitute 'include(s)' to make what i'm about to write a little bit more friendly for the eyes.

The main.cpp -> the game_class.hpp, which -> the isocket_class.hpp and the ssocket_class.hpp, both of which -> the socket_class.hpp and the port_class.hpp

Also, I've dumped the contents of every file (excluding main.cpp) into a namespace called tcgf. I'm guessing there's some linker problem, but I don't know where. Any help would be greatly appreciated.

I've been searching the net for over three hours and I decided to directly ask the question here. I tried editing the makefile, but It didn't work. I'm pretty sure the root of the problem lies there, though. (And yes, I'm reading the make manual, but it's going slowly :D) I've browsed a couple pages that had the same (or similar) error in the header:

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

Here's the 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

Here's the error message from make and g++ when attempting to compile the program:

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

Here's a sample of the 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

And here's the 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.
phounix
  • 11
  • 4
  • I found the problem, I specified the wrong pathways to the files for linkage because I did not know that if for ex. a.cpp is in the folder sources, then after running g++ -c sources/a.cpp the object file would be generated in the directory above; But, I got it to work; Thanks anyway. – phounix Apr 12 '19 at 20:05

0 Answers0