7 Replies - 3515 Views - Last Post: 21 February 2010 - 12:22 PM Rate Topic: -----

#1 jchrispen   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Virtual functions and Link Errors

Posted 20 February 2010 - 11:53 PM

I'm stuck. When I compile my code I get several linker errors.

I have tried everything I can think of. If remove virtual and just create stub functions all but the last error goes away.

The last error has something to do with line of code 'moon = new MFactory();'

[Linker error] undefined reference to `ComputerAbstractFactory::CreateCPUDiag()'
[Linker error] undefined reference to `ComputerAbstractFactory::CreateMMUDiag()'
[Linker error] undefined reference to `ComputerAbstractFactory::CreateMBDiag()'
[Linker error] undefined reference to `vtable for ComputerAbstractFactory'
[Linker error] undefined reference to `MFactory::MFactory()'

Any help is appreciated.

class ComputerAbstractFactory {
      public:
             virtual CPUDiagnostics CreateCPUDiag(); //enforce these interfaces 
             virtual MMUDiagnostics CreateMMUDiag();
             virtual MBDiagnostics CreateMBDiag();
};

class MFactory : public ComputerAbstractFactory{
      private:
              static bool created;
              static MFactory * moon;    //only instance 
              MFactory();              //constructor
      public:
             //Singelton
             static MFactory * getInstance(){
                   if( !created ){
                       moon = new MFactory();
                       created = true;
                   }
                   else
                       cout << "There can be only one." << endl;
                       
                   return moon;
             };
             //Create Products
             CPUDiagnostics CreateCPUDiag(){
                   MoonCPU cpu;
                   return cpu;
             }; 
             MMUDiagnostics CreateMMUDiag(){
                   MoonMMU mmu;
                   return mmu;
             };
             MBDiagnostics CreateMBDiag(){
                   MoonMB mb;
                   return mb;
             };
};

bool MFactory::created = false;
MFactory * MFactory::moon = NULL;



Is This A Good Question/Topic? 0
  • +

Replies To: Virtual functions and Link Errors

#2 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Virtual functions and Link Errors

Posted 20 February 2010 - 11:58 PM

Did you implement the virtual functions of "ComputerAbstractFactory" class somewhere? If not, do it. Otherwise, you can make these functions pure virtual (they must be implemented only in the derived class). The syntax is:
virtual CPUDiagnostics CreateCPUDiag() = 0; //enforce these interfaces 
virtual MMUDiagnostics CreateMMUDiag() = 0;
virtual MBDiagnostics CreateMBDiag() = 0;


And if this is a header file, don't forget the header guards:
#ifndef MY_FILE_H // this must be the first line of your header file
#define MY_FILE_H

// code goes here

#endif // this must be the last line of your header file


And don't forget to include the header files in which "CPUDiagnostics", "MMUDiagnostics" and "MBDiagnostics" classes are defined!

This post has been edited by sarmanu: 20 February 2010 - 11:58 PM

Was This Post Helpful? 1
  • +
  • -

#3 Martyn.Rae   User is offline

  • The programming dinosaur
  • member icon

Reputation: 551
  • View blog
  • Posts: 1,432
  • Joined: 22-August 09

Re: Virtual functions and Link Errors

Posted 20 February 2010 - 11:58 PM

This is probably due to the fact that you have forgotten to include the file that contains the code you posted above.
Was This Post Helpful? 0
  • +
  • -

#4 ajwsurfer   User is offline

  • D.I.C Regular
  • member icon

Reputation: 21
  • View blog
  • Posts: 376
  • Joined: 24-October 06

Re: Virtual functions and Link Errors

Posted 21 February 2010 - 12:10 AM

here is what I am getting:

g++ test.cpp -o test
test.cpp:3: error: ‘CPUDiagnostics’ does not name a type
test.cpp:4: error: ‘MMUDiagnostics’ does not name a type
test.cpp:5: error: ‘MBDiagnostics’ does not name a type
test.cpp:26: error: ‘CPUDiagnostics’ does not name a type
test.cpp:30: error: ‘MMUDiagnostics’ does not name a type
test.cpp:34: error: ‘MBDiagnostics’ does not name a type
test.cpp: In static member function ‘static MFactory* MFactory::getInstance()’:
test.cpp:21: error: ‘cout’ was not declared in this scope
test.cpp:21: error: ‘endl’ was not declared in this scope
test.cpp: At global scope:
test.cpp:41: error: ‘NULL’ was not declared in this scope

which is constant with not declaring any of these types/classes (or including the files that contain them) before they are used:
CPUDiagnostics
MMUDiagnostics
MBDiagnostics
cout
endl
Was This Post Helpful? 0
  • +
  • -

#5 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Virtual functions and Link Errors

Posted 21 February 2010 - 12:16 AM

The compiler can't find
CPUDiagnostics
MMUDiagnostics
MBDiagnostics
cout
endl 


BE sure to include the files containing "CPUDiagnostics", "MMUDiagnostics" and "MBDiagnostics" to the file that you posted here. As for "cout" and "endl", you need to include <iostream>. Be careful to use "using namespace std", otherwise, you'll have to use std::cout and std::endl: http://forums.devshe...mean-45679.html
Was This Post Helpful? 0
  • +
  • -

#6 jchrispen   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Virtual functions and Link Errors

Posted 21 February 2010 - 07:35 AM

View Postsarmanu, on 20 February 2010 - 10:58 PM, said:

Did you implement the virtual functions of "ComputerAbstractFactory" class somewhere? If not, do it. Otherwise, you can make these functions pure virtual (they must be implemented only in the derived class).


This was most of the issue. It solves but all but one of the linker errors.

[Linker error] undefined reference to `MFactory::MFactory()'

I think it has to do with my getInstance() function in MFactory. I tried to make the constructor static but that's not really allowed. \

class MFactory : public ComputerAbstractFactory{
      private:
              static bool created;
              static MFactory * moon;    //only instance 
              MFactory();              //constructor
      public:
             //Singelton
             static MFactory * getInstance(){
                   if( !created ){
                       moon = new MFactory();
                       created = true;
                   }
                   else
                       cout << "There can be only one." << endl;
                       
                   return moon;
             };
             //Create Products
             CPUDiagnostics CreateCPUDiag(){
                   MoonCPU cpu;
                   return cpu;
             }; 
             MMUDiagnostics CreateMMUDiag(){
                   MoonMMU mmu;
                   return mmu;
             };
             MBDiagnostics CreateMBDiag(){
                   MoonMB mb;
                   return mb;
             };
};

bool MFactory::created = false;
MFactory * MFactory::moon = NULL;


Was This Post Helpful? 0
  • +
  • -

#7 sarmanu   User is offline

  • D.I.C Lover
  • member icon

Reputation: 967
  • View blog
  • Posts: 2,362
  • Joined: 04-December 09

Re: Virtual functions and Link Errors

Posted 21 February 2010 - 07:44 AM

MFactory(); 


That's the problem. The constructor is only defined (prototyped), but not implemented. You could just change the semicolon with {}:
MFactory() { /* empty body, acting like a default constructor */ };


This post has been edited by sarmanu: 21 February 2010 - 07:52 AM

Was This Post Helpful? 1
  • +
  • -

#8 jchrispen   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 45
  • Joined: 20-February 08

Re: Virtual functions and Link Errors

Posted 21 February 2010 - 12:22 PM

View Postsarmanu, on 21 February 2010 - 06:44 AM, said:

MFactory(); 


That's the problem. The constructor is only defined (prototyped), but not implemented. You could just change the semicolon with {}:
MFactory() { /* empty body, acting like a default constructor */ };



That solved it. Thank you.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1