简体   繁体   中英

Undefined reference error in c++

I am getting an undefined reference for one of the class methods in c++. The error is "undefined reference to 'Department::getNameabi:cxx11' and other many lines for each method call of department.

#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.

This means that the compiler acknowledges their existence and compiles main.cpp , where they are called, without any trouble.

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:

#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 .

But that's not how C++ (or a typical C++ tool chain, for that matter) works. 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. 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.

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

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

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