GIDForums  

Go Back   GIDForums > Computer Programming Forums > MS Visual C++ / MFC Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Sep-2004, 10:35
monospaz monospaz is offline
New Member
 
Join Date: Sep 2004
Posts: 3
monospaz is on a distinguished road

problem understanding how to use templates


hi,

i'm trying to use the CMap template, instantiating it like this:

CMap<char,char*,CString,CString*> map;

should these classes parameterising it be ok? i get an error from somewhere in the cmap code:

error C2664: 'SetAt' : cannot convert parameter 1 from 'char' to 'char *'

thanks.
  #2  
Old 21-Sep-2004, 00:50
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by monospaz
hi,

i'm trying to use the CMap template, instantiating it like this:

CMap<char,char*,CString,CString*> map;

I think I would have done it more like this

CPP / C++ / C Code:
	CMap <char, char &, CString, CString &> m_map;

or, better yet:

CPP / C++ / C Code:
	CMap <char, char, CString, LPCTSTR> m_map;

You can do some reading here Template-based classes and here CMap Class.

If you would have posted your call to SetAt, I could have told you where exactly the problem is... Oh, well, I think it's more challenging to find it on your own (and you'll problably learn a lot more)

Best regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #3  
Old 21-Sep-2004, 02:46
monospaz monospaz is offline
New Member
 
Join Date: Sep 2004
Posts: 3
monospaz is on a distinguished road
ah but i havent called setAt yet..

thanks for the help though. this is going to make me sound very stupid now, but how is

char &

different to

char *

?

is the second one is a pointer and the first one is a reference? whats the difference?
  #4  
Old 21-Sep-2004, 05:17
LuciWiz's Avatar
LuciWiz LuciWiz is offline
Moderator
 
Join Date: Jul 2004
Location: Cluj-Napoca (Romania)
Posts: 890
LuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the roughLuciWiz is a jewel in the rough
Quote:
Originally Posted by monospaz
ah but i havent called setAt yet..

thanks for the help though. this is going to make me sound very stupid now, but how is

char &

different to

char *

?

is the second one is a pointer and the first one is a reference? whats the difference?


Well, think of using functions. There is a distinctive difference on how you would use variables passed as pointers compared to those passed using references:

CPP / C++ / C Code:
#include "iostream.h"
#include "conio.h"

void TestFunctionPointer(char * c)
{
	cout << "Pointer value " << *c <<endl;
}

void TestFunctionReference(char & c)
{
	cout << "Reference value " << c <<endl;
}

int main(int argc, char* argv[])
{
	char c = 'a';
	TestFunctionPointer(&c);
	TestFunctionReference(c);

	getch();
	return 0;
}

They are both passed by address, but inside the functions they are treated different. (Observe the *)
So, inside this template, methods like SetAt will be called, but they won't know how to deal with your data, sice they expect a reference type.

But did my previous post fix your problem?

Regards,
Luci
__________________
Please read these Guidelines before posting on the forum

"A person who never made a mistake never tried anything new."
Einstein
  #5  
Old 21-Sep-2004, 07:41
monospaz monospaz is offline
New Member
 
Join Date: Sep 2004
Posts: 3
monospaz is on a distinguished road
yeah that explains it, the problem is fixed now.

thanks again.
  #6  
Old 08-Jun-2008, 12:47
Pelon Pelon is offline
New Member
 
Join Date: Jun 2008
Posts: 1
Pelon is on a distinguished road

Re: problem understanding how to use templates


Hi,

i'm trying to use CMap but i get some strange problems whit it. I have defined my variable as follows:
CMap <CString,LPCTSTR,CUser*,CUser*> m_umUsers;
I have my class CUser well done and I have a function called AddNewUser defined as follows:
void CVerificadorDoc::AddNewUser(CUser* pUser)
{
MessageBox(NULL, pUser->GetNick(), _T("New Register"),MB_ICONINFORMATION);
m_umUsers.SetAt((LPCTSTR)pUser->GetNick(), pUser);
SetModifiedFlag();

}
I call this function when I push a button in my dialog, the execution shows the messageBox correctly before the SetAt function but is there where nothing else happens. It never cames back to the calling function. I can not understand why, there isn't any error showed.

Is there anyone capable to explain me what to do or what is happening?

thank you very much in advance
 
 

Recent GIDBlogMore photos on Flickr by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
CD burner will not burn, new problem sdshaman Computer Hardware Forum 4 06-Feb-2008 23:17
Tee chart problem Arun C++ Forum 0 01-Sep-2004 23:23
Another FX 5600 problem (but with details that might shed light on this) BobDaDuck Computer Hardware Forum 2 16-Apr-2004 07:53

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:04.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.