【问题标题】:Compiling from multiple files gives "undefined reference"从多个文件编译会给出“未定义的引用”
【发布时间】:2018-03-28 14:48:07
【问题描述】:

我需要在单独的文件中提供一个 CFG 类,但我不确定如何将它与关联的 .h 和主程序一起编译。

我已经#includeed .h 文件,并在命令行中询问了这两个文件,但我不确定为什么将它们一起编译是错误的。

想法?

CFG.cpp:

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    public:
        string code[25];
        char startNT;
    //private:

    CFG(string inCode[], int stringLen)
    {
        for (int a = 0; a < stringLen; a++)
        {
            //cout << inCode[a] << endl;
            this->code[a] = inCode[a];
        }
        for (int a = 0; a < stringLen; a++)
        {
            cout << this->code[a] << endl;
        }
    }

    char getStartNT()
    {
        return startNT;
    }

    void setStartNT(char stNT)
    {
        startNT = stNT;
    }

    bool processData(string inString, string wkString)
    {
        //Our recursive function
        return true;
    }

    void garbage()
    {
        return;
    }
};

CFG.h:

#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    public:
        string code[25];
        char startNT;

        CFG(string inCode[], int stringLen);
        char getStartNT();
        void setStartNT(char stNT);
        bool ProcessData(string inString, string wkString);
        void garbage();
};

#endif

cfg_entry.cpp:

#include <stdio.h>
#include <iostream>
#include "cfg.h"

using namespace std;

int main()
{
    string inArray[5];
    inArray[0] = "test0";
    inArray[1] = "test1";
    inArray[2] = "test2";
    inArray[3] = "test3";
    inArray[4] = "test4";
    CFG * cfg1 = new CFG(inArray, 5);
    cfg1->garbage();
    return 0;
}

编译错误:

art@tv:~/Dropbox/Weber/CS 4110/Individual Assignment 2$ g++ -g -std=c++11 -Wall -o cfg_entry cfg.cpp cfg_entry.cpp
/tmp/ccICQEd0.o: In function `main':
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:15: undefined reference to `CFG::CFG(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >*, int)'
/home/art/Dropbox/Weber/CS 4110/Individual Assignment 2/cfg_entry.cpp:16: undefined reference to `CFG::garbage()'
collect2: error: ld returned 1 exit status

【问题讨论】:

  • 您在 .cpp 文件中重新实现了整个类。这不是您实现这些功能的方式.. 只需 returnType className::func(parameterType parameter...) {..body..} 为您班级中的每个功能。注意:构造函数和析构函数没有返回类型。
  • .cpp 文件中的类定义是不允许的类的重新定义。您无需再次定义该类。只需定义成员函数。
  • 在其他新闻中:在 C++ 中,为宏保留 ALL UPPERCASE 是个好主意。

标签: c++ c++11 compiler-errors linker-errors undefined-reference


【解决方案1】:

我发现了我的问题。在我的例子中,头文件正在定义类,而 .cpp 文件再次重新定义它,试图创建 CFG 类的 2 个实例。 .h 需要处理类声明和变量实例化,而 .cpp 只处理函数定义。

cfg.h:

#ifndef _cfg_h_
#define _cfg_h_

#include <iostream>
#include <stdio.h>
#include <string>

using namespace std;

class CFG
{
    private:
        string code[25];
        char startNT;

    public:
        CFG(string inCode[], int stringLen);
        char getStartNT();
        void setStartNT(char stNT);
        bool processData(string inString, string wkString);
        void garbage();
};

#endif

cfg.cpp:

#include <iostream>
#include <stdio.h>
#include <string>
#include "cfg.h"

using namespace std;

CFG::CFG(string inCode[], int stringLen)
{
    for (int a = 0; a < stringLen; a++)
    {
        //cout << inCode[a] << endl;
        this->code[a] = inCode[a];
    }
    for (int a = 0; a < stringLen; a++)
    {
        cout << this->code[a] << endl;
    }
}

char CFG::getStartNT()
{
    return startNT;
}

void CFG::setStartNT(char stNT)
{
    startNT = stNT;
}

bool CFG::processData(string inString, string wkString)
{
    //Our recursive function
    return true;
}

void CFG::garbage()
{
    return;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-29
    • 2017-02-08
    • 2013-09-02
    • 1970-01-01
    • 2020-04-09
    • 2013-10-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多