[英]Undefined reference error in c++
我正在為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
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
}
您可能還認為main.cpp
包含department.h
會以某種方式神奇地從department.cpp
提取代碼。
但這不是C ++(或典型的C ++工具鏈)的工作方式。 除非您告訴鏈接程序,否則鏈接程序不知道其他文件中源於源代碼的任何實現。 實際上,在您的情況下,甚至編譯器也不了解它們。
您需要編譯department.cpp
並將生成的二進制代碼傳遞給鏈接器。 這樣做的快捷方法是:
g++ -std=c++11 main.cpp department.cpp
遲早,您將需要一個更復雜的過程,在該過程中,每個*.cpp
文件都被編譯為*.o
文件,然后將*.o
文件傳遞給鏈接器,所有這些操作都在Makefile中發生。
是否將部門的類實現包括在編譯中?
g++ -std=c++11 main.cpp department.cpp
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.