inline functions in header

Started by
13 comments, last by eGamer 18 years, 7 months ago
when i put some functions as inline in a header file a linker error occurs how to avoid this ? i may send the source if you want.
Advertisement
What linker error? And yes, show the relevant code.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
Remember that to call an inline function, the function definition has to be in that translation unit. So in general inline function definitions should go in the header files where they are declared. Note that this does not create a 'multiple definition' error due to the fact that it is inline.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Also, you have to remember to #include the header file in any source file that references those inlines.
here is the source code :


/********************************************************************/
// stdafx.h
/*********************************************************************/

// stdafx.h : include file for standard system include files,
// or project specific include files that are used frequently, but
// are changed infrequently
//

#if !defined(AFX_STDAFX_H__F44E308F_4C58_43B1_B624_1D243E42A429__INCLUDED_)
#define AFX_STDAFX_H__F44E308F_4C58_43B1_B624_1D243E42A429__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers

#include <stdio.h>
#include <windows.h>
#include <list>


// TODO: reference additional headers your program requires here

#include "memManager.h"

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_STDAFX_H__F44E308F_4C58_43B1_B624_1D243E42A429__INCLUDED_)





/********************************************************************/
// stdafx.cpp
/*********************************************************************/
// stdafx.cpp : source file that includes just the standard includes
// memmanagement.pch will be the pre-compiled header
// stdafx.obj will contain the pre-compiled type information

#include "stdafx.h"

// TODO: reference any additional headers you need in STDAFX.H
// and not in this file






/********************************************************************/
//
// memManager.h: interface for the memManager class.
//
//////////////////////////////////////////////////////////////////////

#if !defined(AFX_MEMMANAGER_H__C22E49C5_E639_4E80_88F5_302078A789E7__INCLUDED_)
#define AFX_MEMMANAGER_H__C22E49C5_E639_4E80_88F5_302078A789E7__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum);
void RemoveTrack(DWORD addr);
void DumpUnfreed();


#ifdef _DEBUG
void* __cdecl operator new(unsigned int size,
const char *file,
int line)
{
void *ptr = (void *)malloc(size);
AddTrack((DWORD)ptr, size, file, line);
return(ptr);
};

void __cdecl operator delete(void *p)
{
RemoveTrack((DWORD)p);
free(p);
};


#endif

typedef struct
{
DWORD address;
DWORD size;
char file[64];
DWORD line;
} ALLOC_INFO;

typedef std::list<ALLOC_INFO*> AllocList;

extern AllocList *allocList;

#ifdef _DEBUG
#define new new(__FILE__, __LINE__)
#else
#define new new
#endif

#endif // !defined(AFX_MEMMANAGER_H__C22E49C5_E639_4E80_88F5_302078A789E7__INCLUDED_)





//*/*****************************************************************/
//
// memManager.cpp: implementation of the memManager class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"

AllocList *allocList;


void AddTrack(DWORD addr, DWORD asize, const char *fname, DWORD lnum)
{
ALLOC_INFO *info;

if( !allocList ) allocList = (AllocList*) malloc( sizeof(AllocList) );

info = (ALLOC_INFO*) malloc( sizeof(ALLOC_INFO) );

info->address = addr;

strncpy(info->file, fname, 63);

info->line = lnum;

info->size = asize;

allocList->insert(allocList->begin(), info);
};

void RemoveTrack(DWORD addr)
{
AllocList::iterator i;

if(!allocList)
return;
for(i = allocList->begin(); i != allocList->end(); i++)
{
if((*i)->address == addr)
{
allocList->remove((*i));
break;
}
}
};

void DumpUnfreed()
{
AllocList::iterator i;
DWORD totalSize = 0;
char buf[1024];

if(!allocList)
return;

for(i = allocList->begin(); i != allocList->end(); i++)
{
sprintf(buf, "%-50s:\t\tLINE %d,\t\tADDRESS %d\t%d unfreed\n",
(*i)->file, (*i)->line, (*i)->address, (*i)->size);
OutputDebugString(buf);
totalSize += (*i)->size;
}
sprintf(buf, "-----------------------------------------------------------\n");
OutputDebugString(buf);
sprintf(buf, "Total Unfreed: %d bytes\n", totalSize);
OutputDebugString(buf);
};






/**********************************************/
// memmanagement.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

int main(int argc, char* argv[])
{
printf("Hello World!\n");
return 0;
}



check out the code again and notice inline new and delete operators inside
memmanager.h.

[Edited by - eGamer on August 27, 2005 2:14:34 AM]
I suppose inline makes only sense for member functions.
I can't see any classes here, neither any function bodies in any of the headers.
Exactly what functions do you want to use as inline?

shinjin
Quote:Original post by cr_shinjin
I suppose inline makes only sense for member functions.

I don't see where you're getting that from, considering inline has nothing to do with what kind of function it is, and everything to do with reducing (removing) the overhead of a function call.

Quote:
I can't see any classes here, neither any function bodies in any of the headers.
Exactly what functions do you want to use as inline?

shinjin

Quoted for truth. eGamer, are you sure those files are relevant? As far as I can see, there are no inline functions there at all. It would also help a great deal if you would post exactly what linker errors you are getting, or better yet, search on MSDN for solutions before asking in the first place.
Free speech for the living, dead men tell no tales,Your laughing finger will never point again...Omerta!Sing for me now!
Quote:Original post by eGamer
when i put some functions as inline in a header file...

Looking at your source, there are no inline functions. I recommend you take a brief refresher on what an inline function is.

That said, you failed to include memManager.h and stdafx.h in memManager.cpp. The first is almost mandatory for your project to build. The second is if you want the memManager object output added to your pre-compiled header.
Quote:Original post by Oluseyi
That said, you failed to include memManager.h and stdafx.h in memManager.cpp. The first is almost mandatory for your project to build. The second is if you want the memManager object output added to your pre-compiled header.


stdafx.h includes memManager.h and memManager.cpp includes stdafx.h, so that should be ok. But, as stated, there are no inline functions defined so I don't see what any of that code has to do with the original question.
check out the code againg (new , delete ) are the inline functions inside memmanager.h.

i want to overload the new and delete operators to discover memory leaks,
using the macra below them to replace the new with the new(line,file) to know
where the allocation happened in the source file.

This topic is closed to new replies.

Advertisement