我在C++中的一个类方法中遇到了undefined reference错误。错误信息为:“undefined reference to 'Department::getNameabi:cxx11',并且每个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 << "Enter 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 << "Enter 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