View Full Version: multiple definition of - error

C++ Learning Community > C++ Help > multiple definition of - error


Title: multiple definition of - error
Description: im stuck


akira300 - February 9, 2007 01:22 PM (GMT)
i just started learning to prog in c++, im using netbeans.
i tryed to use a class and i keep getting this error. what am i doing wrong ?
-----------------------------
CODE
class test
{
public:
int funtime();

private:
int time;
};

int test::funtime()
{
return time;
}



---------------------------
when i try to build
i keep getting the error
/home/leandro/netbeans/testjes/testklasse.cc:13: multiple definition of `test::funtime()'

thnx in advance

myork - February 9, 2007 02:19 PM (GMT)
QUOTE (akira300 @ Feb 9 2007, 08:22 AM)
i just started learning to prog in c++, im using netbeans.
i tryed to use a class and i keep getting this error. what am i doing wrong ?
-----------------------------
CODE
class test
{
public:
int funtime();

private:
int time;
};

int test::funtime()
{
return time;
}



---------------------------
when i try to build
i keep getting the error
/home/leandro/netbeans/testjes/testklasse.cc:13: multiple definition of `test::funtime()'

thnx in advance


If you have the function defintion in the header file (.h) then it will be defined in each source file (.cpp). Thus the linker will see multiple definitions.

There are two solutions:
1) Move the function definition:
CODE
int test::funtime()
{
return time;
}
into a source file.

2) Declare the defintion inline.
CODE
inline int test::funtime()
{
return time;
}

akira300 - February 10, 2007 12:26 AM (GMT)
mmm ill explain better
-------------------------
overvieuw
user posted image
main
user posted image
class rubbish that gives the error
user posted image
my output
user posted image




Hosted for free by InvisionFree