LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Search this Thread
Old 03-01-2006, 02:04 AM   #1
RavenOfOdin
Member
 
Registered: Feb 2006
Location: Arkansas, C.S.A
Distribution: Ubuntu 6.06 LTS, PC / Debian Etch 2.6.16.17 PPC
Posts: 60

Rep: Reputation: 15
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?
 
Old 03-01-2006, 03:20 AM   #2
dmail
Member
 
Registered: Oct 2005
Posts: 970

Rep: Reputation: Disabled
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

Last edited by dmail; 03-01-2006 at 03:28 AM.
 
Old 03-01-2006, 05:46 PM   #3
RavenOfOdin
Member
 
Registered: Feb 2006
Location: Arkansas, C.S.A
Distribution: Ubuntu 6.06 LTS, PC / Debian Etch 2.6.16.17 PPC
Posts: 60

Original Poster
Rep: Reputation: 15
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.

Last edited by RavenOfOdin; 03-01-2006 at 05:48 PM.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Homework Help: Copy Constructor on a template class. MicahCarrick Programming 2 01-22-2006 10:43 PM
undefined reference to ' vtable for CData' bianchi Programming 4 12-29-2005 02:48 AM
copy constructor for class containing array of vaiable size objects ? qwijibow Programming 4 12-21-2005 09:50 PM
texinfo - undefined references during make fitret Linux - Software 1 06-18-2005 02:38 PM
Class constructor not being called ChimpFace9000 Programming 1 06-03-2002 08:54 PM


All times are GMT -5. The time now is 09:10 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration