TotalJargon
TotalJargon

Reputation: 147

'Undefined reference to Class::method()'

Recently I've been learning how to create methods within classes so that I only have to write a method once and for each of that class I instantiate I can call the one method and it will work only on the variables of the object that called it, I know how to do this when only using main.cpp and no headers however I am confused on how I should be writing this when I use a class header and cpp.
I have a sample of code similar to what I want to achieve:

#include <iostream>

using namespace::std;

class Object
{
public:
    int stuff;
    void manageStuff();
    Object();
};
void Object::manageStuff()
{
    stuff++;
}

Object::Object() : stuff(0) {}

Object object1, object2;

int main() {
  for (int i = 0; i < 10; i++)
  {
      object1.manageStuff();
      object2.manageStuff();
      cout << object1.stuff << "\n";
      cout << object2.stuff << "\n";
  }
}  

This works fine and allows me to have two instances of Object and a method that works independently for each instance, this is my current project:
main.cpp:

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

using namespace std;

int main()
{

    Test test;

    for (int i = 0; i < 10; i++)
    {
        test.count(); // Here's my error "undefined reference to Test::count"
    }

    return 0;
}  

Test.cpp

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

using namespace std;

Test::Test()
{
    //ctor
}

Test::~Test()
{
    //dtor
}  

Test.h

#include <iostream>

#ifndef TEST_H
#define TEST_H


class Test
{
    public:
        Test();
        virtual ~Test();
        void count();
        int counter();
};

#endif // TEST_H  

and finally TestFunctions.h

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

#ifndef TESTFUNCTIONS_H_INCLUDED
#define TESTFUNCTIONS_H_INCLUDED

void Test::count()
{
    Test::counter++;
    std::cout << Test::counter;
}

#endif // TESTFUNCTIONS_H_INCLUDED  

I'm sure that there will be something that's very obviously wrong to a more seasoned programmer and I'm about to look a bit thick but any help would be greatly appreciated
Thanks!

Upvotes: 1

Views: 11106

Answers (3)

Roman Saveljev
Roman Saveljev

Reputation: 2594

Rename TestFunctions.h into TestFunctions.cpp, make it compiled same way as main.cpp and linked.

Alternatively, include TestFunctions.h somewhere, e.g. main.cpp

Upvotes: 0

Asaf
Asaf

Reputation: 4407

You defined (i.e. implemented) Test::count() in a header file (TestFunctions.h), but you never included it anywhere so the code there is not compiled.

You should change it to be in a .cpp file, compile it and link it with the other source files. There's no reason why not to place it in Test.cpp.

Upvotes: 2

juanchopanza
juanchopanza

Reputation: 227608

I would suggest getting rid of TestFunctions.h, and adding the implementation of Test::count() to Test.cpp. Currently, the TestFunctions.h header is not included anywhere, so you have no access to the definition from main.

Upvotes: 4

Related Questions