|
Here is the whole code: I should of put it out here to begin with on my previous forum. I tends to generate an error c2783 on the find_index() call in the insert funtion. Is it the way I am sending the struct in the main to the insert fucntion? I don't know. Very frustrated with this.
I put void main() on the bottom of this page.
Thanks for the help.
#ifndef TABLE1_H
#define TABLE1_1
#include <cstdlib>
#include <cassert>
const size_t CAPACITY = 811;
int NEVER_USED = -1;
int PREVIOUSLY_USED = -2;
struct tractor
{
int key;
};
template<class recordType>
class table
{
//MEMBER CONSTANTS
private:
//MEMBER VARIABLES
recordType data[CAPACITY];
size_t used;
//HELPER FUNCTIONS
template<class recordType>
size_t hash(const recordType key) const
{
return (key % CAPACITY);
}
size_t next_index(size_t index) const
{
if((index+1)==CAPACITY)
return ((index + 1) % CAPACITY);
}
template<class recordType>
void find_index(int key, bool &found, std::size_t &index) const
{
size_t count; //number of entries that have been examined
count = 0;
i=hash(key);
while((count<CAPACITY) && (!never_used(i)) && (data[i].key != key))
{
++count;
i = next_index(i);
}
found = (data[i].key == key);
}
bool never_used(size_t index) const;
bool is_vacant(size_t index) const;
public:
//CONSTRUCTOR
table()
{
size_t i;
used = 0;
for(i=0; i<CAPACITY; ++i)
data[i].key = NEVER_USED;
}
//MODIFICATION MEMBER FUNCTIONS
void insert(recordType &entry)
{
bool already_present;
size_t index;
assert(entry.key >= 0);
//SET INDEX SO THAT DATA[INDEX] IS THE SPOT TO PLACE THE NEW ENTRY
find_index(entry.key,already_present,index);
//IF THE KEY WASN'T ALREADY THERE, THEN FIND THE LOCATION FOR THE NEW ENTRY.
if(!already_present)
{
assert(size() < CAPACITY);
index = hash(entry);
while(!is_vacant(index))
index = next_index(index);
++used;
}
data[index] = entry;
}
/*template<class recordType>*/
void remove(int key)
{
bool found;
std::size_t index; //SPOT WHERE DATA[INDEX].KEY==KEY
assert(key >= 0);
find_index(key,found,index);
if(found)
{
//THE KEY WAS FOUND SO REMOVE THIS RECORD AND REDUCE USED BY 1
data[index].key = PREVIOUSLY_USED; //INDICATES A SPOT THATS NO LONGER IN USE
--used;
}
}
//CONSTANT MEMBER FUNCTIONS
/*template<class recordType>*/
bool is_present(int key) const
{
bool found;
std::size_t index;
assert(key>=0);
find_index(key,found,index);
return found;
}
/* template<recordType>*/
void find(int key, bool &found, recordType &result) const
{
std::size_t index;
assert(key>=0);
find_index(key,found,index);
if(found)
result = data[index];
}
size_t size() const
{
return used;
}
};
#endif
void main()
{
table<tractor> tr;
tractor t;
t.key=28;
tr.insert(t);
}
|
|
|
|
|
Just a guess, based on the error code -- when you call find_index , you need to specify the template argument, e.g.: find_index<tractor>( ... ) .
In many cases, the compiler will attempt to deduce the template argument based on the types of data you pass to it, but it can't in this case.
- Mike
|
|
|
|
|
Hi,
I am newbee to VC++. I am basically C,C#,VB,VB.Net Programmer.
can anybody please let me know how to resize and reposition all the controls in the dialog in a dialog based applicaiton. actually I added a toobar, statusbar,treeview,listview and few buttons to the dialog. I want to resize and reposition the controls whenever user resizes the dialog window or maximize the dialog window. can anybody please give an idea to proceed or a sample to proceed ?
thanking you
Praveen
|
|
|
|
|
|
I have a problem and need somebody to show me the light.
I am trying to pass a struct through a template class called table. I keep generating an error of c2783 and cant for the life of me figure out why or what is wrong. The code is directly from a text book. Here is the code that is generating that error.
template<class recordType>
class table
{
table()
{
size_t i;
used = 0;
for(i=0; i<CAPACITY; ++i)
data[i].key = NEVER_USED;
}
//MODIFICATION MEMBER FUNCTIONS
template<class recordType>
void insert(const recordType &entry)
{
bool already_present;
size_t index;
assert(entry.key >= 0);
//SET INDEX SO THAT DATA[INDEX] IS THE SPOT TO PLACE THE NEW ENTRY
find_index(&entry.key,already_present,index);
//IF THE KEY WASN'T ALREADY THERE, THEN FIND THE LOCATION FOR THE NEW ENTRY.
if(!already_present)
{
assert(size() < CAPACITY);
index = hash(entry.key);
while(!is_vacant(index))
index = next_index(index);
++used;
}
data[index] = entry;
}
};
struct tractor
{
int key;
};
void main()
{
table<tractor> ta;
tractor t;
t.key=44;
ta.insert(t);
}
|
|
|
|
|
Well, you didn't say where the error is happening, so I can only guess:
<s>template<class recordType></s>
void insert(const recordType &entry) Take out the redundant template there, you only need that if the method has different template params than the class that it's in.
--Mike--
Ericahist | CP SearchBar v2.0.2 | Homepage | RightClick-Encrypt | 1ClickPicGrabber
Actual sign at the laundromat I go to: "No tinting or dying."
|
|
|
|
|
sorry about not being more specific, but it seems to not like the way I am sending the parameter in the void main(). I will try your method and see what happends. thanks.
Also when I double-click on the error, it goes directly to the find_index() call in the insert function.
Such as: see the comment below.
void main()
{
table<tractor> ta;
tractor t;
t.key=44;
ta.insert(t);
}
|
|
|
|
|
In searching Google, I actually found code very similar to this except they pass entry.key by value instead of taking address:
<br />
find_index(entry.key,already_present,index);<br />
You probably just made a typo.
|
|
|
|
|
no, I got this choppy code out of my text book and you are right, it is entry.key but I have tried it that way and several different ways and it still fails to compile.
|
|
|
|
|
Please help me to list requirements and resources to create a windows application that have DHTML GUI like Windows Explorer ... with VC 6.0 ?
----------------------
Coding for lv,lv & di
|
|
|
|
|
Hi!
There is a DHTML editing control distributed with Windows Explorer. Check it out it might be useful. Or you can use the Internet Navigator control distributed with Windows Explorer.
Do you want to make your internet navigator or just use some functions in an application?
Well... I am a beginner ...
|
|
|
|
|
Hi,
My project has 4 dialogues.I inserted a tab ctrl and set its style as Popup style and border as dialogue frame .I set my dialogs as Child and Border None.
The proj worked fine earlier without tab.after insertinon of the tab ctrl it just hangs.There is no error in it.
If i set the dialogues style back to popup and border to dialogue frame then it works without my tab dialogue showing up.
Can anyone suggest me where i'm wrong?
Thanks
|
|
|
|
|
Hi,
I want a alert sound -the sound of piano keys pressed together(The sound we get during "Debug Assertion Failure")when my dialog box pops up.
How do I do it?
Thanks and Regards
Deepak Samuel
|
|
|
|
|
Use MessageBeep(MB_ICONEXCLAMATION);
John
|
|
|
|
|
Hi! You can also create your own wav file and play it. You can use this:
<br />
CString sFilePath = _T("C:\\WavFiles\\sound.wav");<br />
PlaySound(sFilePath, NULL, SND_FILENAME);<br />
or
<br />
PlaySound(MAKEINTRESOURCE(IDR_WAVSOUND), NULL, SND_RESOURCE);<br />
You have to include Header File: mmsystem.h and Import Library: winmm.lib .
I hope this helps!
Well... I am a beginner ...
|
|
|
|
|
I wrote a linked list to hande some object that I created. When I try to print the list, it prints garbage. I made a temporary project to test my class and it works fine, so the problem is how I am using it in my project. I think that it is a memory issue and that the objects are being written over after I store them in my list.
This is how I am using the list.
//funtion to create object that will be stored in list.
CApproach ApproachPage::ParseApproach(CString string)
{
......
CApproach approach(Chart_Name, Chart_Num, Chart_Rev_Date);
return approach;
}
//Insert class in List.cpp
int PrintList::Insert(CApproach* approach )
{
.....
}
//main.h
PrintList* list;
//contructor of main
list = new PrintList();
//main
list->Insert(&ParseApproach(codestring));
//prints garbage to the outputfile, but works in another project
list->print()
I learned to program in java so pointers and c++ memory are far from my specialty. Any help would be great. Thanks.
Kevin Shaffer
kshaff03@msn.com
|
|
|
|
|
The CApproach object returned from ParseApproach is a temporary object. You're passing the address of this temporary to Insert. When the temporary object is destroyed automatically the pointer to it points to garbage. You can either let Insert take a copy of a CApproach object instead of a pointer:
<br />
int PrintList::Insert(CApproach approach)<br />
or you can assign the result of ParseApproach to a local variable, but keep in mind that when that variable goes out of scope, the pointer to it will point to garbage:
<br />
CApproach result = ParseApproach(codestring);<br />
list->Insert(&result);<br />
list->print();<br />
You could also pass Insert a pointer to an object allocated on the heap, but you have to remember to delete it when you're done with it.
|
|
|
|
|
If this is not a homework assignment you should try and use one of the built-in lists link CList or better yet list. Clist is a MFC template class and list is STL. See the MDSN help for documentation. There really is no good reason to have to create your own linked list when there are very good lists available.
John
|
|
|
|
|
hi
I am trying to open PS/2 port , but I can't!!
Anyone can help me to open a PS/2 port (Mouse) and read its data .
my purpose is this :: i want unplug a ps/2 mouse and turn on the computer then plug the mouse to computer and read its data .
i did this action for COM1 port but I can't this for PS/2 .
please help me
thanks alot
|
|
|
|
|
So, you may be asking yourself, with a subject like that, why use this forum? Simple, I did not go elsewhere, as codeproject always has the answer for me. Also, I know nothing of C++. Anyway, my problem.
At uni, we have been told we must learn to program in C. Fair enough. But please, what environment should you use for this? I was told (though I am not sure on this) that naming your file with a .c extension, instead of .cpp should do this for you. Anyway, tried that in VS.net, and it cant find <stdio.h>
So, I tried the same code in Borland C++. It gave me some error about not giving a correct output in my function.
So, you may say I am stupid, and made a mistake. But, I copied (and check often), the code from the C Language book (The bible of C I am told).
Please solve this for me.
|
|
|
|
|
What is the exact code you are trying to compile?
It is strange that stdio.h could not be found. Sounds like you may have an incomplete or corrupted installation of VS.NET.
- Mike
|
|
|
|
|
It depends on what the Uni provides. Back in my day, the universities often steadfastly refused to acknowledge the existence of windows, so you were often doing c programming from a unix prompt with a shell account using the... help me out here.. cc compiler? Ie cc source.c target
or some such command line.
They're going to have to guide you on an environment, because eventually you'll be writing programs and swapping source files and if they don't work in all environs, you'll be wasting your time. My second guess is that they're going to at least start you with pure ANSI c and c++, so TECHNICALLY the environment may not matter as long as you understand your personally chosen environment. In Visual Studio, you can write console programs and stick to completely ANSI code and have no trouble whatsoever. In the Borland environment-- I have no idea.
Hope this helps. As far as your particular error, you might send a snippet of code which directly relates to your error.
Paul
|
|
|
|
|
If you want to test code from Kerningham and Ritchie,
you can use VC of course.
All you have to do is (VC6)
First:
click on File - New
you can now choose the type of project you want to create
select WIN32console application
name it test (the IDE will append .dsw)
click OK
Second:
Select File - New again
you can now choose then type of file vou want to create
select sourcecode (.cpp)
name it main (the IDE will append .cpp)
click OK
Now you can cut and paste your sourcecode to the main.cpp file and compile it.
(usually the first test would be compiling "Hello World")
sledge
|
|
|
|
|
Hi
I'm new to MFC so I'm sure the solution is easy...
I have created a SDI application using App Wizard without support of document view. I have two views in a splitter, CFormView and CEditView..
The program starts up and all but when I try to type anything in the edit view an unhandled excpetion is thrown : Access violation.
Heres the initialization of the views:
BOOL CMainFrame::OnCreateClient(LPCREATESTRUCT lpcs, CCreateContext* pContext) <br />
{<br />
m_Splitter.CreateStatic(this,1,2);<br />
if (!m_Splitter.CreateView(0, 0,<br />
RUNTIME_CLASS(CDBView), CSize(200,400), pContext))<br />
{<br />
TRACE0("Failed to create second pane\n");<br />
return FALSE;<br />
}<br />
if (!m_Splitter.CreateView(0, 1,<br />
RUNTIME_CLASS(CFileView), CSize(0, 0), pContext))<br />
{<br />
TRACE0("Failed to create second pane\n");<br />
return FALSE;<br />
}<br />
return CFrameWnd::OnCreateClient(lpcs, pContext);<br />
}
Where CDBView is a CFormView, CFileView is a CEditView and CMainFrame is a CFrameWnd.
Daniel
|
|
|
|
|
Just use your debugger to trace line by line which part of the codes is throwing exception. Usually is cause by your variables are not getting initialized or accessing deleted pointers.
Sonork 100.41263:Anthony_Yio
|
|
|
|