0

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:\Users\Murmanox\Documents\C++\C++ Projects\CodeBlocks\Class Files Test\main.cpp|11|undefined reference to `Log::initialiseLog(ChoppableLog&, int, int)'|

I can post more details if necessary.

2
  • Should be void Log::initialiseLog(... in your .cpp file. Commented May 3, 2014 at 1:33
  • You need to qualify all your member functions etc. in "Log.cpp" with an explicit "Log::xyz". Commented May 3, 2014 at 1:36

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are a beautiful like gazelle! The closest I got to this was Log::initialiseLog(params){} which only ended up making me more confused.
1

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)
{

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.