views:

39

answers:

4

hey, i got undefined reference error from my main function, but i can not find the problem. my files are:

//Student.h
#ifndef STUDENT_H
#define STUDENT_H
#include <vector>
#include <string>

class Student{
public:
 Student(std::string &line);
...
virtual void evaluateValue(){}
 virtual ~Student(){}
....
};
#endif

//HeStudent.h
#ifndef HESTUDENT_H
#define HESTUDENT_H
#include "Student.h"

class HeStudent : public Student{
public:
 HeStudent(std::string line) : Student(line){
  ...
 }

 static int AgradesCount;
 static double Aaverage;

 virtual void evaluateValue();
 virtual ~HeStudent(){}

};
#endif

for each .h file there is his .cpp file. i've got also a main.cpp file which contains the main and in the main i create: Student stud = new HeStudent(line); i don't know if it's necessary but i included the Student.h and the HeStudent.h and i get some long error and it said:

HeStudent::HeStudent(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)]+0x22): undefined reference to `Student::Student

can anyone tell me what is the problem?

+2  A: 

A guess: The constructor of your class HeStudent calls the constructor of the base class Student:

class HeStudent : public Student{
public:
 HeStudent(std::string line) : Student(line){
 //                          ^^^^^^^^^^^^^^^
 //                          call to ctor of base class
 ...

However, at least in the code you've shown us, that base constructor is not actually defined:

class Student{
public:
 Student(std::string &line);
 ...                    // ^
                        // do you have a definition of the ctor's body somewhere?

Note the semicolon at the end of this line. Have you defined the constructor's body elsewhere or is it missing? In the latter case, that might explain your compilation error.


Update: Another guess then.

class Student{
public:
 Student(std::string &line);
 ...            //   ^
                // could this cause the problem?
stakx
as i said, each .h file has it's .cpp file with the implementation
or.nomore
+2  A: 

Are you compiling each .cpp files and not just the main.cpp ? If not, it is a linker problem:

g++ -c main.cpp
g++ -c student.cpp
g++ -c hestudent.cpp 
g++ main.o student.o hestudent.o
Cedric H.
Yes, i am compiling each .cpp file
or.nomore
Then I guess one of the other answers is correct :p
Cedric H.
but as i mentioned, the implementation is in the .cpp files
or.nomore
+2  A: 

Where are you putting the definition of your constructor:

Student(std::string &line);

I don't see it, and if it's missing, then that is at least one problem you have. You'll notice that you have {} after each of your other declarations which is also defining them. You constructor has no such curly brackets thus you must define the constructor somewhere else. (likely in a Student.cpp file)

You haven't shown the Student.cpp file, so you may think you have it defined and you don't. Make sure you have the Student(std::string &line) defined as:

Student::Student(std::string &line)
{
   // Any specific code you need here
}
RC
it's in a Student.cpp file
or.nomore
RC
A: 

Well, i found my problem, in the Student.cpp file i inline the constructor. did i changed the signature of the constructor when adding inline? why it fix my problem?

or.nomore
From the C++ FAQ on inline functions: Note: It's imperative that the function's definition (the part between the {...}) be placed in a header file, unless the function is used only in a single .cpp file. In particular, if you put the inline function's definition into a .cpp file and you call it from some other .cpp file, you'll get an "unresolved external" error from the linker.
RC