|
From: Jeff Baker on 20 Jun 2008 12:51 I have successfully used templates in the code below. I find I can't change it to return a value to main(). I get these error messages: c:\Documents and Settings\JBaker\My Documents\Visual Studio Projects\Effective C++ 3rd Editon\Macro Item 2\macro_main.cpp(7): error C2951: template declarations are only permitted at global or namespace scope c:\Documents and Settings\JBaker\My Documents\Visual Studio Projects\Effective C++ 3rd Editon\Macro Item 2\macro_main.cpp(8): error C2998: 'T1an' : cannot be a template definition c:\Documents and Settings\JBaker\My Documents\Visual Studio Projects\Effective C++ 3rd Editon\Macro Item 2\macro_main.cpp(9): warning C4552: '<<' : operator has no effect; expected operator with side-effect c:\Documents and Settings\JBaker\My Documents\Visual Studio Projects\Effective C++ 3rd Editon\Macro Item 2\macro_main.cpp(11): fatal error C1506: unrecoverable block scoping error Code: #ifndef MACRO_H #define MACRO_H #include <iostream> class A { public: A(){} template<typename T> A(const T& v, const T& z):a(v),b(z){} template<typename T> T/*void*/ callCompare(const T& a, const T& b) // if T is a number T c as T c = 'A' becomes the int of 'A' or 65 { T d = (a > b ? a : b); std::cout << d << std::endl; T c = 'A'; // if callCompare are numbers in main then A is 65 the ascii value std::cout << c << std::endl; return T d; //originallyl removed with T being void at callCompare() } private: }; #endif #include "Macro.h" int main() { A a; template<typename T> T an = a.callCompare( 5, 7); // originally there was not return value std::cout << "main " << an << std::endl; return 0; } regards, Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: MiB on 20 Jun 2008 20:57 On 21 Jun., 05:51, "Jeff Baker" <algort...(a)gmail.com> wrote: > I have successfully used templates in the code below. I find I can't change > it to return a value to main(). T is not a type, its a placeholder for a type. In your main() function, try: int an = a.callCompare<int>( 5, 7 ); best, Michael -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: wasti.redl on 20 Jun 2008 21:06 On Jun 21, 5:51 am, "Jeff Baker" <algort...(a)gmail.com> wrote: > I have successfully used templates in the code below. I find I can't change > it to return a value to main(). > int main() > { > A a; > template<typename T> > T an = a.callCompare( 5, 7); // originally there was not return value > std::cout << "main " << an << std::endl; > return 0; > > } It doesn't work that way. Currently, you have to deduce the return type of the template yourself and use it. Simple, in this case, since T is trivially deduced to int and T is also the return type, so the correct code is int an = a.callCompare(5, 7); With other templates, this may be far less trivial. In C++0x, there'll be automatic type deduction, which does what you think you did above. auto an = a.callCompare(5, 7); -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Jeff Baker on 22 Jun 2008 05:06 "Jeff Baker" <algorthjb(a)gmail.com> wrote in message news:_9ednfAAScTnq8HVnZ2dnUVZ_gudnZ2d(a)earthlink.com... >> Code: > > #ifndef MACRO_H > #define MACRO_H > #include <iostream> > class A > { > public: > A(){} > template<typename T> > A(const T& v, const T& z):a(v),b(z){} > template<typename T> > T/*void*/ callCompare(const T& a, const T& b) // if T is a number T c as > T > c = 'A' becomes the int of 'A' or 65 > { > T d = (a > b ? a : b); > std::cout << d << std::endl; > T c = 'A'; // if callCompare are numbers in main then A is 65 the ascii > value > std::cout << c << std::endl; > return T d; //originallyl removed with T being void at callCompare() > } > > private: > > }; > #endif > > #include "Macro.h" > int main() > { > A a; > template<typename T> > T an = a.callCompare( 5, 7); // originally there was not return value > std::cout << "main " << an << std::endl; > return 0; > } Thanks for you replys it answers alot. I found that with auto keyword only intergers are accepted. The single binary char is returned as an integer. The string argument needs a different syntax: // goes in main(){...} std::string ans = a.callCompare<std::string>("abc", "cde"); // no auto ans std::cout << "main " << ans << std::endl; char an = a.callCompare<char>('x', 'z'); std::cout << "main " << an << std::endl; With the character 'x', 'z' the return is auto and in accepted by the compiler, like the integer. How is the placer holder T really working? Obviously it will not return the 'x', 'z' like in the function will output, it will be the ascii integer with auto. What happens on the return to keep it as an integer? Why is the conversion neccessary for char and string - string isn't accepted by compiler producing an error - to show the char and string? The answer might be abvious, but ther can be more light to throw on this. regards, Jeff -- [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ]
From: Seungbeom Kim on 22 Jun 2008 10:18
Jeff Baker wrote: > "Jeff Baker" <algorthjb(a)gmail.com> wrote in message > news:_9ednfAAScTnq8HVnZ2dnUVZ_gudnZ2d(a)earthlink.com... >> T c = 'A'; // if callCompare are numbers in main then A is 65 the ascii >> value > > Obviously it will not return the > 'x', 'z' like in the function will output, it will be the ascii integer > with auto. Just one thing: you mention "ascii" in your posts, but there's nothing in the C++ standard that stipulates ASCII specifically (and yes, that word should be in uppercase), though the encoding (or its extensions, such as UTF-8) is common these days. You'd better avoid saying as if it were *the* standard, or universal, because it's wrong. Just as saying "int is 4 bytes" is not appropriate in a general context, though that statement may be true on a particular environment (and many, in fact). -- Seungbeom Kim [ See http://www.gotw.ca/resources/clcm.htm for info about ] [ comp.lang.c++.moderated. First time posters: Do this! ] |