-2

我在 linux mint 中使用 NetBeans IDE 进行 C++ 编程。NetBeans 可以很好地编译代码,但是当我尝试main.cpp使用 g++编译时,出现错误:g++ main.cppundefined reference

我知道有很多这样的问题,他们可以通过使用正确的 -l 库来解决问题,但我找不到与 NetBeans/g++ 相关的任何问题,我不知道如何克服这个问题。任何帮助表示赞赏。

错误:

tmp/ccXSo5xI.o: In function `main':
main.cpp:(.text+0x82): undefined reference to `Hospital::Hospital(int, int, int)'
main.cpp:(.text+0xf3): undefined reference to `Resident::Resident(int, int)'
.
.
.

主文件

#include <iostream>
#include <cstdio>
#include "Resident.h"
#include "Hospital.h"
#include <stack>

using namespace std;

void readFromFiles(int [][10], int [][10]);
void readFromFiles(Hospital*[10], Resident*[10]);
void print10(int [][10], int [][10]);
void print10(Hospital*[10], Resident*[10]);

int main(void) {
    int hospital[10][10] = {0};
    int resident[10][10] = {0};
    Hospital **hospitals = new Hospital*[10];
    for(int i = 0 ; i < 10 ; i++)
        hospitals[i] = new Hospital(3, i, 10);  //3: quota, i: hospital number, 10:prefereneceLength
    Resident **residents = new Resident*[10];
    for(int i = 0 ; i < 10 ; i++)
        residents[i] = new Resident(i,10);  //i: hospital number, 10:prefereneceLength
.
.
.

医院.h

#ifndef HOSPITAL_H
#define HOSPITAL_H

#include "Resident.h"

using namespace std;

class Hospital {
public:
    //constructors & destructors
    Hospital();
    Hospital(const int, const int, const int);
    Hospital(const Hospital& orig);
    virtual ~Hospital();

    //getters & setters
    int getNumber();
    int getQuota();
    int** getPreferenceList();

    //member functions
    void addPreference(const int, const int);

private:
    int number;
    int* preferenceList[2]; //1st row: preference order ; 2nd row: admission status
    int quota;
    Resident *admittedResidents;
};

#endif  /* HOSPITAL_H */

居民.h

#ifndef RESIDENT_H
#define RESIDENT_H

class Resident {
public:
    //constructors & destructors
    Resident();
    Resident(const int, const int);
    Resident(const Resident& orig);
    virtual ~Resident();

    //getters, setters
    int getNumber();
    int* getPreferenceList();
    bool getAdmissionStatus();

    //member functions
    void addPreference(const int, const int);
private:
    int number;     //resident number
    int proposalCount;
    int* preferenceList;    //not inverse, unlike Hospitals pref list
    bool admissionStatus;
};

#endif  /* RESIDENT_H */

我知道这是很多代码,即使我缩短了它们,但我认为它们是必要的部分。谢谢。

4

2 回答 2

1

You need to include all source files:

g++ main.cpp hospital.cpp resident.cpp

于 2013-03-17T21:50:24.707 回答
0

g++ main.cpp will try to compile and link main.cpp into an executable (a.out). In the link stage, ld is not finding the symbols for the Hospital and Resident constructors while are called in main.cpp because they have not yet been compiled.

If you just want to compile main.cpp, use:

g++ -c main.cpp

You can link the object files later using:

g++ main.o hospital.o resident.o 

If you want to compile and link the whole lot:

g++ main.cpp hospital.cpp resident.cpp 
于 2013-03-17T21:52:39.993 回答