【问题标题】:G++ - Undefined Reference to member function that is definedG++ - 未定义对已定义成员函数的引用
【发布时间】:2017-05-27 21:39:25
【问题描述】:

我目前正在开发一个处于早期阶段的虚拟运行时环境程序,由于使用我的 makefile 时出现链接器错误,我无法继续我的工作,如下所示。我收到的错误是:

g++ controller.o processor.o test.o -o final
controller.o: In function `Controller::run()':
controller.cpp:(.text+0x1e0): undefined reference to
Processor::codeParams(char)'
controller.o: In function `Controller::fetch()':
controller.cpp:(.text+0x290): undefined reference to `Controller::pc'
controller.cpp:(.text+0x299): undefined reference to `Controller::pc'
collect2: error: ld returned 1 exit status
makefile:16: recipe for target 'final' failed
make: *** [final] Error 1

我不确定为什么会出现此错误,因为我认为我已在与标头对应的源文件中定义了这些内容。下面将给出所有文件,以便编译程序。

test.cpp:

#include <iostream>
#include <vector>
#include "includes/controller.h"

using namespace std;

int main()
{
    vector<char> prog = {0x0};

    Controller contr(prog);
    cout << "Error Code: " << contr.run() << endl;

    return 0;
}

controller.cpp:

/*
    Author(s):  James Dolan
    File:       controller.cpp
    Build:      0.0.0
    Header:     includes/controller.h
    DoLR:       21:39 11/1/2017

    Todo: n/a
*/



#include "includes/controller.h"


Controller::Controller(vector<char> prog)
{
    printf("Program:");                         //Display program
    for(auto i : program)
    {
        printf("%02X", i);
    }
    printf("\n");

    Controller::program = program;
}

Controller::~Controller ()
{
}

int Controller::run()
{
    bool runFlag = true;
    int errorCode = 0;
    char curCode;
    vector<char> curInstr;
    int paramRef;

    while(runFlag)
    {
        curCode = fetch();
        printf("curCode:%02X\n", curCode);
        curInstr.push_back(curCode);
        paramRef = proc.codeParams(curCode);

        if (paramRef == 0xffff){runFlag = false; continue;}     //Check if shutdown signal was returned, if so shutdown
        printf("opcode good\n");

        for(int i; i<paramRef; i++){curInstr.push_back(fetch());}
    }


    return errorCode;
}

char Controller::fetch()
{
    return program[pc++];                       //Return next instruction then increment the program counter
}

控制器.h:

/*
    Author(s):  James Dolan
    File:       controller.h
    Source:     ../controller.cpp
    DoLR:       21:39 11/1/2017

    Todo: n/a
*/


#ifndef CONTROLLER_H
#define CONTROLLER_H

#include <iostream>
#include <vector>
#include <cstdlib>

#include "processor.h"

using namespace std;

class Controller{

    public:
        Controller(vector<char> prog);
        ~Controller();
        int run();

    protected:

    private:
        vector<char> program;
        static int pc;
        char fetch();
        Processor proc();


};

#endif

处理器.cpp:

#include "includes/processor.h"

Processor::Processor()
{
}

Processor::~Processor()
{
}

int codeParams(char code)
{

    switch(code)
    {
        case 0x0:                   //Halt
            return 0;
        default:
            printf("[ERROR!] Invalid opcode [%02X]", code);
            return 0xffff;          //Return shutdown signal
    }
}

处理器.h:

#ifndef PROCESSOR_H
#define PROCESSOR_H

#include <iostream>
#include <cstdlib>

class Processor{

    public:
        Processor();
        ~Processor();
        int codeParams(char code);

    protected:

    private:

};

#endif

如果有任何帮助,我们将不胜感激,因为它将帮助我继续热情地开发像 java vm 这样的完全成熟的开源虚拟运行时环境,感谢您的宝贵时间。

【问题讨论】:

  • 你有几个问题。一个是错字(看你在cpp文件中是怎么定义codeParams的),另一个是this的骗子

标签: c++ makefile linker g++ linker-errors


【解决方案1】:

在 Controller.cpp 中,您需要 int Controller::pc;int Controller::pc = 0;

在头文件中,您声明了一个名为 pc 的静态 int,它存在于某处。它需要实际存在于某个翻译单元中(在本例中为 Controller.cpp),以便当链接器试图找到它时……它存在。

在 Processor.cpp 中,您的签名应该类似于 int Processor::codeParams(char code),以让编译器知道这是处理器的 codeParams,而不是一个名为 codeParams 的随机函数,它恰好也带有一个字符。

【讨论】:

    【解决方案2】:

    对于成员函数Processor::codeParams,您应该将其定义为:

    int Processor::codeParams(char code)
    //  ~~~~~~~~~~~
    {
      ...
    }
    

    否则它只是一个普通的(非成员)函数。

    对于静态成员Controller::pc,您应该在类定义之外,在controller.cpp 中定义它。

    // Controller.h
    class Controller {
        ...
        private:
            static int pc;
    };
    
    // controller.cpp
    int Controller::pc;
    

    【讨论】:

    猜你喜欢
    • 2011-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多