user2049409
user2049409

Reputation: 9

error: expected primary-expression before & token

Im writing a program that gets an input file and parses through it, it is supposed to create two arrays WORDS and FREQUENCY and then print out what it got in them. Words will have all the words in the file without repeating them. Frequency will have the number of times each word appears on the file.

However I cant seem to get the main to use the files properly and I keep on getting these errors while I try to run my program.

main.cpp:21: error: no matching function for call to 'FileParser::parseFile(Analysis& (&)())'
fileParser.h:56: note: candidates are: void FileParser::parseFile(Analysis&)
main.cpp:25: error: request for member 'printListAll' in 'a', which is of non-class type 'Analysis& ()()'

My code:

//MAIN


#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;

#include "analysis.h"


  #include "fileParser.h"



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

        Analysis a();

        //Input Parse
        FileParser input("input.txt");
        input.parseFile(a);

    //Output
    ofstream outP("output.txt");
    a.printListAll(outP);

    return 0;
}




====================



using namespace std;

#include "word.h"

#ifndef ANALYSIS_H
#define ANALYSIS_H

class Analysis{

    private:

        Word** wordList;
        int wordListSize;
        int wordListCapacity;


        //Resizes the wordList array
        void resize()
        {
            Word** temp = new Word* [wordListCapacity + 10];
            for(int i = 0; i < wordListSize; i++)
                temp[i] = wordList[i];

            delete [] wordList;
            wordList = temp;
            wordListCapacity += 10;
        }



        //Sorts the words in the analysis
        void sortAna()
        {
            for (int i = 0; i < wordListSize - 1; i++)
            {
                int curSmallest = i;
                for (int j = i + 1; j < wordListSize; j++)
                    if (*wordList[j] < *wordList[curSmallest])
                        curSmallest = j;
                if(curSmallest != i)
                {
                    Word* temp = wordList[i];
                    wordList[i] = wordList[curSmallest];
                    wordList[curSmallest] = temp;
                }
            }
        }

        Analysis(const Analysis&){}

    public:


        Analysis(){
            wordListSize = 0;
            wordListCapacity = 10;
            wordList = new Word*[wordListCapacity];
        }


        ~Analysis()
        {
            for (int i = 0; i < wordListSize; i++)
                delete wordList[i];
            delete [] wordList;
        }




        bool isInAnalysis(char str[]){
            for (int i = 0; i < wordListSize; i ++)
            {
                if (wordList[i]->equals(str))
                    return true;
            }
            return false;
        }


        void addWord(char str[], int frequency)
        {
            if (wordListSize == wordListCapacity)
                resize();


            //Check to see if the exists already, if it does just
            //add to the frequency
            else if (isInAnalysis(str))
            {
                for (int i = 0; i < wordListSize; i++)    
                {
                    if (wordList[i]->equals(str))
                    {
                        wordList[i]->addFrequencyToList(str);
                        break;
                    }
                }
            }

            //If its a new word
            else
            {
                wordList[wordListSize] = new Word(str);
                wordList[wordListSize] -> addFrequencyToList(str);
                wordListSize++;
            }
        }



        //Print the Analysis in all output form (-a)
        void printListAll(ostream& out)
        {
            sortAna();
            int * tempFrequency;
            for (int i = 0; i < wordListSize; i++)
            {
                char* str = wordList[i] -> getWord();

                int numFrequency = wordList[i] -> getFrequency();
                tempFrequency = new int[numFrequency];
                wordList[i] -> getFrequencyList(tempFrequency);

                //Handle wrapping

                char tempOutput[55];
                strcpy(tempOutput, str);
                strcat(tempOutput, " = ");
                char tempPageNumber[50];


                for (int i = 0; i < numFrequency; i++){
                    sprintf(tempPageNumber, "%d", tempFrequency[i]);
                    int currPageLen = strlen(tempPageNumber);


                    if(strlen(tempOutput) + currPageLen > 49)
                    {
                        out << tempOutput << endl;
                        strcpy(tempOutput, "    ");
                    }
                    strcat(tempOutput, tempPageNumber);
                    if(i != numFrequency - 1)
                        strcat(tempOutput, "  ");
                }
                //Flush the last set of contents of the buffer
                out << tempOutput << endl;
                delete[] tempFrequency;
                delete[] str;
            }
        }


};

#endif


==========



#include "analysis.h"
#include "word.h"


#ifndef FILEPARSER_H
#define FILEPARSER_H

class FileParser
{
    private:

        char* fileName;
        ifstream cin;

    public:

        FileParser(char* fName)
        {

            fileName = new char[strlen(fName) + 1];
            strcpy(fileName, fName);

            cin.open(fileName);

            if (!cin.is_open())
                cerr << "****ERROR:  Cannot open Input file to parse it. ****" << endl;
        }


        ~FileParser()
        {
            cin.close();
        }


        bool isFileOpen(){
            return cin.is_open();
        }


        //Returns the length of the string given as an int
        int getFrequencyNumber(char* str)
        {
            return atoi(str);
        }


        //Parses the given File
        void parseFile(Analysis& a)
        {

            char currentWord[50] = {0};
            char currentFrequencyString[10] = {0} ;
            int currentFrequency;

            cin >> currentWord;
            while (currentWord[1] != '-')
            {

                if(currentWord[0] == ' ')
                {
                    //copy from 2nd character of currentWord to before the space
                    //which is at strlen(currentWord)
                    int count = 1;
                    while (currentWord[count] != ' ')
                    {
                        currentFrequencyString[count - 1] = currentWord[count];
                        count++;
                    }

                    currentFrequencyString[count - 1] = '\0';
                    currentFrequency = getFrequencyNumber(currentFrequencyString);
                }

                else
                {
                    //Convert the word to lower case
                    Word::toLower(currentWord);

                    //add to the index (passed in as param) with current freq.
                    a.addWord(currentWord, currentFrequency);
                }
                //get next word
                cin >> currentWord;
            }
        }


};

#endif

===============




using namespace std;

#ifndef WORD_H
#define WORD_H

class Word{

    private:
        char* string;
        int* frequencyList;
        int frequencySize; //Actual number of frequencies in the list.
        int frequencyCap;  //Total size of frequency
        int wordFrequency; //Frequency of a certain word.


        void resizeFrequency(){
            int* temp = new int[frequencyCap+10];
            for (int i = 0; i < frequencyCap; i++)
                temp[i] = frequencyList[i];
            delete [] frequencyList;
            frequencyCap += 10;
            frequencyList = temp;
        }

    public:
        /**
         * This constructor constructs the word
         * with the parameter value and initializes
         * the frequencyList
         */
        Word(char* input)
        {
            string = new char[strlen(input) + 1];
            strcpy (string, input);
            frequencyList = new int[10];
            frequencySize = 0;
            frequencyCap= 10;
            wordFrequency = 0;
        }


        // Copy constructor for word
        Word (const Word& rhs)
        {
            string = new char[strlen(rhs.string) + 1];
            strcpy(string, rhs.string);
            frequencyList = new int[rhs.frequencyCap];
            for (int i = 0; i < rhs.frequencyCap; i++)
                frequencyList[i] = rhs.frequencyList[i];
            frequencyCap = rhs.frequencyCap;
            frequencySize = rhs.frequencySize;
            wordFrequency = rhs.wordFrequency;
        }

        //Destructor
        ~Word()
        {
            delete[]string;
            delete[]frequencyList;
        }


       //Overloaded assignment operator
        Word& operator=(const Word& rhs)
        {
            if(this != &rhs)
            {
                delete [] string;
                delete [] frequencyList;

                string = new char[strlen(rhs.string) + 1];
                strcpy(string, rhs.string);
                frequencyList = new int[rhs.frequencyCap];
                for (int i = 0; i < rhs.frequencyCap; i++)
                    frequencyList[i] = rhs.frequencyList[i];
                frequencyCap = rhs.frequencyCap;
                frequencySize = rhs.frequencySize;
                wordFrequency = rhs.wordFrequency;
            }

            return *this;
        }


        //Seeing if given word matches another word.
        bool equals(char* temp)
        {
            if(strcmp(string, temp) == 0)
                return true;
            return false;
        }


        //Adds the frequency of the given word to the list
        //and if it already exits, frequency++
        void addFrequencyToList(char* temp)
        {
            if (frequencySize == frequencyCap)
                resizeFrequency();

            //Only adds a new frequency if the word doesn't
            //already appear in the list.
            if (equals(temp))
            {
                frequencyList[frequencySize] = wordFrequency;
                frequencySize++;
                wordFrequency++;
            }
        }


        //Gets the total size of the frequency list.
        int getFrequency()
        {   
            return frequencySize;
        }


        //Gets the frequency of a word
        /*int getFrequencyOfWord(char* temp)
        {
            for(int i=0; i<=frequencySize; i++)
            {
                if (*temp == string[i])
                    return frequencyList[i];
            }
        }*/



        void getFrequencyList(int* tempFrequencyList)
        {
            sort(frequencyList, frequencyList + frequencySize);
            for (int i = 0; i < frequencySize; i++)
                tempFrequencyList[i] = frequencyList[i];
        }


        //Returns a copy of the word
        char* getWord()
        {
            char* temp = new char[strlen(string) + 1];
            strcpy(temp, string);
            return temp;
        }


        //Helper function that converts to lower case.
        static void toLower(char* str)
        {
            int len = strlen(str);
            for (int i = 0; i < len; i++)
                str[i] = tolower(str[i]);
        }


        bool operator < (const Word& rhs)
        {
            return (strcmp(this->string, rhs.string) < 0);
        }

};

#endif

Upvotes: 0

Views: 3577

Answers (1)

Thilo
Thilo

Reputation: 9157

The problems you see will be solved if you replace your declaration of

Analysis a();

with

Analysis a;

See for example this question: Default constructor with empty brackets

Or look at the link chris provided in his comment.


The next time you ask a question, please try to remove as much code as possible. The following would have been enough to spot the problem:

#include <iostream>
#include <cstdlib>
#include <fstream>

using namespace std;


class Analysis {
    private:
        Analysis(const Analysis&) {};
    public:
        Analysis() {};
};

class FileParser
{
    public:

        FileParser(char* fName);
        ~FileParser();

        //Parses the given File
        void parseFile(Analysis& a); 

}

int main(int argc, char *argv[])
{
    Analysis a();

    //Input Parse
    FileParser input("input.txt");
    input.parseFile(a);

    //Output
    ofstream outP("output.txt");
    a.printListAll(outP);

    return 0;
}

Do you notice that spotting the problem now needs only a quick glance, as opposed to the tons of code you provided that were in no way connected with the error message you saw? Usually, asking good questions forces you to break the problem down to its roots. Commonly you find the problem yourself when trying to do so. And if you do not find it, you will get tons of help, just because you showed that you actually tried to solve the problem and your problem is easy to understand. Nobody is willing to scroll through two hundred lines of code just for a small problem like this.

Upvotes: 1

Related Questions