简体   繁体   中英

Undefined symbols for architecture x86_64 - Compiling inherited class

I currently have an abstract User class and a Student class which inherits from user. I'm attempting to initialize an instance of the Student class from main. I'm receiving this error

Undefined symbols for architecture i386: "Student::Student()", referenced from: _main in ccJo7npg.o "Student::~Student()", referenced from: _main in ccJo7npg.o ld: symbol(s) not found for architecture i386 collect2: ld returned 1 exit status

User Class:

#include <iostream>
#import <stdio.h>
#import <string.h>
using namespace std;

class User
    {
public:
    void setName(const string n)
{
    name = n;
}
string getName()
{
    return name;
}
void setUsername(const string u)
{
    username = u;
}
string getUsername()
{
    return username;
}
void setPassword(const string p)
{
    password = p;
}
string getPassword()
{
    return password;
}
void setID(const int ID)
{
    this->ID=ID;
}
int getID()
{
    return ID;
}
void setClassID(const int cid)
{
    classID=cid;
}
int getClassID()
{
    return classID;
}
void logOut()
{
    cout<<"you have logged out"<<endl;
}
void print()
{
    cout<< "Student : "<< ID << name << " "<< username << " " << password << endl;
}
virtual void menu()=0;
protected:
   int classID, ID;
   string name, username, password;
};

Student Class:

#include <iostream>
#include "User.h"
using namespace std;

class Student: public User
{
public:
Student()
{
    classID=0;
    ID=0;
    username="";
    name="";
    password="";
}
~Student()
{
    cout<<"destructor"<<endl;
}
void studyDeck(const int i)
{

}
void viewScores(const int)
{

}
void viewScores()
{

}
virtual void menu()
{
    cout << "Student menu" << endl;
}
};

Main.cpp:

#include <iostream>
#include "User.h"
#include "Student.h"
using namespace std;

int main()
{
    Student s;
    return 0;
}

I'm compiling with g++ with " g++ User.cpp Student.cpp main.cpp "

Thanks!

GCC is not producing code for the Student constructor and destructor because they are defined inside of the class declaration. That's why those symbols are missing and generating a link error. At a minimum, you need to move the function bodies for the Student constructor and destructor outside of the class declaration and provide just the signature (no body) in the definition:

class Student: public User 
{
Student();
~Student();
...
};

You would define these function bodies in Student.cpp after the class definition as follows:

Student::Student()
{
    classID=0;
    ID=0;
    username="";
    name="";
    password="";
}

Student::~Student()
{
    cout<<"destructor"<<endl;
}

Though it's not necessary, you should separate all of the function definitions from their implementation. To do this, you would omit the class definition from the Student.cpp file; and instead include Student.h in Student.cpp (even though you didn't post Student.h, it appears to be correct or the program would not have compiled). In other words, "Student.h" would contain "class Student { ... };" with just function signatures and no function bodies inside the braces and "Student.cpp" would contain all of the function definitions with bodies such as:

void Student::menu()
{
    cout << "Student menu" << endl;
}

If you do this, you will also need #ifndef guards in the .h files as Kevin Grant explained. You would treat User.cpp and User.h in the same way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM