C ++中的未定义参考错误

Bip*_*Adh 0 c++ compiler-errors

我正在为c ++中的类方法之一获得未定义的引用。错误是“部门的每个方法调用的对'Department :: getNameabi:cxx11'的未定义引用和其他许多行。

#include <iostream>
#include <fstream>
#include <cstdlib>
#include <vector>

#include "department.h"

using namespace std;

void addDepartment();
void writeToFile();
void showArt();
void removeDepartment();

vector <Department> departments;


int main()
{
    int choice;
    do
    {
        cout<<"1)Add asset \t 2)Remove Asset \t 3)Departmental Options \t 4)Quit"<<endl;
        cin>>choice;
        switch( choice )
        {
            case 1: addDepartment();
                    break;

            case 2: removeDepartment();
                    break;

            case 3: //showReport();
                    break;

        }

        addDepartment();
    }while(choice != 4);

}

void showArt()
{
    cout<<"ASSET MANGEMENT"<<endl;
}

void addDepartment()
{
    string name;
    cout<<"Ener department name"<<endl;
    cin>>name;
    vector<Department>::iterator it;
    int flag = 0;

    for( it = departments.begin(); it!=departments.end(); it++)
    {

        if (it->getName().compare(name) == 0)
        {
            flag = 1;
            it->addAsset();
            break;
        }
    }
    if (flag == 0 )
    {
        Department temp(name);
        temp.addAsset();
        departments.push_back(temp);
    }
}

void removeDepartment()
{
  string name;
    cout<<"Ener department name"<<endl;
    cin>>name;
    vector<Department>::iterator it;
    int flag = 0;

    for( it = departments.begin(); it!=departments.end(); it++)
    {
        if (it->getName().compare(name) == 0)
        {
            flag = 1;
            it->removeAsset();
            break;
        }
    }
    if (flag == 0 )
    {
        cout<<"No such department puta!"<<endl;
    }
}
Run Code Online (Sandbox Code Playgroud)

我部门的头文件如下:

#ifndef DEPARTMENT_H
#define DEPARTMENT_H
#endif

#include <cstring>
#include <vector>
#include <string>

#include "computer.h"
#include "software.h"
using namespace std;

class Department
{
    string name;
    vector<Computer> computers;
    vector <Software> softwares;

    void addComputer();
    void addSoftware();

    public:
    Department(string);
    void addAsset();
    void removeAsset();
    string getName();
};
Run Code Online (Sandbox Code Playgroud)

错误

bipgen@genbox:~/asset-management$ g++ -std=c++11 main.cpp
/tmp/ccvu6doW.o: In function `addDepartment()':
main.cpp:(.text+0x181): undefined reference to `Department::getName[abi:cxx11]()'
main.cpp:(.text+0x1cd): undefined reference to `Department::addAsset()'
main.cpp:(.text+0x220): undefined reference to `Department::Department(std::__cxx11::basic_string<char, std::char_traits<char>, std::a
llocator<char> >)'
main.cpp:(.text+0x23b): undefined reference to `Department::addAsset()'
/tmp/ccvu6doW.o: In function `removeDepartment()':
main.cpp:(.text+0x392): undefined reference to `Department::getName[abi:cxx11]()'
main.cpp:(.text+0x3d8): undefined reference to `Department::removeAsset()'
collect2: error: ld returned 1 exit status
Run Code Online (Sandbox Code Playgroud)

Chr*_*ckl 5

的所有Department成员函数仅声明,未定义。

这意味着编译器确认了它们的存在并main.cpp在没有任何麻烦的情况下进行了编译。

编译器完成工作后,该链接器将“实现”函数的实现“粘合”到其调用中。但是它找不到实现,因此理所当然地抱怨。


现在,我想您不只是声明成员函数而忘记定义它们了。您很有可能在一个单独的文件中包含这些定义,department.cpp如下所示:

#include "department.h"

void Department::addSoftware() {
    // implementation
}

Department::Department(string) {
    // implementation
}

void Department::addAsset() {
    // implementation
}

void Department::removeAsset() {
    // implementation
}

string Department::getName() {
    // implementation
}
Run Code Online (Sandbox Code Playgroud)

您可能还认为,department.hby 的包含main.cpp会以某种方式神奇地从中引入代码department.cpp

但这不是C ++(或典型的C ++工具链)的工作方式。除非您告诉链接程序,否则链接程序不知道其他文件中源于源代码的任何实现。实际上,在您的情况下,甚至编译器也不了解它们。

您需要编译department.cpp并将生成的二进制代码传递给链接器。这样做的快捷方法是:

g++ -std=c++11 main.cpp department.cpp
Run Code Online (Sandbox Code Playgroud)

迟早,您将需要一个更复杂的过程,在该过程中,每个*.cpp文件都被编译成一个*.o文件,*.o然后将这些文件传递给链接器,所有这些都在Makefile中发生。