LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Undefined references to vtable in class constructor? (https://www.linuxquestions.org/questions/programming-9/undefined-references-to-vtable-in-class-constructor-420524/)

RavenOfOdin 03-01-2006 02:04 AM

Undefined references to vtable in class constructor?
 
Hi, got a program which I've been trying to port to Linux for a while now - and after all the changes I can think of making, the build fails with a problem related to one of my main classes. Compilation on Win32 was done with Dev-C++ 4.9.9.2 and on n*x I've been using Anjuta 1.2.4. I've posted on another site about this but so far have had absolutely no luck.

I've been at this for a couple of more days but I can't think of what could be wrong with the below sections of code, in red. The build log and those sections follow:

(PS: Before anyone thinks of this, yes I do have virtual functions present in class Sockets, thus the destructor.)

Build Log:

Code:

Building the whole Project: ConsoleL...
make -s
Making all in po
Making all in src
g++ -Wall -g -g -O2 -o consolel main.o
main.o:In function 'main':
/home/rg40/projects/ConsoleL/src/main.cc:596:undefined reference to Sockets::Sockets(Sockets const&)
/home/rg40/projects/ConsoleL/src/main.cc:681:undefined reference to Sockets::Sockets(Sockets const&)
main.o:In function '~Sockets':
../include/network.h:482:undefined reference to 'vtable for Sockets'
main.o:In function 'Sockets':
../include/network.h:405:undefined reference to 'vtable for Sockets'
collect2: ld returned 1 exit status
make[2]: *** consolel [Error 1]
make[1]: *** all-recursive [Error 1]
make: *** all-recursive-am [Error 2]
Completed ... unsuccessful
Total time taken: 11 secs

Code:
Source file:
Code:


    {     
    HostRes hr; 
    Sockets sck;
    #ifdef DEBUG_D
    cout << "Debug (production) mode enabled." << endl;
    cout << "=================================" << endl;
    cout << "\n\n" << endl;
    cout << "recvSocket before hr.ResolutionFunc() is " << hr.recvSocket << endl;
    #endif
    hr.ResolutionFunc(&ndata, sck, sck.Connection, SEND_PORT, &A, SOCK_READ);
    }

. . .

    if(getpeername(hr.recvSocket, (struct sockaddr *)&hr.host, &hr.tolen) == MINUS_ONE)
        {
                      perror("getpeername");
                      exit(1);
        }   
 
    if(!hr.recvSocket)
        { 
          std::cout << "Connect failed." << endl;
          sock.CLSocks(Main::i, Main::j, SOCK_READ);
          #ifdef WIN32
                        WSACleanup();
          #endif
                        return(SOCKET_INIT_FAIL);         
        }

sock.ConnInitialize(&ndata, sock.c, sock);

char* c = const_cast<char*>(argv[1]);

if(isdigit(argv[1][0]))
  {
  cout << "Connected to " << address << " \\ " << FlagVars::NameBuffer << " \n";
  }
else
  {
  cout << "Connected to " << c << " \\ " << FlagVars::NameBuffer << " \n";     
  }


Include file "network.h":
Code:


    class Sockets

            {

            private:

              friend class HostRes;

              char HostResAddress[32];

             
              . . .


            protected:

              int IntFile;

              void* File;

              int* SocketConstruct;

              int* SocketIntConstruct;
           
            . . .

            public:

      Sockets(){SocketConstruct = new int[1024*2]; return;};

            Sockets(int){SocketIntConstruct = new int[1024*2]; return;};

            Sockets::Sockets(const Sockets&);

            char *params;

            std::string c;



           
            . . . //rest of class is here
 


      virtual ~Sockets(){delete [] Sockets::SocketConstruct;};       

            };

What could be going wrong?

dmail 03-01-2006 03:20 AM

The first error is probably due to you having the scope operator in the class declaration.
Code:

Sockets::Sockets(const Sockets&);
//try changing this to
Sockets(const Sockets&);

And see what errors you get then.

Plus does the copy constructor allocated memory? hmmm you are using the scope operator again. (is there a reason for this?)
I find it better to include a small check to be safe, like
Code:

{delete [] Sockets::SocketConstruct;};
Code:

{if(SocketConstruct)delete [] SocketConstruct;}

and lastly why do you have two pointers instead of just the one
Code:

int* SocketConstruct;
int* SocketIntConstruct;

it maybe an idea to enclude both these in a check like above

RavenOfOdin 03-01-2006 05:46 PM

Quote:

Originally Posted by dmail
The first error is probably due to you having the scope operator in the class declaration.
Code:

Sockets::Sockets(const Sockets&);
//try changing this to
Sockets(const Sockets&);

And see what errors you get then.

>.<
I took it out completely and that got rid of the error. Sockets() is handling the class fine for now.

Quote:

Originally Posted by dmail
Plus does the copy constructor allocated memory? hmmm you are using the scope operator again. (is there a reason for this?)
I find it better to include a small check to be safe, like
Code:

{delete [] Sockets::SocketConstruct;};
Code:

{if(SocketConstruct)delete [] SocketConstruct;}

No reason, I just like strict guidelines. I'll take you up on that though and see what happens.

Quote:

Originally Posted by dmail
and lastly why do you have two pointers instead of just the one
Code:

int* SocketConstruct;
int* SocketIntConstruct;


That was for overloading purposes, if I had to work with Sockets() and Sockets(int) . . .
But I guess I don't really need that.

Anywho, it builds fine now. The "undefined reference to vtable" errors were easy enough to get rid of by including the main virtual functions of Sockets() in its first derived class - without the "virtual" specifier.

Thanks for the input.


All times are GMT -5. The time now is 06:25 PM.