Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/135.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++ - Fatal编程技术网

C++ 如何在给定此定义的情况下创建类的实例

C++ 如何在给定此定义的情况下创建类的实例,c++,C++,我得到了一个名为lexer.h的头文件,它预定义了一个名为Token的类。但是,我不理解构造函数。例如,给定下面的lexer.h,我如何创建一个TokenType=T_ID,lexeme=“this”,lnum=2的Token实例?谢谢 #ifndef LEXER_H_ #define LEXER_H_ #include <string> #include <iostream> using std::string; using std::istream; using s

我得到了一个名为lexer.h的头文件,它预定义了一个名为Token的类。但是,我不理解构造函数。例如,给定下面的lexer.h,我如何创建一个TokenType=T_ID,lexeme=“this”,lnum=2的Token实例?谢谢

#ifndef LEXER_H_
#define LEXER_H_

#include <string>
#include <iostream>
using std::string;
using std::istream;
using std::ostream;

enum TokenType {
        // keywords
    T_INT,
    T_STRING,
    T_SET,
    T_PRINT,
    T_PRINTLN,

        // an identifier
    T_ID,

        // an integer and string constant
    T_ICONST,
    T_SCONST,

        // the operators, parens and semicolon
    T_PLUS,
    T_MINUS,
    T_STAR,
    T_SLASH,
    T_LPAREN,
    T_RPAREN,
    T_SC,

        // any error returns this token
    T_ERROR,

        // when completed (EOF), return this token
    T_DONE
};

class Token {
    TokenType   tt;
    string      lexeme;
    int     lnum;

public:
    Token(TokenType tt = T_ERROR, string lexeme = "") : tt(tt), lexeme(lexeme) {
        extern int lineNumber;
        lnum = lineNumber;
    }

    bool operator==(const TokenType tt) const { return this->tt == tt; }
    bool operator!=(const TokenType tt) const { return this->tt != tt; }

    TokenType   GetTokenType() const { return tt; }
    string      GetLexeme() const { return lexeme; }
    int             GetLinenum() const { return lnum; }
};

extern ostream& operator<<(ostream& out, const Token& tok);

extern Token getToken(istream* br);


#endif /* LEXER_H_ */
\ifndef LEXER\u H_
#定义LEXER\u H_
#包括
#包括
使用std::string;
使用std::istream;
使用std::ostream;
枚举标记类型{
//关键词
T_INT,
T_弦,
图集,
T_PRINT,
T_PRINTLN,
//标识符
T_ID,,
//整数和字符串常量
T_ICONST,
斯图·斯孔斯特,
//运算符、parens和分号
T_PLUS,,
T_减去,
巨星,
T_SLASH,
托尔帕伦,
托帕尔,
理大,
//任何错误都将返回此令牌
T_错误,
//完成后(EOF),返回此令牌
完成
};
类令牌{
标记类型tt;
字符串词素;
内耳廓;
公众:
令牌(令牌类型tt=T_错误,字符串lexeme=”“):tt(tt),lexeme(lexeme){
外部内部行号;
lnum=行号;
}
bool操作符==(const TokenType tt)const{返回this->tt==tt;}
bool运算符!=(const TokenType tt)const{返回此->tt!=tt;}
TokenType GetTokenType()常量{return tt;}
字符串GetLexeme()常量{return lexeme;}
int GetLinenum()常量{return lnum;}
};

extern ostream&operator这个类有点古怪,因为它从外部变量初始化
lnum
。它实际上应该来自构造函数的参数。但既然是这样,你就不能控制它,只能通过设置
lineNumber
的值来控制它,这可能不是你想要的;它的值可能来自处理输入的任何地方,并在每一行递增

因此,要创建该类型的对象,只需执行以下操作:

Token t(T_ID, "this");

由于构造函数中的默认参数,token类型的对象可以通过三种方式创建

Token A(T_ID, "this");
Token B(T_STRING);
Token C;

后两个将具有de构造函数中定义的成员变量。

看起来您真的需要读取一个。
Token A(T_ID, "this");
Token B(T_STRING);
Token C;