Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/150.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 错误:应在&;之前使用主表达式;代币_C++_Arrays_Class_Pointers - Fatal编程技术网

C++ 错误:应在&;之前使用主表达式;代币

C++ 错误:应在&;之前使用主表达式;代币,c++,arrays,class,pointers,C++,Arrays,Class,Pointers,我正在编写一个程序,获取一个输入文件并对其进行解析,它应该创建两个数组单词和频率,然后打印出其中的内容。 Word将包含文件中的所有单词,而不会重复它们。频率将具有每个单词在文件上出现的次数 然而,我似乎无法让主文件正确使用这些文件,并且在我尝试运行程序时,我不断地出现这些错误 main.cpp:21: error: no matching function for call to 'FileParser::parseFile(Analysis& (&)())' filePars

我正在编写一个程序,获取一个输入文件并对其进行解析,它应该创建两个数组
单词
频率
,然后打印出其中的内容。 Word将包含文件中的所有单词,而不会重复它们。频率将具有每个单词在文件上出现的次数

然而,我似乎无法让主文件正确使用这些文件,并且在我尝试运行程序时,我不断地出现这些错误

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& ()()'
我的代码:

//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
//MAIN
#包括
#包括
#包括
使用名称空间std;
#包括“analysis.h”
#包括“fileParser.h”
int main(int argc,char*argv[])
{
分析a();
//输入解析
FileParser输入(“input.txt”);
输入解析文件(a);
//输出
流输出(“output.txt”);
a、 printListAll(输出);
返回0;
}
====================
使用名称空间std;
#包括“word.h”
#ifndef分析
#定义分析
类分析{
私人:
单词**词表;
int字表大小;
int字列表容量;
//调整单词列表数组的大小
void resize()
{
单词**临时=新单词*[wordListCapacity+10];
for(int i=0;iequals(str))
返回true;
}
返回false;
}
void addWord(字符str[],整数频率)
{
if(wordListSize==wordListCapacity)
调整大小();
//检查是否已经存在,如果只是
//增加频率
else if(isInAnalysis(str))
{
for(int i=0;iequals(str))
{
wordList[i]->addFrequencyToList(str);
打破
}
}
}
//如果这是个新词
其他的
{
wordList[wordListSize]=新词(str);
wordList[wordListSize]>addFrequencyToList(str);
wordListSize++;
}
}
//以所有输出形式打印分析(-a)
void printListAll(ostream&out)
{
索塔纳();
int*tempFrequency;
for(int i=0;igetWord();
int numFrequency=wordList[i]->getFrequency();
tempFrequency=新的整数[numFrequency];
wordList[i]->getFrequencyList(tempFrequency);
//手柄包装
字符输出[55];
strcpy(tempOutput,str);
strcat(tempOutput,“=”);
字符tempPageNumber[50];
对于(int i=0;i49)
{

out如果您替换您的声明,您看到的问题将得到解决

Analysis a();

例如,见本问题:

或者看看克里斯在评论中提供的链接


下次提出问题时,请尝试删除尽可能多的代码。以下内容足以发现问题:

#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;
}
#包括
#包括
#包括
使用名称空间std;
类分析{
私人:
分析(常量分析&){};
公众:
分析{};
};
类文件解析器
{
公众:
文件解析器(char*fName);
~FileParser();
//解析给定的文件
无效分析文件(分析与分析);
}
int main(int argc,char*argv[])
{
分析a();
//输入解析
FileParser输入(“input.txt”);
输入解析文件(a);
//输出
流输出(“output.txt”);
a、 printListAll(输出);
返回0;
}
您是否注意到,现在发现问题只需快速浏览一下,而不是提供大量与您看到的错误消息毫无关联的代码?通常,提出好问题会迫使您将问题分解为问题的根源。通常,您在尝试这样做时会自己发现问题。如果您没有发现问题,你会得到很多帮助,只是因为你表明你确实试图解决这个问题,而且你的问题很容易理解。没有人愿意为了这样一个小问题滚动两百行代码。

我会给你一点提示。但是,这比导致错误的代码还要多。减少迷你们的问题
#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;
}