关于c ++:G ++ undefined对class :: function的引用

G++ undefined reference to class::function

本问题已经有最佳答案,请猛点这里访问。

我终于绝望了。因此,在我的C++类中,我们被指示使用类。我们会让头文件声明类和函数,而另一个.cpp文件实现它。事情应该是可行的,但事实并非如此,网络上没有解决方案对我有效。为此,我在Linux上使用了g++编译器,它似乎在IDE或普通命令行上都不起作用。

我的tbook.h中出现的错误是:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
/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]

我有点不喜欢文件没有接收到任何电话类的方法。下面是tbook.h的代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#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

这就是tbook.cpp的样子:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#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;
}

由于这个问题似乎是由电话课引起的,我想我也应该显示这个代码。

Telephone.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#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;
}

我的主文件(也称为test.cpp)如下所示:

test.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#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;
}

我在test.cpp中也遇到了这个错误

1
2
3
4
/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

我认为这主要是一个编译错误,但我仍然不确定,我觉得我是meme的设置者"我的代码不起作用,我不知道为什么。"通常我会猛击代码,尝试不同的方法直到它起作用,但我根本没有时间。所以我需要你的帮助。


这是一个链接器错误。尝试:

1
g++ test.cpp Telephone.cpp -o test

基本上,链接器抱怨您使用的函数,但没有提供实现。要查看编译器为您执行的所有步骤,请输入a-v:

1
g++ -v test.cpp Telephone.cpp -o test