1

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!

TotalJargon
  • 147
  • 2
  • 2
  • 14
  • The code looks good. The error is probably in how you are trying to link it to form an executable. – David Schwartz Jul 31 '12 at 10:18
  • This question is far too verbose for what I believe to be a minor point (which moreover has hundreds of duplicates). Please condense it into a small, self-contained example. Don't tell us what works, just tell us what the problem is, and give the question a more relevant title. – Kerrek SB Jul 31 '12 at 10:19
  • Your variable `test` is never initialised in main.cpp. – Germann Arlington Jul 31 '12 at 10:34

3 Answers3

4

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.

juanchopanza
  • 223,364
  • 34
  • 402
  • 480
  • Thanks! I put it in Test.cpp, now however I get a new error: Test::counter++; \\"error: no post-increment operator for type" any ideas? I find mixed results online, some people say it's something to do with using templates instead of classes and otehrs say using another compiler like VC++ would help – TotalJargon Jul 31 '12 at 10:34
  • @TotalJargon the problem is that `Test::counter` is a function, which you cannot increment. You don't have any data to increment. It isn't clear what you are trying to do but the code certainly looks wrong. And that is beyond the scope of this question :) – juanchopanza Jul 31 '12 at 10:39
  • count is of type void in the class Test, counter is of type int, still no idea on why this wouldn't work? – TotalJargon Jul 31 '12 at 11:59
  • @TotalJargon because ` int counter();` declares a **function** that returns an int. If you want an int data member, then you need ` int counter;`. And you should initialize it to `0` in the constructor. – juanchopanza Jul 31 '12 at 12:04
  • like: Test::Test() { //ctor counter(0); } ? – TotalJargon Jul 31 '12 at 12:55
  • @TotalJargon it is better to use the constructor initializer list: `Test::Test() : counter(0) { }`. You can also omit the `0`. – juanchopanza Jul 31 '12 at 13:00
  • well, I'm convinced that there's something terribly wrong with how it's linked together because apparently there is no field named counter in Test despite being able to 'see' the variable inside void Test::count() and main.cpp – TotalJargon Jul 31 '12 at 13:16
  • This thread is very quickly becoming comment heavy, going to start a new thread dedicated to my current problem, thanks for the help :) – TotalJargon Jul 31 '12 at 13:23
2

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.

Asaf
  • 4,317
  • 28
  • 48
0

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

Roman Saveljev
  • 2,544
  • 1
  • 21
  • 20