c ++中未定义的引用错误

时间:2017-04-17 08:34:01

标签: 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;
    }
}

我的部门头文件如下:

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

错误

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

2 个答案:

答案 0 :(得分:4)

所有Department个成员函数都只是声明的,未定义。

这意味着编译器确认它们的存在并编译main.cpp,它们被调用,没有任何麻烦。

当编译器完成其工作时,它是链接器转向&#34;粘合&#34;功能&#39;实现他们的调用。但它没有找到实现,所以它理所当然地抱怨。

现在,我想你并没有宣布成员函数而忘记定义它们。您很可能将这些定义放在名为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
}

您可能还认为department.h main.cpp的加入会以某种方式神奇地从department.cpp中提取代码。

但这并不是C ++(或典型的C ++工具链)的工作方式。链接器不知道源自其他文件中的源代码的任何实现,除非您告诉它。事实上,在你的情况下,编译器甚至都不知道它们。

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

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

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

答案 1 :(得分:-1)

Department类实现是否包含在compile中?

  <div id="vds" class="embed-responsive embed-responsive-16by9" style="margin-top: -100px;">
        <iframe class="embed-responsive-item" {% if profil.pageAccueil %}{%if profil.pageAccueil.video!=""%}src="{{profil.pageAccueil.video}}"{%else%} {{pageAccueil.video}} {%endif%}{%endif%}></iframe>
  </div>