[英]G++ - Undefined Reference to member function that is defined
I am currently working on a virtual run time environment program that is at a very early stage, i am prevented from continuing my work due to a linker error when using my makefile, provided below. 我目前正在处理一个处于早期阶段的虚拟运行时环境程序,由于使用我的makefile时出现链接器错误,我无法继续工作,如下所示。 The error i am receiving is: 我收到的错误是:
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
I am unsure as to why i get this error as i thought i had defined these things in the source file corresponding to the header. 我不确定为什么我得到这个错误,因为我认为我已经在与头文件对应的源文件中定义了这些东西。 All files will be given below so that the program can be compiled. 下面将给出所有文件,以便编译程序。
test.cpp: 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: 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
}
controller.h: 或者Controller.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
processor.cpp: processor.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
}
}
processor.h: processor.h:
#ifndef PROCESSOR_H
#define PROCESSOR_H
#include <iostream>
#include <cstdlib>
class Processor{
public:
Processor();
~Processor();
int codeParams(char code);
protected:
private:
};
#endif
All if any help is appreciated massively as it will help me to continue with my passion of developing a fully fledged open-source virtual runtime enviroment like the java vm, thank you for your time. 所有这些都得到了大量的赞赏,因为这将有助于我继续热衷于开发像java vm这样的完全成熟的开源虚拟运行环境,感谢您的时间。
For the member function Processor::codeParams
you should define it as: 对于成员函数Processor::codeParams
您应该将其定义为:
int Processor::codeParams(char code)
// ~~~~~~~~~~~
{
...
}
Otherwise it's just a normal (non–member) function. 否则它只是一个普通(非成员)功能。
For the static member Controller::pc
you should define it outside of the class definition, in controller.cpp
. 对于静态成员Controller::pc
您应该在controller.cpp
中的类定义之外定义它。
// Controller.h
class Controller {
...
private:
static int pc;
};
// controller.cpp
int Controller::pc;
In Controller.cpp you need a int Controller::pc;
在Controller.cpp中,你需要一个int Controller::pc;
or int Controller::pc = 0;
或int Controller::pc = 0;
In the header file you declared a static int named pc that exists somewhere. 在头文件中,您声明了一个名为pc的静态int,它存在于某处。 It needs to actually exist in a translation unit somewhere (in this case Controller.cpp) so that when the linker tries to find it... it exists. 它需要实际存在于某个地方的转换单元中(在本例中为Controller.cpp),以便当链接器试图找到它时......它就存在了。
In Processor.cpp your signature should look like int Processor::codeParams(char code)
to let the compiler know that is Processor's codeParams and not a random function named codeParams that happens to also take a character. 在Processor.cpp中,您的签名应该看起来像int Processor::codeParams(char code)
,让编译器知道这是Processor的codeParams,而不是一个名为codeParams的随机函数,它恰好也会占用一个字符。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.