Classes: Accessor functions and link list

Hello, i have a small problem. i have to classes and one acts like the head node, while other acts as the list, consists of some data and another pointer to the next list.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class EmployeeNode {
    private:
        int employeeNumber ;
        char* firstName ;
        char* lastName ;
        double wage ;
        double hours ;
        EmployeeNode* next ;

    public:
        EmployeeNode() ;
       //  EmployeeNode( int emplNum, char* fName, char* lName, double hourlyWage, double hrs, EmployeeNode* n ) ;
       // ~EmployeeNode() ;
        int getEmplNumber() ;
         
        EmployeeNode * getNext() ;
         
} ;

int EmployeeNode::getEmplNumber()
{
    return (employeeNumber);
}

EmployeeNode * EmployeeNode::getNext()
{
     return (next);
}


the head node is::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include "EmployeeNode.h"

class EmployeeList {
    private:
        EmployeeNode* head;

    public:
        EmployeeList();
        EmployeeNode* locateEmployee( int emplNum );
        bool locateEmployee( int emplNum, char*& firstName, char*& lastName ); // if emplNum does not exists return FALSE; TRUE otherwise
       
  
 } ;

EmployeeList::EmployeeList()
{
}
bool EmployeeList::locateEmployee( int emplNum, char*& firstName, char*& lastName )
{
    EmployeeNode * lct=head;
    while (lct != NULL)
    {
        if(lct->getEmplNumber() == emplNum)
        {
            return (true);
        }
        lct =lct->getNext(); //This is valid to do right?, i am telling it
                           //to go to the object and evoke the getNext() on it. 
    }
}



i am not so sure why am i getting a error, lct = lct->getNext() should be fine.
thank you in advance
Last edited on
it would help if you told us what the error was.

The only thing I immediately see wrong is that locateEmployee never returns false.
sorry, the errors are. also forgot to add the headers to each file (done it now).
still same error.

1
2
3
4
5
6
7
Errors::

EmployeeList.o:EmployeeList.cpp|| undefined reference to `EmployeeNode::getEmplNumber()'|
EmployeeList.o:EmployeeList.cpp|| undefined reference to `EmployeeNode::getNext()'|

||=== Build finished: 2 errors, 0 warnings ===|

Those errors mean you forgot to give those two functions a body.

Write the bodies for those functions and they'll disappear.

EDIT:

wait a moment... you did give them a body.

waaaaat? I'll check this tomorrow after I get some sleep.
Last edited on
yea, mostly when i don't define it, that kinda error comes up, but i actually defined it.
Last edited on
Any one, any ideas on the error?
This is strange indeed. I don't see any reason why you'd get this error.

Try cleaning / rebuilding your project. Maybe that will help.
i tired again, but same error::

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23

#ifndef EMPLOYEENODE_H_INCLUDED
#define EMPLOYEENODE_H_INCLUDED

class EmployeeNode {
    private:
        int employeeNumber ;
        char* firstName ;
        char* lastName ;
        double wage ;
        double hours ;
        EmployeeNode* next ;

    public:
        
        int getEmplNumber() ;
        EmployeeNode * getNext() ;
} ;


#endif // EMPLOYEENODE_H_INCLUDED



1
2
3
4
5
6
7
8
9
10
11
12
#include "EmployeeNode.h"

int EmployeeNode::getEmplNumber()
{
    return (employeeNumber);
}

EmployeeNode * EmployeeNode::getNext()
{
     return (next);
}


now the head:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#ifndef EMPLOYEELIST_H_INCLUDED
#define EMPLOYEELIST_H_INCLUDED

class EmployeeList {
    private:
        EmployeeNode* head ;

    public: EmployeeList() ;
        ~EmployeeList() ;
        EmployeeNode* getHead() ;
        void setHead( EmployeeNode * n ) ;
        bool newEmployee( int emplNum, char* firstName, char* lastName, double hrs, double hourlyWage) ; // if emplNum already exists return FALSE; TRUE otherwise

#endif // EMPLOYEELIST_H_INCLUDED


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include <iostream>
#include "EmployeeNode.h"
#include "EmployeeList.h"

EmployeeList::EmployeeList()
{
    head =NULL;
}

EmployeeList::~EmployeeList()
{
    /// nothing to do for now.
}

EmployeeNode * EmployeeList::getHead()
{
    return (head);
}

void EmployeeList::setHead(EmployeeNode * n)
{
        head = n;
}

bool EmployeeList::newEmployee(int emplNum, char* firstName, char* lastName, double hrs, double hourlyWage)
{
    EmployeeNode * tpr = head;
    while (tpr!=NULL)
    {
        if(tpr->getEmplNumber() == emplNum) {return (true); }
        tpr=tpr->getNext(); 


    }
}


1
2
3
4
5
6
Error::
EmployeeList.o:EmployeeList.cpp|| undefined reference to `EmployeeNode::getEmplNumber()'|
\EmployeeList.o:EmployeeList.cpp|| undefined reference to `EmployeeNode::getNext()'|
\mingw\lib\libmingw32.a(main.o):main.c|| undefined reference to `WinMain@16'|
||=== Build finished: 3 errors, 0 warnings ===|
i Think i can ignore the winmain@16 error for now cause i haven't made a make file.


i tried on the different function, i am doing similar thing, trying to locate a node, this should work, i said go to whats tpr is pointing to then evoke the accessor getnext(), which will fetch the next node. same for getEmplNumber() which will fetch the number.
a small update lets say, on purpose i make an error like put NUl, rather NULL, the compiler will only warn me that NUL is undefined the other error, like the undefined reference doesn't come up.
make file


*cringe*

I just assumed you were using an IDE.

Okay -- your problem is you're not linking to whatever source file has those function bodies. You need to link to that cpp file. If you were using an IDE, you could just add the file to your project, but since you're compiling by hand you have to type some cryptic stuff into the command prompt which I can't help you with.


the compiler will only warn me that NUL is undefined the other error, like the undefined reference doesn't come up.


That's because the errors you're getting are linker errors. The linker isn't run until all compiling is complete, and if there are any compiler errors, then the linker doesn't run at all (so you don't get any linker errors).


As for the WinMain error -- WinMain is the entry point for Win32 programs (instead of the usual 'main'). So you have an incorrect setting somewhere. You'll need to fix this too.

... maybe you are using an IDE? How are you building this program?
well, i am using an IDE (codeblocks), but i have not made a project, but rather just put all the files in one folder.
but i have not made a project

Then how are you compiling?

And why not?

Make a project. Add all the files you want to be part of your program to the project. Then build.
mostly, i just make headers and cpp files and put them into a single folder, then compile the code and it compiles.

(i guess that's why i can't use the debugger, or any of the option related with projects, like save projects etc)

so to make a project do i just say create empty project?

by the way what IDE do you use?

Topic archived. No new replies allowed.