0

Ok so I'm still getting used to C++ again, so this error may seem simple. I'm attempting to create a simple class with a three member functions (I'm only trying to call one of them in this). So I create the class, instantiate an object, then attempt to call the function using that object and this error comes up:

Code.cpp:(.text+0x15): undefined reference to `Code::genCode()'

I've double checked to see if it was an error with the function itself, but that is not the case. I've seen others post about this issue but there seems to be a multitude of situations and solutions. Anyway here's the code:

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <list>
using namespace std;

class Code {
    public:
        int genCode();
        int checkCorrect();
        int checkIncorrect();
};

int main()
{
    Code c1;
    c1.genCode();
}

////////////////FUNCTIONS/////////////////////////
int genCode()
{
    vector <int> newcode;
    srand(time(NULL));
    for(int i = 0; i < 9; i++){
        int x;
        x = (rand() % 6);
        if (find(newcode.begin(),newcode.end(), x) == newcode.end())
        {
            newcode.push_back(x);
        }
    }
    if (newcode.size() > 4)
    {
        newcode.pop_back();
    }
    for(int i = 0; i < 4; i++)
    {
        return newcode[i];
    }   
}

int checkCorrect()
{

}

int checkIncorrect()
{

}
N. Sunilkumar
  • 65
  • 1
  • 7

4 Answers4

1

you need to put class name before method name

the format is 'returnType Classname::methodname { codes }'

int code::genCode()
{
    //codes
}

or you also possible to write code in class

moongom
  • 29
  • 2
1

Change the implementation of the methods of your class to the following:

int Code::genCode()
{
...
}

int Code::checkCorrect()
{
...
}

int Code::checkIncorrect()
{
...
}
csnate
  • 1,601
  • 4
  • 19
  • 31
0

You are defining the functions outside the class. Put them inside the class, then you don't need to declare them inside the class. Directly define them.

#include <vector>
#include <iostream>
#include <cstdlib>
#include <ctime>
#include <algorithm>
#include <list>
using namespace std;

class Code {

    public:
        ////////////////FUNCTIONS/////////////////////////
        int genCode()
        {
            cout << "Inside genCode. Just for Debugging purpose." << endl;
            vector <int> newcode;
            srand(time(NULL));
            for(int i = 0; i < 9; i++){
                int x;
                x = (rand() % 6);
                if (find(newcode.begin(),newcode.end(), x) == newcode.end())
                {
                    newcode.push_back(x);
                }
            }
            if (newcode.size() > 4)
            {
                newcode.pop_back();
            }
            for(int i = 0; i < 4; i++)
            {
                return newcode[i];
            }
        }

        int checkCorrect()
        {

        }

        int checkIncorrect()
        {

        }

};

int main()
{
    Code c1;
    c1.genCode();
}

Otput:

Inside genCode. Just for Debugging purpose.

CodeRunner
  • 687
  • 5
  • 13
  • This solves my problem, thank you! However I'm still kind of confused as to why. Do all member functions have to be defined within the class? I thought the benefit of using functions was that they could be implemented outside of the class, at the end of the program and wait to be called. Or do member functions act differently? – N. Sunilkumar Sep 18 '15 at 02:08
  • Yes. You have to define the member functions inside the class, If not then how you say that these are the member functions. You can define functions outside but then you will not be able to call them through the object of that class because they aren't the members of the class anymore. Then you have to call them directly from the main function. The global functions should be declared or defined before the main function. – CodeRunner Sep 18 '15 at 02:22
  • @CodeRunner _"Yes. You have to define the member functions inside the class, If not then how you say that these are the member functions."_ Stop telling nonsense please. If you define member functions outside of the class, you simply qualify them with the class name, as all of the other answers point out correctly. – πάντα ῥεῖ Sep 18 '15 at 07:44
  • We aren't prodding about that. He is calling the functions through the object of the class. First read, then do comment or down voting. – CodeRunner Sep 19 '15 at 05:06
0

The function

int genCode()

Is what's called a free function. It is not bound to a class.

In order for the compiler to know that genCode is part of a class, you have to tell it by explicitly stating the namespace to which genCode belongs.

int Code::genCode()

However since it appears code Code is entirely contained within one file, following CodeRunner's advice will lead to a cleaner implementation.

But why would anyone want to got the trouble of splitting everything up?

Separating the class definition from the method implementations allows you place the class definition into one file, the 'h header file, and the methods in an implementation file, usually a .cpp file. The header file is then shared with users of the Code object and the implementation file can be compiled into a library and hidden from the callers view.

There are a number of reasons to do this, but most of them have to do with creating pre-compiled libraries and using them to reduce build times.

With a library, you build the library once, and then compile the rest of the code that uses the library over and over until you get it right. Can you imagine how long it would take to build a program if you had to rebuild the C++ standard library every time you fixed a bug and wanted to test?

Had a job like that once. Had to spend four hours compiling third party network code every time I made a fix because the company's paranoid build system rebuilt everything every time. Off by one error? 4 hours. Need to add a debug line? 4 hours. You could make and test three changes a day. Sure, you can batch up a bunch of fixes, but if one failed spectacularly and broke the system, which one was it? Sooner or later you're reduced to a crawl, making tweaks, building, testing, profiling one at a time. Fortunately I was working on contract and paid by the hour.

Another good example is you can have one library that supports Windows and other libraries supporting QNX and other operating systems. All use the same header and the user can write a program that, in theory, will operate on all supported platforms simply by recompiling the user's code. It's never quite that clean, but one can dream.

The library can even be replaced with an updated library without requiring changes or compilation of the user's code and different variants of the library can exist for different needs. A debug version with extra logging, for example.

Perhaps the implementation is not intended for public eyes. The users get to see the header and call functions in the library, but no more.

user4581301
  • 33,082
  • 7
  • 33
  • 54