开发者

undefined reference error

开发者 https://www.devze.com 2023-01-15 15:24 出处:网络
i got undefined reference error from my main function, but i can not find the problem. my files are: //Student.h

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(){}
开发者_JS百科 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?


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


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
}


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?


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?

0

精彩评论

暂无评论...
验证码 换一张
取 消