473,320 Members | 2,111 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Error in distructor of template class

Hi,

what is wrong in this code ?

It is giving the error as below,

Functions containing while are not expanded inline. This while is in ~JList().
Functions containing for are not expanded inline. This is in T* Item(int Index)

The code is,

#ifndef JLIST_HPP
#define JLIST_HPP


template <class T> class JList
{
struct Cell
{
T * Data;
Cell *Next;
};

struct Cell *Head;

public:
JList()
{
Head = NULL;
}

~JList()
{
Cell * Current = Head;
Cell * Next = NULL;
while(Current)
{
Next = Current->Next;
delete Current->Data;
delete Current;
Current = Next;
}
}


T * Item(int Index)
{
Cell *Next, *Current = Head;
for(int i=0; ((i<Index) && (Current != 0)); i++)
{
Next = Current->Next;
Current = Next;
}
return Current->Data;
}

int Add(T * NewData)
{
int i=0;
Cell * Current = Head;
Cell * Previous = NULL;
while(Current)
{
Previous = Current;
Current = Current->Next;
i++;
}
if (Previous)
{
Previous->Next = new Cell;
Current = Previous->Next;
Current->Data = NewData;
Current->Next = NULL;
}
else
{
Head = new Cell;
Head->Data = NewData;
Head->Next = NULL;
}
return i;
}

int Delete(int Index)
{
Cell * Current = Head;
Cell * Previous=NULL, *Next=NULL;
for(int i=0; ((i<Index) && (Current != NULL)); i++)
{
Previous = Current;
Current = Current->Next;
if (Current) Next = Current->Next;
else Next = NULL;
}
if (Previous)
{
Previous->Next = Next;
delete Current->Data;
delete Current;
return 1;
}
else
{
//This is the case when you delete the first element.
//Or you delete an item from an empty list
if (Current)
{
Head = Current->Next;
delete Current->Data;
delete Current;
return 1;
}
}
return 0;
}

int Count()
{
Cell * Current = Head;
int i=0;
while(Current)
{
Current = Current->Next;
i++;
}
return i;
}
};

#endif



Please give me the solution.
Nov 15 '07 #1
1 1171
weaknessforcats
9,208 Expert Mod 8TB
Defining functions inside a class makes them inline in C++ but the the onl;y way to code in Java. Often Java programmers use the Java code in C++ and get these errors.

A inline function is a request by you to the compiler that, in your opinion, the function is just too small oir simple to make a call out of it. Every compiler has a complexity evaulator built in. When your inline function exeeds the complexity limit, the inline is ignored and a functon call is made.

This is bad news becuse when this happens, you get a function definition every time you include your header in a source file. Then you die in the link with multiple function definitions.

Your solution is simple: Do not make C++ member functions inline.

Only the function prototype should appear in the class and the function definition should be in a sepparate source file reserved for function definitions for that class.
Nov 15 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: AIM | last post by:
Error in msvc in building inheritance.obj to build hello.pyd Hello, I am trying to build the boost 1.31.0 sample extension hello.cpp. I can not compile the file inheritance.cpp because the two...
2
by: B_Love | last post by:
Hey! When trying to compile the code for a ordered vector class I get the following error: undefined reference to `WinMain@16' Anyone have any idea what I might be doing wrong? I've been...
2
by: Rudy Ray Moore | last post by:
Whenever I get any error with Vc++7.1/.net/2003, it is followed by huge ammounts of "template assistance" error messaging referencing template code (MTL) that has nothing to do with the error. ...
7
by: Alden Pierre | last post by:
Hello, I'm trying to create my own user define container, but I'm having a little hard time figuring out why is my class considered undefined by my compiler. Here is the following code. //...
9
by: Prasad | last post by:
HI, I am a beginner in VC++.. I am trying to write a Win32 console application in visual studio.. I am using following header files.. #include <STRING> using namespace std; #include...
9
by: ckkwan | last post by:
Hi C# gurus, In MFC, we have a CSingleLock class, which will ensure that a mutex is unlocked by itself when the CSingleLock object goes out of scope (when the distructor is been invoked). ...
0
by: rnot | last post by:
My code is working but I am not sure why. Is it because of the copy constructor? Is it becuase I still didn't define the distructor? (I did define an empty one) I am adding a psaudo code: ...
8
by: Jess | last post by:
Hi, I have a template function that triggered some compiler error. The abridged version of the class and function is: #include<memory> using namespace std; template <class T>
7
by: JustBeSimple | last post by:
Hi Everyone, I'm having a problem compiling useing the VS2005 .NET I need help to resolve those error, I try to create a new project it doesn't help any suggestion? I got the following errors:...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.