I've been trying to use member functions from a class (prototypes define in counter.h) in my main file.
The class implementation is in file counter.cpp and i am trying to compile countertest.cpp. The error message is :
g++ countertest.cpp
Undefined symbols:
"counter::counter()", referenced from:
_main in ccmTnGJX.o
_main in ccmTnGJX.o
"counter::decrement()", referenced from:
_main in ccmTnGJX.o
ld: symbol(s) not found
collect2: ld returned 1 exit status

Here how the countertest.cpp looks like :

#include "counter.h"
#include <iostream>
#include <climits>
using namespace std;

int main()
{
    counter c1;
    counter c2;
    c1.decrement();
    return 0;
}

And counter.cpp is define as :

#include "counter.h"
#include <climits>
#include <iostream>

using namespace std;

counter::counter()
{
    count=0;
    maxValue= INT_MAX;
}

void counter::decrement()
{
    count --;
}

Finally, here is the class definition header file counter.h :

#ifndef COUNTER_H
#define COUNTER_H


class counter
{
    public:
        counter();
        void decrement();

    private:
    int count;
    int maxValue;
};


#endif // COUNTER_H

I imagine the problem is how you are actually compiling it. You need to compile both countertest.cpp as well as counter.cpp. My guess is that you are only compiling countertest.cpp.

Dave

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.