简体   繁体   English

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

[英]Undefined reference error in c++

I am getting an undefined reference for one of the class methods in c++. 我正在为c ++中的类方法之一获得未定义的引用。 The error is "undefined reference to 'Department::getNameabi:cxx11' and other many lines for each method call of department. 错误是“部门的每个方法调用的对'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;
    }
}

my departments header file is as follows: 我部门的头文件如下:

#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();
};

Error 错误

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

All of Department 's member functions are only declared, not defined. Department的所有成员函数仅声明,未定义。

This means that the compiler acknowledges their existence and compiles main.cpp , where they are called, without any trouble. 这意味着编译器确认它们的存在并在没有任何麻烦的情况下编译main.cpp ,在其中进行调用。

When the compiler has done its job, it's the linker 's turn to "glue" the functions' implementations to their calls. 编译器完成工作后,该链接器将“实现”函数的实现“粘合”到其调用中。 But it doesn't find the implementations, so it rightfully complains. 但是它找不到实现,因此理所当然地抱怨。


Now, I guess you didn't just declare the member functions and forgot to define them. 现在,我想您不只是声明成员函数而忘记定义它们了。 You very likely have the definitions in a separate file called department.cpp which looks like this: 您很可能在一个名为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
}

You probably also thought that the inclusion of department.h by main.cpp would somehow magically pull in the code from department.cpp . 您可能还认为main.cpp包含department.h会以某种方式神奇地从department.cpp提取代码。

But that's not how C++ (or a typical C++ tool chain, for that matter) works. 但这不是C ++(或典型的C ++工具链)的工作方式。 The linker doesn't know about any implementations originating from source code in other files unless you tell it. 除非您告诉链接程序,否则链接程序不知道其他文件中源于源代码的任何实现。 As a matter of fact, not even the compiler knows about them in your case. 实际上,在您的情况下,甚至编译器也不了解它们。

You need to compile department.cpp and pass the resulting binary code to the linker. 您需要编译department.cpp并将生成的二进制代码传递给链接器。 The quick way to do so would be: 这样做的快捷方法是:

g++ -std=c++11 main.cpp department.cpp

Sooner or later, you will want a more elaborate process, in which each *.cpp file is compiled into an *.o file and the *.o files are then passed to the linker, all of that happening within a Makefile. 迟早,您将需要一个更复杂的过程,在该过程中,每个*.cpp文件都被编译为*.o文件,然后将*.o文件传递给链接器,所有这些操作都在Makefile中发生。

是否将部门的类实现包括在编译中?

g++ -std=c++11 main.cpp department.cpp

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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