2

all

I bet this is a frequently asked question. But each time it is raised with a different context. Here is my problem: I have a project in VS2012 Express. The project include few header files. It is meaningless to paste the whole project here, thus I just give a brief structure.

In header file Header.h, I have a parent class Sim which is defined as follows:

class Sim
{
    protected:
    // some member data
    public:
       Sim(int x, int y, A a);
       vector<int> return_parameters(A a);  // problems happen at this function
       // class A is defined in another header file and properly included here
       // function return_parameters has no definition, thus is an abstract function
}

Sim::Sim(int x, int y, A a)
{// some operations}

Then I have class Sim_son that public derived from Sim

  class Sim_son: public Sim
  {
      private:
        // some member data
      public:
        Sim_son(int x, int y, A a, int z);
        vector<int> return_parameters(A a);  
   };

  Sim_son::Sim_son(int x, int y, A a, int z): Sim(x,y, a)
  {
     // some operations
  }

  vector<int> Sim_son::return_parameters(A a)
  {
     return a.someData;
  }

In main(), I code like this:

  void main()
  {
     // some operations
     Sim_son Obj_son(x, y, a, z);
     Obj_son.return_parameters(a)    //PROBLEM HAPPENS
  }

The compiler gives a LNK2019 error which says unresolved external symbol Sim::return_parameters referenced in function Sim_son::Sim_son(void).

I commeted the line "Obj_son.return_parameters(a)", then the error disappears. So I guess this is the source of trouble.

Can anyone help me out of this? Many thanks in advance.

Best regards Long

ChangeMyName
  • 7,018
  • 14
  • 56
  • 93
  • Welcome to Stack Overflow! You don't need to include signature in your post - your user card is added automatically. Read [Help](http://stackoverflow.com/help/behavior) for more details. – Artemix Jul 30 '13 at 11:55

2 Answers2

0

First of all, your main should look like

int main() {
    return 0;
}

Secondly, do you have a ; after the following statement?

Obj_son.return_parameters(a)

Also, your class 'Sim' needs to end with a ;.

oddRaven
  • 672
  • 1
  • 7
  • 20
Oleksiy
  • 37,477
  • 22
  • 74
  • 122
0

In the declaration of class Sim, you are telling the compiler that you have a function named return_parameters, but you are not giving it an implementation therefore the object code of Sim::return_parameters is not generated. It's fine as long as you don't call this function because its object code is not required. But when you call it, the linker needs its object code and can't find it, so it gives an error.

This happens because you didn't tell the compiler that return_parameters is a virtual function. Try:

class Sim
{
    protected:
    // some member data
    public:
       Sim(int x, int y, A a);
       virtual vector<int> return_parameters(A a) = 0;
}

This will make return_parameters a pure virtual function and therefore an instance of Sim cannot be generated.

Or if you don't want Sim to be an abstract class, you can mark return_parameters virtual and provide a default implementation for it. Like this:

class Sim
{
    protected:
    // some member data
    public:
       Sim(int x, int y, A a);
       virtual vector<int> return_parameters(A a)
       {
           return vector<int>();
       }
}

The problem is that not providing a function body does not make a function virtual. Hence, the keyword virtual is provided :)

It seems to me that you need a little bit more training on virtual functions. Take a look at this link: C++ Polymorphism

erelender
  • 6,175
  • 32
  • 49