Josh
Josh

Reputation: 107

Getting error: missing template arguments before ‘(’ token

I'm not sure what's wrong. I'm pretty new to c++, but I don't see any problems. I've read through a bunch of other stack overflow pages, but none of them seem to address my issue.

This is from terminal

Joshs-MacBook-Pro:desktop Josh$ g++ BinaryCompare.cpp
BinaryCompare.cpp: In function ‘int main()’:
BinaryCompare.cpp:9: error: missing template arguments before ‘(’ token

Here's the code.

#include <iostream>
#include <string>

using namespace std;

bool isGreater(string a, string b);

int main (){
    if(greater("11", "00"))
        cout << "hello"<<endl;
  return 0;
}

bool isGreater(string a, string b){
    if(a.length() > b.length() ) return false;
    if(a.length() < b.length() ) return true;

    for(int i= 0; i < a.length(); i++){
        if(a[i] != b[i]){
            if(a[i] == '1') return false;
            return true;
        }
    }
    return false;   
}

Upvotes: 1

Views: 7164

Answers (3)

Joachim Isaksson
Joachim Isaksson

Reputation: 180887

This is a good example why using namespace std is not always a good idea.

You've typo'd

if(greater("11", "00"))

which should really be

if(isGreater("11", "00"))

...and manage to hit the name of a class template defined in the std namespace you've just imported whole-sale. Thereby the confusing error message.

Upvotes: 5

Drakosha
Drakosha

Reputation: 12155

greater() should be replaced with isGreater() on line 9 within if.

Upvotes: 2

Mats Petersson
Mats Petersson

Reputation: 129324

Aside from fixing the greater call to make a call to isGreater on line 9, you probably want to make sure that you pad the string with zero's until both strings are the same length, instead of just returning true or false in the first couple of lines in isGreater.

Upvotes: 1

Related Questions