Thread: templates

  1. #1
    Registered User
    Join Date
    Dec 2007
    Posts
    15

    templates

    Hey all.
    I'm trying to study for a C++ test and I just got to the templates section.
    I'm trying to run the above code and get:

    "error C2670: 'Sqr<T>::doIt' : the function template cannot convert parameter 1 from type 'int [4]'"

    can I get some help please (don't forget that I'm new to templates...)
    Code:
    template <class T>
    class Sqr
    {
    public:
    	void operator () (T& var)
    	{
    		var *= var;
    	}
    	void doIt (T* begin, T* end, T& var)
    	{
    		for (T* i=begin, int z = 0; i != end; i++, z++)
    			begin[z]();
    	}
    };
    void main ()
    {
    	int arr [] = {2, 3, 4, 5};
    	for (int i=0; i<4; i++)
    		cout << arr[i] << ' ';
    	cout << endl;
    
    	Sqr::doIt(arr, arr+4, arr[0]);
    
    	for (int i=0; i<4; i++)
    		cout << arr[i] << ' ';
    	cout << endl;
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    Sqr::doIt(arr, arr+4, arr[0]);
    Your first argument here is an array, and the function expects a pointer. The argument matching is done before the conversion from array to pointer to first element.

    You need to do
    Code:
    Sqr::doIt(arr+0, arr+4, arr[0]);
    There may be other solutions too [may be that you actually need &arr[0], but I think arr+0 should be ok].

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Dec 2007
    Posts
    15
    Hey matsp. I tried both you're solutions but that doesn't seem to be the problem...

    any other ideas?

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not at this time. I'm sure someone else will come along and give you a better answer - it's well past my bedtime, really.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    First of all it's "int main()", not "void main()" void has never been a valid return type for main in C or C++.

    Next, you need to deal with the compiler errors in the order they appear in the build output. Prior to the error you mentioned you should get the following error:
    Code:
    error C2955: 'Sqr' : use of class template requires template argument list
    That is telling you that you need to use Sqr<int>::doIt. The compiler isn't goint to try and work out the parameter types for the member function when it doesn't even know what class instantiation you're talking about first.
    Then after fixing that you'll get the following error:
    Code:
    error C2352: 'Sqr<T>::doIt' : illegal call of non-static member function
    This means you either need an object of type Sqr<int> to call this on, or you need to declare the function as static. I assume the later is intended here.
    Now magically the other error has vanished. If you don't start with the first error you can quite often be looking at an error that is simply a consequence of an earlier error, and not actually an error in itself at all.

    This also then wont compile:
    Code:
    		for (T* i=begin, int z = 0; i != end; i++, z++)
    You can't declare two variables of different types in a for-loop. And this look like you're attempting to call an int as though it is a function!
    Code:
    			begin[z](3);
    I suggest perhaps getting the code you're writing to work with concrete types first, and then convert it to template code.
    Last edited by iMalc; 02-04-2008 at 12:11 AM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by iMalc View Post
    That is telling you that you need to use Sqr<int>::doIt.
    Well actually, as you know, the compiler can figure that out. The problem lies in that the class function is not static or the OP isn't calling the function on an actual instance of the object.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    Well actually, as you know, the compiler can figure that out.
    No, Elysia, the compiler can not infer template parameters for class templates. That only works for function templates.

    Thanks Elysia. You're a programming master! How the hell do you know every thing?
    Nice quote LOL!

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by scriptkiddie View Post
    No, Elysia, the compiler can not infer template parameters for class templates. That only works for function templates.
    No, the compiler can figure out template arguments for any functions, unless the arguments are ambiguous.
    A template class must have an explicit template type, however.

    Nice quote LOL!
    Indeed, it's what someone once replied. I quoted it.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    No, the compiler can figure out template arguments for any functions, unless the arguments are ambiguous.
    A template class must have an explicit template type, however.
    In this case you are both right.

    I think the code is a little too broken, so I agree with iMalc: get the code you're writing to work with concrete types first, and then convert it to template code.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  10. #10
    Registered User
    Join Date
    Feb 2008
    Posts
    2
    No, the compiler can figure out template arguments for any functions, unless the arguments are ambiguous.
    Like I said, Elysia, the compiler can only infer template parameters for function templates. The function "doIt" is not a function template. iMalc was entirely correct in pointing out that yosef_yaniv needs to specify the type.

    Good luck!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Templates from DLL or static library problem
    By mikahell in forum C++ Programming
    Replies: 2
    Last Post: 01-01-2008, 01:49 AM
  2. Questions about Templates
    By Shamino in forum C++ Programming
    Replies: 4
    Last Post: 12-18-2005, 12:22 AM
  3. templates and inheritance problem
    By kuhnmi in forum C++ Programming
    Replies: 4
    Last Post: 06-14-2004, 02:46 AM
  4. When and when not to use templates
    By *ClownPimp* in forum C++ Programming
    Replies: 7
    Last Post: 07-20-2003, 09:36 AM