草庐IT

c++ - G++未定义对类::函数的引用

coder 2023-05-31 原文

我终于绝望了。因此,在我的 c++ 类(class)中,我们被指示使用类。我们会让头文件声明类和函数,而单独的 .cpp 文件实现它。事情应该是有效的,但他们没有,而且网络上没有任何解决方案似乎对我有用。为此,我在 linux 上使用 G++ 编译器,它似乎不适用于 IDE 或普通命令行。

我在 TBook.h 中遇到的错误是:

/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]

我有点不喜欢文件没有接收任何 Telephone 类的方法。 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

这就是 TBook.cpp 的样子:

#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() << "\n";
    }
    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 类有关,我想我也应该显示该代码

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;
}

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

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;
}

我在 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

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

最佳答案

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

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

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

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

关于c++ - G++未定义对类::函数的引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19661406/

有关c++ - G++未定义对类::函数的引用的更多相关文章

  1. ruby - Facter::Util::Uptime:Module 的未定义方法 get_uptime (NoMethodError) - 2

    我正在尝试设置一个puppet节点,但ruby​​gems似乎不正常。如果我通过它自己的二进制文件(/usr/lib/ruby/gems/1.8/gems/facter-1.5.8/bin/facter)在cli上运行facter,它工作正常,但如果我通过由ruby​​gems(/usr/bin/facter)安装的二进制文件,它抛出:/usr/lib/ruby/1.8/facter/uptime.rb:11:undefinedmethod`get_uptime'forFacter::Util::Uptime:Module(NoMethodError)from/usr/lib/ruby

  2. ruby-on-rails - Rails 3.2.1 中 ActionMailer 中的未定义方法 'default_content_type=' - 2

    我在我的项目中添加了一个系统来重置用户密码并通过电子邮件将密码发送给他,以防他忘记密码。昨天它运行良好(当我实现它时)。当我今天尝试启动服务器时,出现以下错误。=>BootingWEBrick=>Rails3.2.1applicationstartingindevelopmentonhttp://0.0.0.0:3000=>Callwith-dtodetach=>Ctrl-CtoshutdownserverExiting/Users/vinayshenoy/.rvm/gems/ruby-1.9.3-p0/gems/actionmailer-3.2.1/lib/action_mailer

  3. ruby-on-rails - 如何优雅地重启 thin + nginx? - 2

    我的瘦服务器配置了nginx,我的ROR应用程序正在它们上运行。在我发布代码更新时运行thinrestart会给我的应用程序带来一些停机时间。我试图弄清楚如何优雅地重启正在运行的Thin实例,但找不到好的解决方案。有没有人能做到这一点? 最佳答案 #Restartjustthethinserverdescribedbythatconfigsudothin-C/etc/thin/mysite.ymlrestartNginx将继续运行并代理请求。如果您将Nginx设置为使用多个上游服务器,例如server{listen80;server

  4. ruby-on-rails - form_for 中不在模型中的自定义字段 - 2

    我想向我的Controller传递一个参数,它是一个简单的复选框,但我不知道如何在模型的form_for中引入它,这是我的观点:{:id=>'go_finance'}do|f|%>Transferirde:para:Entrada:"input",:placeholder=>"Quantofoiganho?"%>Saída:"output",:placeholder=>"Quantofoigasto?"%>Nota:我想做一个额外的复选框,但我该怎么做,模型中没有一个对象,而是一个要检查的对象,以便在Controller中创建一个ifelse,如果没有检查,请帮助我,非常感谢,谢谢

  5. ruby - 主要 :Object when running build from sublime 的未定义方法 `require_relative' - 2

    我已经从我的命令行中获得了一切,所以我可以运行rubymyfile并且它可以正常工作。但是当我尝试从sublime中运行它时,我得到了undefinedmethod`require_relative'formain:Object有人知道我的sublime设置中缺少什么吗?我正在使用OSX并安装了rvm。 最佳答案 或者,您可以只使用“require”,它应该可以正常工作。我认为“require_relative”仅适用于ruby​​1.9+ 关于ruby-主要:Objectwhenrun

  6. ruby - 在没有 sass 引擎的情况下使用 sass 颜色函数 - 2

    我想在一个没有Sass引擎的类中使用Sass颜色函数。我已经在项目中使用了sassgem,所以我认为搭载会像以下一样简单:classRectangleincludeSass::Script::FunctionsdefcolorSass::Script::Color.new([0x82,0x39,0x06])enddefrender#hamlengineexecutedwithcontextofself#sothatwithintemlateicouldcall#%stop{offset:'0%',stop:{color:lighten(color)}}endend更新:参见上面的#re

  7. ruby-on-rails - 在 ruby​​ 中使用 gsub 函数替换单词 - 2

    我正在尝试用ruby​​中的gsub函数替换字符串中的某些单词,但有时效果很好,在某些情况下会出现此错误?这种格式有什么问题吗NoMethodError(undefinedmethod`gsub!'fornil:NilClass):模型.rbclassTest"replacethisID1",WAY=>"replacethisID2andID3",DELTA=>"replacethisID4"}end另一个模型.rbclassCheck 最佳答案 啊,我找到了!gsub!是一个非常奇怪的方法。首先,它替换了字符串,所以它实际上修改了

  8. ruby - 在 Ruby 中有条件地定义函数 - 2

    我有一些代码在几个不同的位置之一运行:作为具有调试输出的命令行工具,作为不接受任何输出的更大程序的一部分,以及在Rails环境中。有时我需要根据代码的位置对代码进行细微的更改,我意识到以下样式似乎可行:print"Testingnestedfunctionsdefined\n"CLI=trueifCLIdeftest_printprint"CommandLineVersion\n"endelsedeftest_printprint"ReleaseVersion\n"endendtest_print()这导致:TestingnestedfunctionsdefinedCommandLin

  9. ruby - 定义方法参数的条件 - 2

    我有一个只接受一个参数的方法:defmy_method(number)end如果使用number调用方法,我该如何引发错误??通常,我如何定义方法参数的条件?比如我想在调用的时候报错:my_method(1) 最佳答案 您可以添加guard在函数的开头,如果参数无效则引发异常。例如:defmy_method(number)failArgumentError,"Inputshouldbegreaterthanorequalto2"ifnumbereputse.messageend#=>Inputshouldbegreaterthano

  10. ruby - 如何在 Grape 中定义哈希数组? - 2

    我使用Ember作为我的前端和GrapeAPI来为我的API提供服务。前端发送类似:{"service"=>{"name"=>"Name","duration"=>"30","user"=>nil,"organization"=>"org","category"=>nil,"description"=>"description","disabled"=>true,"color"=>nil,"availabilities"=>[{"day"=>"Saturday","enabled"=>false,"timeSlots"=>[{"startAt"=>"09:00AM","endAt"=>

随机推荐