简体   繁体   English

找不到头文件cpp

[英]Header file not found cpp

main.cpp main.cpp中

#include <iostream>
//#include "cfgtocnf.h"
#include "cyk.h"
using namespace std;



    int main(){
        int option;
        cout << "Select an option below: \n";
        cout << "1. CFG to CNF \n";
        cout << "2. CYK \n";
        cin >> option;

        switch(option){
            case 1:
                //output();
                break;
            case 2:
                cykoutput();
                break;

        }
    }

cyk.h cyk.h

#ifndef CYK_H
#define CYK_H

// This is the content of the .h file, which is where the declarations go
void cykoutput(); 

// This is the end of the header guard
#endif

cyk.cpp cyk.cpp

//CNF grammer to CYK Chart
//statement to infom user whether the string can/cannot be generated by the grammar

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
#include "cyk.h"

using namespace std;

string grammer[50][50];

bool state_checking1( string a){
    if ( a.length() == 1 && (a[0] >= 'A' && a[0] <='Z') ) {
        return true;
    }
    else{
        return false;
    }
}

bool input_checking(string a){
    if (a.length() == 1 && (a[0] >= 'a' && a[0] <= 'z')) {
        return true;
    }
    else if (a.length() == 2 && (a[0] >= 'A' && a[0] <= 'Z' && a[1] >= 'A' && a[1] <= 'Z')){
        return true;
    }
    else{
        return false;
    }
}

bool remove_line1(int line , string a){
    bool end = false;
    bool check_grammer = true;
    size_t line_position = -1;
    int y = 1 , start = 0;

    while (!end && check_grammer){
        line_position = a.find("|");
        if (line_position != -1){
            a.erase(line_position,1);
            line_position = line_position - start;
            grammer[line][y] = a.substr(start, line_position);
            check_grammer = input_checking(grammer[line][y]);
            if (check_grammer == false){
                break;
            }
            y++;
            start = start + line_position;
        }
        else{
            end = true;
            grammer[line][y] = a.substr(start, line_position);
            check_grammer = input_checking(grammer[line][y]);
        }
    }

    return check_grammer;
}

string search(string a, int grammer_line_count){
    string temp = "";
    int k;
    for (int j = 0; j < grammer_line_count; j++){
        k = 1;
        while (grammer[j][k] != "")
        {
            if (grammer[j][k] == a)
            {
                temp += grammer[j][0];
            }
            k++;
        }
    }

    return temp;
}

string compare(string a, string b, int grammer_line_count){
    string temp ,temp2 = "";
    string aa = a;
    string bb = b;

    if (aa.find("_") ==0){
        aa = aa.substr(1, aa.length());
    }

    if (bb.find("_") == 0){
        bb = bb.substr(1, bb.length());
    }

    for (int i = 0; i < aa.length();  i++){
        for (int j = 0; j < bb.length(); j++){
            temp = "";
            temp = temp + aa[i] + bb[j];
            temp2 = temp2 + search(temp, grammer_line_count);
        }
    }

    return temp2;
}

void cykoutput(){
    ifstream file;
    string file_name , file_line , input_string;
    size_t search_pointer;
    bool done = false;
    bool isGrammer;
    string check_generated;

    //to output to file
    ofstream myfile;
    myfile.open ("cyk.txt");


    while (!done) {
        int grammer_line_count = 0;
        isGrammer = true;
        cout<<"=============================================================================="<<endl;
        cout<<"\t\tCNF Grammar to CYK Chart Generator\n";
        cout<<"==============================================================================\n"<<endl;
        cout<<"**Enter -1 to exit**\nPlease enter CNF Grammar file name (e.g. xxx.txt) : ";
        cin>>file_name;

        if (file_name != "-1"){
            file.open(file_name.c_str());
            if (file) {
                cout << "\nCNF Grammer : \n" << endl;
                for (int i = 0; getline(file, file_line); i++) {
                    cout << file_line << endl;
                    search_pointer = file_line.find("->");
                    if (i == 0){
                        check_generated = file_line.substr(0, search_pointer);
                    }

                    grammer[i][0] = file_line.substr(0, search_pointer);
                    grammer_line_count++;
                    if (isGrammer) {
                        isGrammer = state_checking1(grammer[i][0]);
                        file_line = file_line.substr(search_pointer + 2, file_line.length());
                        isGrammer = remove_line1(i, file_line);
                    }
                }

                file.close();

                if (!isGrammer) {
                    cout << "\nInvalid grammar!!!" << endl;
                }
                else{
                    cout << "\nEnter the input string : ";
                    cin >> input_string;

                    //Assign value-----------------
                    string temp;
                    string temp2;
                    string cykTable[50][50];


                    int input_length = input_string.length();
                    for (int i = 0; i < input_length; i++){
                        int y = 0;
                        temp = "";
                        temp2 = "";
                        temp+= input_string[i];

                        temp2 = search(temp, grammer_line_count);
                        cykTable[i][y] = temp2;
                    }

                    //compare the value------------
                    int x,y;
                    for (int i = 1; i < input_length; i++){ //already write 1st 1ine need to +1
                        y = i - 1;
                        x = 0;
                        for (int j = 0; j < (input_length - i); j++){
                            if (cykTable[x][y] != "DONE"){
                                if (cykTable[x][y + 1] == ""){
                                    if (cykTable[x + 1][y] != "DONE"){
                                        temp = "";
                                        temp = compare(cykTable[x][y], cykTable[x + 1][y], grammer_line_count);
                                        if (temp != ""){
                                            cykTable[x + 1][y + 1] = "DONE";
                                        }
                                        else{
                                            if (cykTable[x][y].find("_") == 0){
                                                temp = cykTable[x][y];
                                            }
                                            else{
                                                temp = "_" + cykTable[x][y];
                                            }
                                        }

                                        cykTable[x][y + 1] = temp;
                                    }
                                    else{
                                        int xx = x;
                                        while (cykTable[xx + 1][y] == "DONE"){
                                            xx++;
                                        }
                                        temp = "";
                                        temp = compare(cykTable[x][y], cykTable[xx + 1][y], grammer_line_count);
                                        if (temp != ""){
                                            cykTable[xx + 1][y + 1] = "DONE";
                                        }
                                        else{
                                            if (cykTable[x][y].find("_") == 0){
                                                temp = cykTable[x][y];
                                            }
                                            else{
                                                temp = "_" + cykTable[x][y];
                                            }
                                        }

                                        cykTable[x][y + 1] = temp;
                                    }
                                }

                            }
                            x++;
                        }

                        for (int k = 0; k < input_length; k++){
                            if (cykTable[k][i] == ""){
                                if (cykTable[k][i - 1] != "DONE"){
                                    if (cykTable[k][i-1].find("_") == 0){
                                        cykTable[k][i] = cykTable[k][i - 1];
                                    }
                                    else{
                                        cykTable[k][i] = "_" + cykTable[k][i-1];
                                    }

                                }
                                else{
                                    cykTable[k][i] = "DONE";
                                }
                            }
                        }
                    }

                    //print out the cyk table------
                    myfile << "\n=========================================================" << endl;
                    myfile << "\t\tCYK Chart\n";
                    myfile << "=========================================================\n" << endl;

                    string check123;
                    y = 0;
                    for (int i = 0; i < input_length; i++){
                        if (i == input_length - 1){
                            if (cykTable[0][y].find("_") != -1){
                                search_pointer = cykTable[0][y].find("_");
                                cykTable[0][y] = cykTable[0][y].substr(search_pointer + 1, cykTable[0][y].length());
                            }
                            myfile<< cykTable[0][y];
                        }
                        else
                        {
                            for (int j = 0; j < input_length - i; j++){
                                check123 = "";

                                if (cykTable[j][y] == "DONE" || (cykTable[j][y].find("_") == 0)){
                                    check123 = "";
                                }
                                else{
                                    check123 = cykTable[j][y];
                                }

                                myfile << check123 << "\t";
                            }
                        }

                        y++;
                        myfile << endl;

                    }

                    //check either can be generated

                    int test = cykTable[0][input_length - 1].find(check_generated);
                    if ( test >= 0){
                        myfile << "\nString can be generated!!!\n"<<endl;
                    }
                    else{
                        myfile << "\nString can not be generated!!!\n"<<endl;
                    }

                } //end else

            }
            else {
                cout << "\nFile Not Found!!!" << endl;
            }

            system("PAUSE");
            system("CLS");

            for (int i = 0; i < 50; i++){
                for (int j = 0; j < 50; j++){
                    grammer[i][j] = "";
                }
            }
        }
        else{
            done = true;
        }
    }


}

When I tried to compile, it shows 当我试图编译时,它显示

fatal error: cyk.h: no such file or directory:

What's wrong here ? 这有什么不对? I am using codeblocks and all the files are under one project. 我正在使用代码块,所有文件都在一个项目下。 I have already created the cyk.h under Header section but it says no such file or directory. 我已经在Header部分下创建了cyk.h,但它没有说明这样的文件或目录。

问题与您的源代码本身无关,请检查您的编译器设置:请注意源代码和头文件目录是单独设置的(尽管它们可能指向同一目录)

请检查代码块的构建选项中的头文件搜索路径配置。

If you want a shortcut solution: Open Code::Blocks, open "Settings" menu, click "Compiler...". 如果需要快捷方式解决方案:打开Code :: Blocks,打开“设置”菜单,单击“编译器...”。 Open the "Build Options" tab, and check the option "Explicitly add the current compiling file's directory...". 打开“构建选项”选项卡,然后选中“显式添加当前编译文件的目录...”选项。 在此输入图像描述

If not, you should configure your Compiler settings. 如果没有,您应该配置编译器设置。 If you don't know, then try crazy and learn. 如果你不知道,那就试着疯狂学习吧。 It's in "Search Directories" tab under "Global Compiler Settings". 它位于“全局编译器设置”下的“搜索目录”选项卡中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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