G ++未定义对类::函数的引用 [英] G++ undefined reference to class::function
问题描述
我终于绝望了.因此,在我的 c++ 课程中,我们被指示使用类.我们会让头文件声明类和函数,而单独的 .cpp 文件实现它.事情应该是有效的,但他们没有,而且网络上没有任何解决方案似乎对我有用.为此,我在 linux 上使用 G++ 编译器,它似乎不适用于 IDE 或普通命令行.
我在 TBook.h 中遇到的错误是:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o:在函数`_start'中:(.text+0x20): 未定义的对main"的引用/tmp/ccxqI6An.o:在函数TBook::TBook()"中:TBook.cpp:(.text+0x3b): 未定义对 `Telephone::Telephone()' 的引用TBook.cpp:(.text+0x100): undefined reference to `Telephone::Telephone()'TBook.cpp:(.text+0x132): 未定义引用 `Telephone::allNum(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'TBook.cpp:(.text+0x182): undefined reference to `Telephone::~Telephone()'TBook.cpp:(.text+0x191): undefined reference to `Telephone::~Telephone()'TBook.cpp:(.text+0x2b3): undefined reference to `Telephone::~Telephone()'TBook.cpp:(.text+0x2e6): undefined reference to `Telephone::~Telephone()'TBook.cpp:(.text+0x2fa): undefined reference to `Telephone::~Telephone()'/tmp/ccxqI6An.o:TBook.cpp:(.text+0x370): 更多未定义的对 `Telephone::~Telephone()' 的引用如下/tmp/ccxqI6An.o:在函数TBook::write()"中:TBook.cpp:(.text+0x4e1): 未定义对 `Telephone::getNumber()' 的引用TBook.cpp:(.text+0x506): 未定义对 `Telephone::getAreaCode()' 的引用TBook.cpp:(.text+0x53a): 未定义对 `Telephone::getName()' 的引用/tmp/ccxqI6An.o:在函数TBook::lookup(std::basic_string, std::allocator>, std::basic_string<char,std::char_traits<char>,std::allocator<char>>)'/tmp/ccxqI6An.o:在函数TBook::print()"中:TBook.cpp:(.text+0x880): 未定义对 `Telephone::getName()' 的引用TBook.cpp:(.text+0x8e0): 未定义对 `Telephone::getNumber()' 的引用TBook.cpp:(.text+0x8ff): 未定义对 `Telephone::getAreaCode()' 的引用collect2: ld 返回 1 个退出状态[在 0.3 秒内完成,退出代码为 1]
我有点不喜欢文件没有接收任何 Telephone 类的方法.TBook.h 的代码如下所示:
#ifndef TBOOK_H#define TBOOK_H#include "电话.h"类 TBook{私人的:电话通讯录[10];国际电流;最大整数;上市:书();〜TBook();无效添加(电话);无效写入();布尔 is_full();无效打印();无效检查();电话查询(字符串);};#万一
这就是 TBook.cpp 的样子:
#include <iostream>#include <fstream>#include <字符串>#include "TBook.h"#include "电话.h"使用命名空间标准;TBook::TBook(){当前= 0;最大值 = 9;cout <<你好"<<结束;字符串线;ifstream myfile ("rolodex.txt");如果(myfile.is_open()){while ( getline (myfile,line) ){cout <<行 <<结束;电话 t;t.allNum(行);添加(t);}我的文件.close();}否则如果(!myfile.is_open()){我的文件流;myfile.open("rolodex.txt");我的文件 <<这是一个空文件(相对而言).";我的文件.close();}}TBook::~TBook(){}void TBook::add(电话 tel){如果(!is_full()){rolodex[当前] = 电话;当前++;}}无效的 TBook::write(){我的文件流;myfile.open("rolodex.txt");for (int i = 0; i < 当前; ++i){我的文件 <
由于问题似乎与 Telephone 类有关,我想我也应该显示该代码
<块引用>电话.h
..
#ifndef TELEPHONE_H#define TELEPHONE_H#include <iostream>#include <字符串>使用标准::字符串;类电话{私人的:字符串名称;字符串编号;int areaCode;上市:电话(整数,字符串,字符串);电话();〜电话();bool setAreaCode(int);//二传手无效集合名称(字符串);无效集数(字符串);bool allNum(字符串);//吸气剂字符串 getName();字符串 getNumber();int getAreaCode();//检查bool checkX(int);bool checkY(int);};#万一
<块引用>
电话.cpp
..
#include <iostream>#include <字符串>#include <stdio.h>#include <stdlib.h>#include "电话.h"使用命名空间标准;电话::电话(){区号 = 0名称 = "";数 = "";}Telephone::Telephone(int aCode, string nam, string number){areaCode = aCode;名字=名字;}电话::~电话(){//不,纳达}bool Telephone::allNum(string all){size_t 找到 = all.find_first_of(",");//string first = all.substr(0, found);string second = all.substr((found)+1, found+1);第三个字符串 = all.substr(all.find_last_of(",")+1, all.length());整数 x, y;//将字符串转换为int值如果(第三个长度()== 7){x = atoi(third.substr(0,3).c_str()),y = atoi(third.substr(3,4).c_str());}别的{cerr <<您的电话号码无效"<<结束;}int ac = atoi(second.substr(0, second.length()).c_str());设置名称(第一个);如果(!setAreaCode(ac)){设置区域代码(100);返回真;}if (!checkX(x) || !checkY(y)){setNumber("1000000");}别的{setNumber(第三个);}cerr <<"提供的信息无效" <<结束;返回假;}void Telephone::setNumber(string number){数字=数字;}布尔电话::setAreaCode(int aCode){if(aCode >= 100 && aCode <= 999){areaCode = aCode;返回真;}返回假;}void Telephone::setName(string theName){名称 = 名称;}布尔电话::checkX(int x){如果(x >= 100 && x <= 999){返回真;}cerr <<"前三位无效" <<结束;返回假;}布尔电话::checkY(int y){如果(y >= 0000 && y <= 9999){返回真;}cerr <<"后四位无效" <<结束;返回假;}//吸气剂字符串电话::getName(){返回名称;}字符串电话::getNumber(){返回号码;}int 电话::getAreaCode(){返回区号;}
我的主文件(也称为 test.cpp)如下所示:
<块引用>test.cpp
#include <iostream>#include <字符串>#include "TBook.h"#include "电话.h"使用命名空间标准;int main(int argc, char const *argv[]){//创建一个通讯录TBook 地址簿;返回0;}
我在 test.cpp 中也遇到了这个错误
/tmp/ccl8anRb.o:在函数main"中:test.cpp:(.text+0x24): 未定义对 `TBook::TBook()' 的引用test.cpp:(.text+0x38): 未定义对 `TBook::~TBook()' 的引用collect2: ld 返回 1 个退出状态
我认为这主要是一个编译错误,但我仍然不确定,我觉得我是模因我的代码不起作用,我不知道为什么"的设置.通常我会 bash 代码并尝试不同的方法直到它起作用,但我根本没有时间.因此我需要你的帮助.
这是一个链接器错误.试试:
g++ test.cpp Telephone.cpp -o test
基本上,链接器抱怨您使用但没有提供实现的函数.要查看编译器为您执行的所有步骤,请输入 -v:
g++ -v test.cpp Telephone.cpp -o test
I'm finally pretty desperate. So, in my c++ class we were instructed to use classes. We'd have the header file declare the class and functions while a separate .cpp file implements the it. Things should be working, but they're not and no solutions on the web seem to be working for me. I'm using the G++ compiler on linux for this, and it doesn't seem to work on either IDE's or the normal command line.
The error I'm getting in my TBook.h is this:
/usr/lib/gcc/x86_64-linux-gnu/4.6/../../../x86_64-linux-gnu/crt1.o: In function `_start':
(.text+0x20): undefined reference to `main'
/tmp/ccxqI6An.o: In function `TBook::TBook()':
TBook.cpp:(.text+0x3b): undefined reference to `Telephone::Telephone()'
TBook.cpp:(.text+0x100): undefined reference to `Telephone::Telephone()'
TBook.cpp:(.text+0x132): undefined reference to `Telephone::allNum(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
TBook.cpp:(.text+0x182): undefined reference to `Telephone::~Telephone()'
TBook.cpp:(.text+0x191): undefined reference to `Telephone::~Telephone()'
TBook.cpp:(.text+0x2b3): undefined reference to `Telephone::~Telephone()'
TBook.cpp:(.text+0x2e6): undefined reference to `Telephone::~Telephone()'
TBook.cpp:(.text+0x2fa): undefined reference to `Telephone::~Telephone()'
/tmp/ccxqI6An.o:TBook.cpp:(.text+0x370): more undefined references to `Telephone::~Telephone()' follow
/tmp/ccxqI6An.o: In function `TBook::write()':
TBook.cpp:(.text+0x4e1): undefined reference to `Telephone::getNumber()'
TBook.cpp:(.text+0x506): undefined reference to `Telephone::getAreaCode()'
TBook.cpp:(.text+0x53a): undefined reference to `Telephone::getName()'
/tmp/ccxqI6An.o: In function `TBook::lookup(std::basic_string<char, std::char_traits<char>, std::allocator<char> >)':
TBook.cpp:(.text+0x6d4): undefined reference to `Telephone::getName()'
TBook.cpp:(.text+0x79e): undefined reference to `Telephone::Telephone(int, std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::basic_string<char, std::char_traits<char>, std::allocator<char> >)'
/tmp/ccxqI6An.o: In function `TBook::print()':
TBook.cpp:(.text+0x880): undefined reference to `Telephone::getName()'
TBook.cpp:(.text+0x8e0): undefined reference to `Telephone::getNumber()'
TBook.cpp:(.text+0x8ff): undefined reference to `Telephone::getAreaCode()'
collect2: ld returned 1 exit status
[Finished in 0.3s with exit code 1]
I'm kind of not liking that the file is not receiving any of the Telephone class's methods. Here's what the code looks like for TBook.h:
#ifndef TBOOK_H
#define TBOOK_H
#include "Telephone.h"
class TBook{
private:
Telephone rolodex[10];
int current;
int max;
public:
TBook();
~TBook();
void add(Telephone);
void write();
bool is_full();
void print();
void check();
Telephone lookup(string);
};
#endif
And this is what TBook.cpp looks like:
#include <iostream>
#include <fstream>
#include <string>
#include "TBook.h"
#include "Telephone.h"
using namespace std;
TBook::TBook(){
current = 0;
max = 9;
cout << "Hello" << endl;
string line;
ifstream myfile ("rolodex.txt");
if (myfile.is_open()){
while ( getline (myfile,line) ){
cout << line << endl;
Telephone t;
t.allNum(line);
add(t);
}
myfile.close();
}else if (!myfile.is_open()){
ofstream myfile;
myfile.open ("rolodex.txt");
myfile << "This is an empty file (Relatively).";
myfile.close();
}
}
TBook::~TBook(){
}
void TBook::add(Telephone tel){
if (!is_full()){
rolodex[current] = tel;
current++;
}
}
void TBook::write(){
ofstream myfile;
myfile.open ("rolodex.txt");
for (int i = 0; i < current; ++i)
{
myfile << rolodex[i].getName() << "," << rolodex[i].getAreaCode() << "," << rolodex[i].getNumber() << "
";
}
myfile.close();
}
bool TBook::is_full(){
if (current <= max){
return false;
}
return true;
}
Telephone TBook::lookup(string lookName){
for (int i = 0; i < current; ++i){
if (rolodex[i].getName() == lookName){
return rolodex[i];
}
}
return Telephone(100, "", "1000000");
}
void TBook::print(){
//Print the vairables
for (int i = 0; i < current; ++i){
cout << "Name: " << rolodex[i].getName() << endl;
cout << "Number: (" << rolodex[i].getAreaCode() << ") " << rolodex[i].getNumber() << endl;
}
}
void TBook::check(){
cout << "the message" << endl;
}
Since the problem seems to be arising with the Telephone class, I figure I should also show that code too
Telephone.h
..
#ifndef TELEPHONE_H
#define TELEPHONE_H
#include <iostream>
#include <string>
using std::string;
class Telephone{
private:
string name;
string num;
int areaCode;
public:
Telephone(int, string, string);
Telephone();
~Telephone();
bool setAreaCode(int);
//Setters
void setName(string);
void setNumber(string);
bool allNum(string);
//Getters
string getName();
string getNumber();
int getAreaCode();
//Checks
bool checkX(int);
bool checkY(int);
};
#endif
Telephone.cpp
..
#include <iostream>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "Telephone.h"
using namespace std;
Telephone::Telephone(){
areaCode = 0
name = "";
num = "";
}
Telephone::Telephone(int aCode, string nam, string number){
areaCode = aCode;
name = name;
}
Telephone::~Telephone(){
//Nope Nada
}
bool Telephone::allNum(string all){
size_t found = all.find_first_of(",");
//
string first = all.substr(0, found);
string second = all.substr((found)+1, found+1);
string third = all.substr( all.find_last_of(",")+1, all.length());
int x, y;
//convert string to int values
if(third.length() == 7){
x = atoi(third.substr(0,3).c_str()),
y = atoi(third.substr(3,4).c_str());
}else{
cerr << "Your phone number is not valid" << endl;
}
int ac = atoi(second.substr(0, second.length()).c_str());
setName(first);
if (!setAreaCode(ac)){
setAreaCode(100);
return true;
}
if (!checkX(x) || !checkY(y)){
setNumber("1000000");
}else{
setNumber(third);
}
cerr << "The info provided is not valid" << endl;
return false;
}
void Telephone::setNumber(string number){
num = number;
}
bool Telephone::setAreaCode(int aCode){
if(aCode >= 100 && aCode <= 999){
areaCode = aCode;
return true;
}
return false;
}
void Telephone::setName(string theName){
name = theName;
}
bool Telephone::checkX(int x){
if(x >= 100 && x <= 999){
return true;
}
cerr << "First three digits are not valid" << endl;
return false;
}
bool Telephone::checkY(int y){
if(y >= 0000 && y <= 9999){
return true;
}
cerr << "Last four digits are not valid" << endl;
return false;
}
//Getters
string Telephone::getName(){
return name;
}
string Telephone::getNumber(){
return num;
}
int Telephone::getAreaCode(){
return areaCode;
}
And my main file (also called test.cpp) looks like this:
test.cpp
#include <iostream>
#include <string>
#include "TBook.h"
#include "Telephone.h"
using namespace std;
int main(int argc, char const *argv[])
{
//Create a Rolodex
TBook addressBook;
return 0;
}
I'm also getting this error with test.cpp
/tmp/ccl8anRb.o: In function `main':
test.cpp:(.text+0x24): undefined reference to `TBook::TBook()'
test.cpp:(.text+0x38): undefined reference to `TBook::~TBook()'
collect2: ld returned 1 exit status
I think this is mostly a compiling error, but I'm still not sure, and I feel like I'm the setup of the meme "My code doesn't work, and I don't know why." Usually I would bash the code and try different methods until it works, but I simply don't have the time. I therefore need your help.
This is a linker error. Try:
g++ test.cpp Telephone.cpp -o test
Basically, the linker is complaining about functions you used but didn't provide an implementation for. To see all the steps the compiler performs for you, throw in a -v:
g++ -v test.cpp Telephone.cpp -o test
这篇关于G ++未定义对类::函数的引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!