0

Possible Duplicate:
Undefined reference to vtable

I have a class Student on the file - student.h and this is its constructor:

class Student
{
protected:
  double _id;
  double _salary;
  double _grade_average;

public:
    Student(double id, double salary,double average):
        _id(id),_salary(salary),_grade_average(average)
    {}
};

And then I get the error:

undefined reference to 'vtable for Student'

What is the problem?

this is a University.h file:

 #include "Student.h"
class University : public Student
{
public:
  virtual double getValue();
  University(char university_id,double id, double average,double salary):
    _university_id(university_id),Student(id,average,salary)
  {
  }

private:
  char _university_id;
  static double how_many;
  static double sum_avg_grades;
};
Community
  • 1
  • 1
fgfjhgrjr erjhm
  • 325
  • 1
  • 10
  • 24
  • Please post a real code demonstrating the problem – BЈовић Sep 07 '11 at 13:01
  • 2
    And it's probably un _d_ efined, not un _r_ efined. – Seth Carnegie Sep 07 '11 at 13:01
  • How are you using this class in your code? – wkl Sep 07 '11 at 13:01
  • yes, the other files are headers that inherits it, and their implementations – fgfjhgrjr erjhm Sep 07 '11 at 13:01
  • 3
    Use `g++`, not `gcc` for the linking... – Kerrek SB Sep 07 '11 at 13:02
  • You're gonna need to tell us which compiler, and which version, you're using... – Sean Sep 07 '11 at 13:03
  • The problem is you are not providing enough information. Your statement above is akin too: "The plane is late. Why is the plane late?". How should I know why the plain is late you have not even given me the flight number. A **fully compilable example** of how to generate the error should be what you post. You should strip it down so that you only include the important parts (The processes of doing this will probably cause you to discover the problem). – Martin York Sep 07 '11 at 13:07

1 Answers1

2

A couple of things.

  1. Put the underscore after the variable name. As names that start with an underscore may be used by the compiler.
  2. Make sure to define at least one virtual function if you plan on inheriting from it or using RTTI. (ex. a Virtual Destructor)
  3. Make sure that every declared function has a corresponding implementation.
Skyler Saleh
  • 3,961
  • 1
  • 22
  • 35
  • The "don't start variable names with underscores" only counts for globals. – Seth Carnegie Sep 07 '11 at 13:06
  • 2
    Ok problem solved. I had a virtual function without imnplementation, so I fixed it to virtual double getValue() =0; – fgfjhgrjr erjhm Sep 07 '11 at 13:10
  • @Seth Carnegie while that is true, variables that start with an underscore and a capital letter, or variables with double underscores(__var) are reserved everywhere so it is best to just not start variable names with an underscore. – Skyler Saleh Sep 07 '11 at 13:14
  • 1
    @RTS As you've yourself said, it is variables starting with an underscore *and* a capital letter that are reserved. I myself prefer naming private variables with a trailing underscore, but the leading underscore is also pretty common, and I don't think it violates any guideline. After all, C++ is case sensitive. – Praetorian Sep 07 '11 at 13:19