简体   繁体   中英

“unkown parser error” in c++ netbeans

Sorry in advance, I posted a picture but I don't have the rep yet :( The error at line 1 says "Unknown parser error" repeated over four lines, and starting at the ReadFile class line pretty much each line has an error saying "unexpected token". I have included all of the custom header files from a folder either by right clicking "header" under project and adding the folder containing them or by right clicking the project name itself and adding the folder there. The private folder containing the private snippets of the headers are also included. Anyone have any ideas? This is for a free collection of lectures by Stanford, and you have to download and install their custom headers.. The instructor is using Visual c++, but that shouldn't mean I can't use the files for Netbeans right, their just .h files..?

#include "genlib.h"
#include <iostream>
#include <fstream>
#include "map.h"
#include "random.h"
#include "set.h"
using namespace std;

void ReadFile(ifstream &in, Map<int> &m)
{
    while (true) {
        string word;
        in >> word;
        if (in.fail()) break;
        if (m.containsKey(word)) 
            m[word]++;
        else
            m[word] = 1;
    }
    Map<int>::Iterator itr = m.iterator();
    string max;
    int maxCount = 0;
    while (itr.hasNext()) {
        string key = itr.next();
        if (m[key] > maxCount) {
            max = key;
            maxCount = m[key];
        }
    }
    cout << "Max is " << max << " = " << maxCount << endl;
}

void TestRandom()
{
    Set<int> seen;
    while (true) {
        int num = RandomInteger(1, 100);
        if (seen.contains(num)) break;
        seen.add(num);
    }
    Set<int>::Iterator itr = seen.iterator();
    while (itr.hasNext()) 
        cout << itr.next() << endl;
}

int main()
{
    ifstream in("burgh.txt");
    Map<int> counts;
    ReadFile(in, counts);
    Randomize();
    TestRandom();
    return 0;
}

In your ReadFile function (in multiple places) you are attempting to use a string as the key to a Map with integers as keys.

string word;
in >> word;
if (in.fail()) break;
if (m.containsKey(word)) 
    m[word]++;

In order to do this properly you will need to parse your string input into an integer. This can be achieved through the stoi function or similar.

Example

string word;
in >> word;
int iKey = stoi(word, nullptr);
if (in.fail()) break;
if (m.containsKey(iKey)) 
    m[iKey]++;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM