C++ error Undefined reference to Class::Function()

I am farily new to C++ and I have been stuck with this problem for a few hours now. I am trying to setup the foundations for a video game related experience calculator, but I can't get past this problem.

main.cpp

#include <iostream>
#include "Log.h"

using namespace std;

int main()
{
    Log Logs;
    enter code here
    struct ChoppableLog Yew;

    Logs.initialiseLog(Yew, 60, 175);
    return 0;
}

Log.h

#ifndef LOG_H
#define LOG_H

struct ChoppableLog
{
    int level;
    int xp;
};

class Log
{
    public:
        void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int);
        Log();
};

#endif // LOG_H

Log.cpp

#include "Log.h"
#include <iostream>

using namespace std;

Log::Log()
{

}

void initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{

}

The error I get is

C:UsersMurmanoxDocumentsC++C++ ProjectsCodeBlocksClass Files Testmain.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|

I can post more details if necessary.


You have to define Log::initialiseLog with its full name, like so:

void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{ }

What you are doing is defining a new, free function of the name initialiseLog instead of defining the member function of Log .

This leaves the member function undefined, and, when calling it, your compiler (well, technically linker) will be unable to find it.


The definitions of functions in a header file should specify the scope. In your case, you should define initialiseLog() function in your cpp file as follows:

void Log::initialiseLog(struct ChoppableLog &par1_log, int par2_int, int par3_int)
{

}
链接地址: http://www.djcxy.com/p/95536.html

上一篇: 循环包含问题与扩展类c ++

下一篇: C ++错误对Class :: Function()的未定义引用