![]() |
| | #1 |
| Novice Join Date: Jul 2009
Posts: 32
| Function template specialization. Prototypes and error below. Can someone explain to me what I'm doing wrong? ![]() Code: // template template <typename T> T findMax(const T arr[], int n); // specializations for char** template <> char* findMax(const char* arr[], int n); Code: error: template-id 'findMax<>' for 'char* findMax(const char**, int)' does not match any template declaration |
| msh is offline | |
| | #2 |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| It should be: Code: // specializations for char* template <> char* findMax(char* const arr[], int n); Code: // specializations for char** template <> char** findMax(char** const arr[], int n);
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way |
| laserlight is online now | |
| | #3 |
| Novice Join Date: Jul 2009
Posts: 32
| Ah. I see! It's different from regular functions, e.g: Code: void f(const char arr[]); |
| msh is offline | |
| | #4 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
Code: void f(const char* arr); Code: void f(char const* arr);
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #5 |
| Novice Join Date: Jul 2009
Posts: 32
| Okay. I'm lost now. That still does not work as I want it to, that is, I want the specialization to process something like this: Code: const char* s[5] =
{
"London",
"Berlin",
"Paris",
"Madrid",
"Lisbon"
};
|
| msh is offline | |
| | #6 |
| Registered User Join Date: Jun 2006
Posts: 72
| You are declaring a pointer array in which the elements are 5 characters(?) long. Code: #include<stdlib.h>
int main(){
const char* cities[10] = { "London", "Paris", "NYC" };
printf("%s", *(cities + 1)); // prints Paris
system("PAUSE");
return 0;
}
|
| Kudose is offline | |
| | #7 |
| Novice Join Date: Jul 2009
Posts: 32
| I was under the impression that I'm declaring an array of 5 pointers to string constants. Code: int main(void) {
const char* s[5] =
{
"London",
"Berlin",
"Paris",
"Madrid",
"Lisbon"
};
for (int i = 0; i < 5; i++) {
std::cout << s[i] << std::endl;
}
return 0;
}
|
| msh is offline | |
| | #8 | |
| and the Hat of Guessing Join Date: Nov 2007
Posts: 8,740
| Quote:
If this is supposed to match your function definition thing, well, it doesn't. The const here refers to the fact that your strings are constants; the const in your function declaration says that the array won't be changed (i.e., it's a char * const arr[]). | |
| tabstop is offline | |
| | #9 | ||
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Quote:
(Although your function wants a constant POINTER to char*.)
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| ||
| Elysia is offline | |
| | #10 |
| Novice Join Date: Jul 2009
Posts: 32
| Well. I managed to sort it out. Apparently, I was, as they say on the Internet, doing it wrong. Code: template <typename T> T findMax(T arr[], int n); template <> const char* findMax(const char* arr[], int n); |
| msh is offline | |
| | #11 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| As explained, it has nothing to do with specialization and everything to do with you using the improper types. const char* arr[] -> Arrays of pointers to const char. const char** arr -> Pointers to pointers to const char (can also be seen as an array of pointers to const char). char* const arr[] -> Constant array of pointers to char. char** const arr -> Constant pointers to pointers to char (can also be seen as a constant array of pointers to char).
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
| | #12 |
| Cat without Hat Join Date: Apr 2003
Posts: 8,439
| By the way, usually overloading functions is better than specializing them.
__________________ All the buzzt! CornedBee"There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code." - Flon's Law |
| CornedBee is offline | |
| | #13 |
| Registered User Join Date: Mar 2003 Location: Louisiana
Posts: 926
| |
| linuxdude is offline | |
| | #14 | |
| C++ Witch Join Date: Oct 2003 Location: Singapore
Posts: 10,365
| Quote:
__________________ C + C++ Compiler: MinGW port of GCC Build + Version Control System: SCons + Bazaar Look up a C/C++ Reference and learn How To Ask Questions The Smart Way | |
| laserlight is online now | |
| | #15 | |
| Mysterious C++ User Join Date: Oct 2007
Posts: 14,099
| Code: template<typename T> T func(); template<typename T> T func(T); template<typename T> T func(T, T); template<typename T, typename T2> T func(); template<typename T, typename T2> T func(T2); template<typename T, typename T2> T func(T2, T2);
__________________ Using: Microsoft Windows™ 7 Professional (x64), Microsoft Visual Studio™ 2008 Team System I dedicated my life to helping others. This is only a small sample of what they said: "Thanks Elysia. You're a programming master! How the hell do you know every thing?" Quoted... at least once. Quote:
| |
| Elysia is offline | |
![]() |
| Tags |
| function, specialization, template |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Game Pointer Trouble? | Drahcir | C Programming | 8 | 02-04-2006 02:53 AM |
| <Gulp> | kryptkat | Windows Programming | 7 | 01-14-2006 01:03 PM |
| Post... | maxorator | C++ Programming | 12 | 10-11-2005 08:39 AM |
| Dikumud | maxorator | C++ Programming | 1 | 10-01-2005 06:39 AM |
| C++ compilation issues | Rupan | C++ Programming | 1 | 08-22-2005 05:45 AM |