|
-
Sep 17th, 2000, 12:38 PM
#1
Thread Starter
Frenzied Member
Harry.
"From one thing, know ten thousand things."
-
Sep 17th, 2000, 03:22 PM
#2
Monday Morning Lunatic
The destructor has to be virtual:
Code:
virtual ~CValue() {
next->prev = prev;
prev->next = next;
numVals--;
if(!numVals)
first = last = prev = next = NULL;
delete value;
}
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 17th, 2000, 07:37 PM
#3
Thread Starter
Frenzied Member
Ah right. Why's that? I don't know anything about virtual stuff yet.
Harry.
"From one thing, know ten thousand things."
-
Sep 18th, 2000, 12:11 PM
#4
Monday Morning Lunatic
I'm not completely sure why the destructor needs to be virtual. It's probably something to make sure that it can't be overloaded (?).
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 18th, 2000, 01:37 PM
#5
Fanatic Member
I can answer most general C++ questions, including programming for MS Windows. Full understanding of the Standard Template Library, and very helpful for general application structure.
I know that that's a useless post, but I had to post that.
-
Sep 18th, 2000, 01:39 PM
#6
Monday Morning Lunatic
LOL
Cool...you went to ask me a question...hehehe.
It is a bit contrived, isn't it!
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 18th, 2000, 08:50 PM
#7
Thread Starter
Frenzied Member
In the book I'm learning from it's just covered class destructors and no mention is made of anything being virtual. Several examples are given without the use of virtual. Is it just something that you personally do whenever you write a class destructor or did something point to a need for a virtual function?
Harry.
"From one thing, know ten thousand things."
-
Sep 19th, 2000, 01:11 PM
#8
Monday Morning Lunatic
I don't know - I always use virtual because that's what the examples said, and what I've seen in most code anywhere. I have no idea why it is, I just use it.
I suppose it helps the compiler slightly.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 25th, 2000, 06:27 PM
#9
Thread Starter
Frenzied Member
I tried making the destructor virtual today (took me a while I know) and I still get link errors
Harry.
"From one thing, know ten thousand things."
-
Sep 26th, 2000, 12:42 PM
#10
Monday Morning Lunatic
Can you post the whole build output, please?
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 26th, 2000, 05:57 PM
#11
Thread Starter
Frenzied Member
Certainly, sorry I should have done that before but I thought it would probably be a glaring error. It's something to do with the static data members of the class:
Code:
--------------------Configuration: messAround2 - Win32 Debug--------------------
Linking...
main.obj : error LNK2001: unresolved external symbol "private: static class CValue * CValue::first" (?first@CValue@@0PAV1@A)
main.obj : error LNK2001: unresolved external symbol "private: static class CValue * CValue::last" (?last@CValue@@0PAV1@A)
main.obj : error LNK2001: unresolved external symbol "private: static int CValue::numVals" (?numVals@CValue@@0HA)
Debug/messAround2.exe : fatal error LNK1120: 3 unresolved externals
messAround2.exe - 4 error(s), 0 warning(s)
Know what that means? I mean, what do I do to fix it? I kind of assumed you'd try to compile it yourself to get the error messages.
Harry.
"From one thing, know ten thousand things."
-
Sep 27th, 2000, 12:31 PM
#12
Monday Morning Lunatic
Yeah...just tried it, same messages (at least I know it's on both, though).
Right, it compiles if you add this between the definition of your class and the definition of main:
Code:
CValue *CValue::first = 0;
CValue *CValue::last = 0;
int CValue::numVals = 0;
The problem is the linker doesn't know what to do with the static values, and they have to be initialised at file scope.
Also, I think you've got another problem - it linked okay, but prints "255" then crashes .
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 27th, 2000, 08:10 PM
#13
Thread Starter
Frenzied Member
Okay, thanks a lot, I'll do that 
What's file scope mean? I guess this is obvious, I'm just wondering though.
Harry.
"From one thing, know ten thousand things."
-
Sep 28th, 2000, 12:52 PM
#14
Monday Morning Lunatic
It means the code goes into the file outside of any { } blocks.
I refuse to tie my hands behind my back and hear somebody say "Bend Over, Boy, Because You Have It Coming To You".
-- Linus Torvalds
-
Sep 28th, 2000, 05:12 PM
#15
Lively Member
Well it's been a while since my last C++ project and I don't have C++ on my present station but...a thought or 2
Code:
long *value; // pointer to a long
...
int decVal()
{
cout << dec << *value << endl; // output of a pointer to a pointer to long
return 0;
}
...
You declared value as a pointer to a long - in your decVal
you print out a pointer to a pointer - is this what you really want to do? Why don't you just use a simple long?
parksie's note:
CValue *CValue::first = 0;
CValue *CValue::last = 0;
int CValue::numVals = 0;
works because you try to return a pointer to an instance of
your class before a value for 'first'etc. has been assigned.
In your constructor you initialize everthing based on
numVals which has not been initialized either.
That's why 'first' causes a crash - you only initialize in the if( numVals ) block.
Now I think the other problem is that you do a
current->getNext();
there is no next at this point so you try to assign a null to current.
Then there's
while(current && current != last);
current && current ???
&& is a logical AND that is always true. Did you want to do a bitwise AND? -> use just one &
Or did you mean to do
while(current && current->getNext() != last);
?
Anyway, have fun. C++ is a great language. Actually I miss her a little 
C/C++,Delphi, VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
I love deadlines. I like the whooshing sound they make as they fly by.
—Douglas Adams
-
Sep 28th, 2000, 10:54 PM
#16
Thread Starter
Frenzied Member
Okay... err... to answer some of your queries:
The reason why value is of type long* is because I am practising using DMA in class constructors/destructors. This is just a quick and simple practise for me because I'm learning about classes in C++. I think I explained some of this in my initial post.
On the subject of the crashes you mentioned (I haven't actually had any crashes as such, only link errors, and I still haven't tried the file scope static initialisation thing, been kinda busy) it says in my big book O' C that statics are initialised with the value 0, so the pointers start out as null. I check for this in my main code block before trying to print values for all the objects in my linked list. Whenever anything gets assigned a null pointer, that's intentional. Or I think it is.
Now this little gem:
while(current && current != last);
This is meant to be checking that there is a current object and the current object is not on the last object in the list. Perhaps I have my precedence wrong? In case there's confusion, this is what I would have written if I was being really careful:
while(current && (current != last));
So I don't think there are any errors there... feel free to point them out if you can see any. Bear in mind this in an educational exercise more than anything so I'm interested in anything you have to say.
Harry.
"From one thing, know ten thousand things."
-
Sep 29th, 2000, 11:03 AM
#17
Lively Member
just testing huh? Keep on codin'.
ALWAYS initialze pointers anyway.
As to the 'current && current!=last' line - it is better to
watch for precedence with the parenthesis as you say.
The != and == operators have precedence over the && op. so
actually the mistake is mine (last C++ project 1997!)
but still above comment is always safest.
C/C++,Delphi, VB6,Java,PB (blech!),ASP,JSP,SQL...bla bla bla and bla
I love deadlines. I like the whooshing sound they make as they fly by.
—Douglas Adams
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|