Generic List Template, Function Parameter
-
Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:
void mCtrlWrap::IsVisible(bool TF, List^ list)
{
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried
template
void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list){
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan
-
Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:
void mCtrlWrap::IsVisible(bool TF, List^ list)
{
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried
template
void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list){
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan
In C# you would handle this by casting each component to a Control object. I assume you can do the same in C++/CLI. Also, why do you need the
else if
clause above?TF
can only betrue
orfalse
, if it is not true then you only need a simpleelse
clause for the second block, thus:if (TF == true) // or just if (TF)
{
// what to do if it is true
}
else
{
// what to do if it is false
} -
Hello All. I'm trying to create a function that can loop through a list of Windows::Forms::Controls (I know there's no loop below, I'm just keeping it simple for sake of testing) and perform a task. Right now I have a function which works properly:
void mCtrlWrap::IsVisible(bool TF, List^ list)
{
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
but i want to make the List parameter a template so that it take List^ as a parameter or List^, etc. I've tried
template
void mCtrlWrap::IsVisible(bool TF, System::Collections::Generic::List^ list){
if (TF == true)
{
list[0]->Visible = true;
list[0]->BackColor = Color::Azure;
}
else if (TF != true)
{
list[0]->Visible = false;
}}
which compiles just fine but i get linking errors: 1>MainScreen.obj : error LNK2020: unresolved token (06000001) mCtrlWrap::IsVisible can someone please help me figure out what I'm doing wrong? Thanks Brennan
You are mixing C++ template and .NET generics. This is OK is you understand what are you doing. In your specific case, this is classic C++ template problem. To call templated function C++ compiler must see the function implementation, and not only prototype. So, you can instantiate IsVisible function only in the same .cpp file. Or make it inline by moving the function implementation to .h file.