1

I am a newbie. I tried this template function in visual studio, but I'm getting the following syntax error:

Missing type specifier - int assumed. Note: C++ does not support default-int

 template <typename Object,typename Comparator> 
        const Object & findMax(const vector<Object> &a, Comparator comp)
       {
            int maxIndex = 0;
            for(int i = 1; i < a.size(); i++){
                if(comp.isLessThan(a[maxIndex], a[i]))
                    maxIndex = i;
            }
            return a[maxIndex];
        }
        class LessThanByWidth {
        public:
            bool isLessThan(const Rectangle &a, const Rectangle &b) const{
                return (a.getWidth() < b.getWidth());
            }
        };

I don't know what exactly the problem is. This function is not declared inside any class.

6
  • 2
    How did you use the template ? Left alone it won't be compiled or generate any code (and looks correct). Commented Mar 6, 2012 at 7:13
  • 1
    It works for me in g++ (with a vector of ints, and a simple comparator). Can you show calling code? Commented Mar 6, 2012 at 7:14
  • 1
    This code can work, but it also can be a problem depending on what is surrounding it. Can you show a minimal but complete example that gives you a compiling error? Commented Mar 6, 2012 at 7:18
  • 2
    Is that really the only error you got? I used to tell my students to always start at the first message printed by the compiler. Any further errors might just be side effects of the first one, so it's futile to try to fix them without fixing the first problem. Commented Mar 6, 2012 at 7:29
  • Usually this means you've declared a function but forgotten to put a return type. C would assume integer returns, and C++ compilers have followed the convention but aren't happy about it. I think it will be in the code you haven't shown us. Commented Mar 6, 2012 at 7:44

4 Answers 4

1

Without more context from the compiler error I can't be sure, but this usually is the error that you get if you try declaring a function as having an argument of some type that isn't in scope or hasn't been declared. Did you #include <vector> at the top of your program? If you did, can you try rewriting the function as

 template <typename Object,typename Comparator> 
    const Object & findMax(const std::vector<Object> &a, Comparator comp){
        int maxIndex = 0;
        for(int i = 1; i < a.size(); i++){
            if(comp.isLessThan(a[maxIndex], a[i]))
                maxIndex = i;
        }
        return a[maxIndex];
    }

to explicitly use the fully-qualified name of the vector? This might resolve your issue.

Hope this helps!

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for your reply. I made a mistake in the template implementing class. Thats the reason for this error. Resolved it now. Thanks again.
0

You did not give the actual error, so I am guessing: the object class Comparator (whatever real class you are using) needs to have isLessThan method defined.

1 Comment

Thanks for your reply. I did implement that method. But I made a mistake in the class which is using this template. Resolved it now. Thanks again.
0

Are you sure you didnt forget to use the std namespace? Try adding at the top of your code (right after the including part)

using namespace std;

4 Comments

this is a recommended practice only in cpp file; in header files it can cause havoc in your (or others) projects. Prefer to use the prefix std::
he wasnt talking about header files at all so of course he should only use it in the cpp file. sorry for that.
Thanks for your reply. I included it in my header already. Actually the error was not because of this template and I found that, I made a mistake in the implementing class of template. Resolved it now.
Do NOT include it in your header file. "using namespace" should ONLY be used in cpp as LeSnip3R has already mentioned.
0

maybe you should add:

#include <vector>
using namespace std;

1 Comment

Please do not recommend the "using namespace std", especially to newbies as this practice is dangerous if used in header files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.