error: no match for ‘operator>’

Hello, I am working on some code but when I compile it I got this error and I don't know what to do next


#include <iostream>
#include <fstream>
#include <sstream>
#include <string>

using namespace std;


struct Park {
string parkname;
string state;
int area;
};

void printList(const Park parks[],int length){

Park park;
for (int i = 0; i < length; ++i)
{
park=parks[i];
cout<<park.parkname<<" ["<<park.state<<"] area: "<<park.area<<endl;
}

}

void addPark(Park parks[],string parkname,string state,int area,int length){

Park park;
park.parkname= parkname;
park.state= state;
park.area= area;
parks[length]=park;

}


int main(int argc, char const *argv[])
{

if (argv[1]==NULL ||argv[2]==NULL ||argv[3]==NULL )
{
cout<<"invalid commandline arguments"<<endl;
exit(1);
}

fstream inFile;
ofstream outFile;
outFile.open (argv[2]);
Park parks[100]; string line,parkname,token;
string state;
int area,length;
inFile.open(argv[1]);

stringstream ss;
char comma=',';
if (!inFile)
{
cout << "Failed to open the file."<<endl;
return 0;
}

while (getline(inFile, line))
{
ss.clear();
ss.str(line);
getline(ss, token, ',');
parkname=token;
getline(ss, token, ',');
state=stoi(token);
getline(ss, token, ',');
area=stoi(token);
if(state>stoi(argv[3]))
outFile<<parkname<<","<<state<<","<<area<<"\n";
addPark(parks,parkname,state,area,length++);
}

printList(parks,length);
inFile.close();
outFile.close();
return 0;
}


Here is what the error displaying :

main.cpp: In function ‘int main(int, const char**)’:
main.cpp:80:11: error: no match for ‘operator>’ (operand types are ‘std::string {aka std::basic_string}’ and ‘int’)
if(state>stoi(argv[3]))
~~~~~^~~~~~~~~~~~~~
In file included from /usr/include/c++/6/bits/stl_algobase.h:64:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:9:
/usr/include/c++/6/bits/stl_pair.h:459:5: note: candidate: template constexpr bool std::operator>(const std::pair<_T1, _T2>&, const std::pair<_T1, _T2>&)
operator>(const pair<_T1, _T2>& __x, const pair<_T1, _T2>& __y)
^~~~~~~~
/usr/include/c++/6/bits/stl_pair.h:459:5: note: template argument deduction/substitution failed:
main.cpp:80:24: note: ‘std::string {aka std::basic_string}’ is not derived from ‘const std::pair<_T1, _T2>’
if(state>stoi(argv[3]))
^
In file included from /usr/include/c++/6/bits/stl_algobase.h:67:0,
from /usr/include/c++/6/bits/char_traits.h:39,
from /usr/include/c++/6/ios:40,
from /usr/include/c++/6/ostream:38,
from /usr/include/c++/6/iostream:39,
from main.cpp:9:
/usr/include/c++/6/bits/stl_iterator.h:313:5: note: candidate: template bool std::operator>(const std::reverse_iterator<_Iterator>&, const std::reverse_iterator<_Iterator>&)
operator>(const reverse_iterator<_Iterator>& __x,
^~~~~~~~
/usr/include/c++/6/bits/stl_iterator.h:313:5: note: template argument deduction/substitution failed:
main.cpp:80:24: note: ‘std::string {aka std::basic_string}’ is not derived from ‘const std::reverse_iterator<_Iterator>’
if(state>stoi(argv[3]))
^
orL, _Container>’
The error message says you can't compare a string with an int. Which seems perfectly right and proper, don't you think? Not very clear what you intended to do here.

There's a few other problems (like not initialising length; inventing a variable "comma" that you then failed to use) but they are going to take a lot of sorting out unless you put your code in code tags.
Last edited on
OP's code, formatted and put into code tags:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <fstream>
#include <iostream>
#include <sstream>
#include <string>

using namespace std;

struct Park {
  string parkname;
  string state;
  int area;
};

void printList(const Park parks[], int length) {
  Park park;
  for (int i = 0; i < length; ++i) {
    park = parks[i];
    cout << park.parkname << " [" << park.state << "] area: " << park.area
         << endl;
  }
}

void addPark(Park parks[], string parkname, string state, int area,
             int length) {
  Park park;
  park.parkname = parkname;
  park.state = state;
  park.area = area;
  parks[length] = park;
}

int main(int argc, char const *argv[]) {
  if (argv[1] == NULL || argv[2] == NULL || argv[3] == NULL) {
    cout << "invalid commandline arguments" << endl;
    exit(1);
  }

  fstream inFile;
  ofstream outFile;
  outFile.open(argv[2]);
  Park parks[100];
  string line, parkname, token;
  string state;
  int area, length;
  inFile.open(argv[1]);

  stringstream ss;
  char comma = ',';
  if (!inFile) {
    cout << "Failed to open the file." << endl;
    return 0;
  }

  while (getline(inFile, line)) {
    ss.clear();
    ss.str(line);
    getline(ss, token, ',');
    parkname = token;
    getline(ss, token, ',');
    state = stoi(token);
    getline(ss, token, ',');
    area = stoi(token);
    if (state > stoi(argv[3]))
      outFile << parkname << "," << state << "," << area << "\n";
    addPark(parks, parkname, state, area, length++);
  }

  printList(parks, length);
  inFile.close();
  outFile.close();
  return 0;
}


state is an std::string. I'm not sure why, since you assign it the output of stoi.

Also, argc exists for a reason. Please use it.

-Albatross
Topic archived. No new replies allowed.