【发布时间】:2015-09-23 21:49:58
【问题描述】:
我正在尝试为一个类编写一些程序,该类涉及使用Token 类创建一个对象并将其打印出来。这是我的主要方法:
int main(int argc, char** argv) {
Token tok(10, "test", 1, 2);
printf("%d\n", tok.type());
}
这是我的Token.cc 文件及相关方法:
Token::Token(int t, string str, int l, int c) {
int tokType = t;
string lexStr = str;
int lineNum = l;
int charPos = c;
}
int Token::type() {
return tokType;
}
这是我的Token.h 文件:
#ifndef TOKEN_H
#define TOKEN_H
using namespace std;
#include <string>
class Token{
private:
public:
Token(int t, string str, int l, int c);
~Token();
int type();
int tokType;
string lexStr;
int lineNum;
int charPos;
};
#endif
程序编译得很好,但是当我运行它时,它会打印一个看似随机的数字,例如 1475212264 或 -258154088 或该长度的其他一些随机数。
有什么想法可能会出错吗?这让我发疯了。
谢谢
【问题讨论】:
-
所以我不需要在.cc文件中重新定义
lexStr? -
minimal reproducible example 请像往常一样。您的问题缺乏必要的信息/调试工作。