代码之家  ›  专栏  ›  技术社区  ›  Giogre

C++错误C2512试图使用IFStand读取类中的文件VisualStudio 2008

  •  -1
  • Giogre  · 技术社区  · 6 年前

    我承认我是一个极端的C++新手,所以请原谅我的一个很幼稚的问题。

    我正在编写代码,该代码将分析汇编语言文件的基本部分,并在第二阶段转换为机器语言。

    我建造了一个 parser 但是我没有成功地打开外部程序集 .asm 文本文件,并将其提供给组成我的 语法分析器 班级。

    更具体地说,构造函数存在问题。 我附上我写在下面的完整代码:

    // parses .asm assembly files
    #include <iostream>
    #include <fstream>
    #include <varargs.h>
    #include <string>
    using namespace std;
    
    class parser
    {
    private:
        istream inputfile;
        char inputname[30];
        string line;
        bool endfile;
        bool a_command, l_command, c_command; 
        string parsedLine, destParsedLine, compParsedLine, jumpParsedLine;
    public:
        // default parser constructor
        parser()
        {
        }
    
        //parser(char* argv[])
        //{
        //  reader(argv[]);
        //}
    
        // opens input file
        string reader(char* argv[])
        {
            strcpy(inputname,argv[1]);
            strcat(inputname,".asm");
            // opens input .asm file
            ifstream inputfile(inputname);
            // reads first line
            getline(inputfile,line);
            if (line[0] == '/' || line.empty())
                inputfile.ignore(line.length(),'\n');
            return line;
        }
    
        // checks if at end file
        bool hasMoreCommands()
        {
            a_command = false;
            l_command = false;
            c_command = false;
            endfile = false;
            if (inputfile.eof())
                endfile = true;         
            return endfile;
        }
    
        // advances read of inputfile
        void advance()
        {
            if (line[0] == '/' || line.length() == 0)
                inputfile.ignore(line.length(),'\n');
            getline(inputfile,line);
        }
    
        /* function for labelling the type of command (address,computation,label) */
        bool commandType()
        {
            if (line[0] == '@')
                a_command = true;
            else if (line[0] == '(')
                l_command = true;
            else
                c_command = true;
            return a_command, l_command, c_command;
        }
    
        // function to select parsing function
        string selector()
        {
            if (a_command || l_command)
                symbol();
            else if (c_command)
            {
                dest();
                comp();
                jump();
                string parsedLine = destParsedLine + compParsedLine + jumpParsedLine;
            }
            return parsedLine;
        }
    
        // function returning address or label symbol
        string symbol()
        {
            if (a_command)
                string parsedLine = line.substr(1);
            else if (l_command)
                string parsedLine = line.substr(1,line.length()-1);
            return parsedLine;
        }
    
        // functions returning computation destination
        string dest()
        {
            size_t equal = line.find('='); //no '=' found = returns 'npos'
            string destParsedLine = line.substr(0,equal);
            return destParsedLine;
        }
        string comp()
        {
            size_t equal = line.find('=');
            size_t semicolon = line.find(';');
            string compParsedLine = line.substr(equal,semicolon);
            return compParsedLine;
        }
        string jump()
        {
            size_t semicolon = line.find(';');
            string jumpParsedLine = line.substr(semicolon);
            return jumpParsedLine;
        }
    };
    
    // main program
    int main (int argc, char *argv[])
    {
        bool endfile = false;
        string parsedLine;
        int count = 0;
    
        if ((argc != 2) || (strchr(argv[1],'.') != NULL))
        {
            cout << argv[0] << ": assembly .asm file argument should be supplied, without .asm extension\n";
            return 1;
        }
    
        parser attempt1 = parser();
        attempt1.reader(argv[]);
        while (!endfile)
        {
            attempt1.hasMoreCommands();
            if (endfile)
                return 0;
            if (count > 0)
                attempt1.advance();
            attempt1.commandType();
            attempt1.selector();
            cout << parsedLine << endl; //debugging purposes
            count++;
        }
    }
    

    我提供了 ASM 从命令行打开文本文件( ASM 文件位于此文件夹的同一文件夹中 cpp 文件)。

    因此我需要使用 varargs.h 我想这可能是问题的一部分。

    当我尝试构建此版本时,Visual Studio 2008会给出以下2个错误:

    1误差 C2512 :'std::basic_istream<_elem,_traits>':没有合适的默认构造函数可用 第21行

    2误差 C2059 :语法错误:“]” 第137行

    帮助被欣赏,侮辱被容忍,谢谢:)

    2 回复  |  直到 6 年前
        1
  •  0
  •   Remy Lebeau    6 年前

    你的班级使用 std::istream 对于 inputfile 成员,但不初始化它。那不管用。

    在这种情况下,您的类需要使用 std::ifstream 代替它 输入文件 成员,然后调用其 open() 方法,然后再尝试从中读取。

    还有,你的 reader() 方法正在忽略 输入文件 成员,而不是创建要从中读取的同名局部变量。您需要去掉这个局部变量,而不是调用 打开() 关于你的班级成员。

        2
  •  0
  •   Giogre    6 年前

    按照@remy lebeau的建议,下面修改的代码至少可以正确编译(但仍然不能执行它应该执行的操作)。

    // parses .asm assembly files
    #include <iostream>
    #include <fstream>
    #include <varargs.h>
    #include <string>
    using namespace std;
    
    class parser
    {
    private:
        istream inputfile;
        char inputname[30];
        string line;
        bool endfile;
        bool a_command, l_command, c_command; 
        string parsedLine, destParsedLine, compParsedLine, jumpParsedLine;
    public:
        // default parser constructor
        parser()
        {
        }
    
        // ignores inputfile line if comment or empty
        void ignoreline()
        {
            if (line[0] == '/' || line.empty())
                inputfile.ignore(line.length(),'\n');
        }
    
        // composes inputfile name and opens input file
        void reader(char* argv[])
        {
            strcpy(inputname,argv[1]);
            strcat(inputname,".asm");
            // opens input .asm file
            inputfile.open(inputname, fstream::in);
            // reads first line
            getline(inputfile,line);
            ignoreline();
        }
    
        // checks if at end file
        bool hasMoreCommands()
        {
            a_command = false;
            l_command = false;
            c_command = false;
            endfile = false;
            if (inputfile.eof())
                endfile = true;         
            return endfile;
        }
    
        // advances read of inputfile
        void advance()
        {
            ignoreline();
            getline(inputfile,line);
        }
    
        /* function for labelling the type of command (address,computation,label) */
        bool commandType()
        {
            if (line[0] == '@')
                a_command = true;
            else if (line[0] == '(')
                l_command = true;
            else
                c_command = true;
            return a_command, l_command, c_command;
        }
    
        // function to select parsing function
        string selector()
        {
            if (a_command || l_command)
                symbol();
            else if (c_command)
            {
                dest();
                comp();
                jump();
                string parsedLine = destParsedLine + compParsedLine + jumpParsedLine;
            }
            return parsedLine;
        }
    
        // function returning address or label symbol
        string symbol()
        {
            if (a_command)
                string parsedLine = line.substr(1);
            else if (l_command)
                string parsedLine = line.substr(1,line.length()-1);
            return parsedLine;
        }
    
        // functions returning computation destination
        string dest()
        {
            size_t equal = line.find('='); //no '=' found = returns 'npos'
            string destParsedLine = line.substr(0,equal);
            return destParsedLine;
        }
    
        string comp()
        {
            size_t equal = line.find('=');
            size_t semicolon = line.find(';');
            string compParsedLine = line.substr(equal,semicolon);
            return compParsedLine;
        }
    
        string jump()
        {
            size_t semicolon = line.find(';');
            string jumpParsedLine = line.substr(semicolon);
            return jumpParsedLine;
        }
    };
    
    // main program
    int main (int argc, char *argv[])
    {
        bool endfile = false;
        string parsedLine;
        int count = 0;
    
        if ((argc != 2) || (strchr(argv[1],'.') != NULL))
        {
             cout << argv[0] << ": assembly .asm file argument should be supplied, without .asm extension\n";
            return 1;
        }
    
        parser attempt1 = parser();
        attempt1.reader(argv);
        while (!endfile)
        {
            attempt1.hasMoreCommands();
            if (endfile)
                return 0;
            if (count > 0)
                attempt1.advance();
            attempt1.commandType();
            attempt1.selector();
            cout << parsedLine << endl;
            count++;
        }
        return 0;
    }