- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在开发一个程序,该程序应该解析命令行输入、读取输入文本文件,然后执行测试文件中指定的一系列步骤。
在分词器类上工作了一段时间后,我遇到了瓶颈,即一个错误,我不太确定如何解决。
所以,我有 Tokenizer.h:
#ifndef _TOKENIZER_GUARD
#define _TOKENIZER_GUARD 1
#include <string>
#include <cstdlib>
#include <climits>
#include <cfloat>
#include <iostream>
#include <fstream>
#include "Token.h"
//
// A class to create a sequence of tokens from an input file stream.
//
class Tokenizer
{
public:
Tokenizer(const std::string&, std::ostream&);
Tokenizer(Tokenizer&); // Copy Constructor -I
virtual ~Tokenizer();
virtual int nextInt();
virtual bool hasNextInt() const;
virtual long nextLongInt();
virtual bool hasNextLongInt() const;
virtual float nextFloat();
virtual bool hasNextFloat() const;
virtual std::string next();
virtual bool hasNext() const;
Tokenizer& operator= (Tokenizer&); // overloaded equals operator
protected:
private:
Tokenizer();
std::ifstream stream;
std::ostream _os;
Token _token;
};
#endif
和 Tokenizer.cpp:
#include <string>
#include <cstdlib>
#include <climits>
#include <cfloat>
#include <iostream>
#include <fstream>
#include "Tokenizer.h"
Tokenizer::Tokenizer()
{ //error occurs here
}
// Constructor
Tokenizer::Tokenizer(const std::string& s, std::ostream& o)
{ //error occurs here
stream.open(s);
std::string t;
getline(stream, t, ' ');
_token = Token(t);
}
// Copy Constructor
Tokenizer::Tokenizer(Tokenizer& t) :_token(t._token), stream(t.stream), _os(t._os)
{
}
// Destructor
Tokenizer::~Tokenizer()
{
stream.close();
}
// Saves current int to return, then moves _token to the next value
int Tokenizer::nextInt()
{
int temp = _token.toInteger();
std::string t;
getline(stream, t, ' ');
_token = Token(t);
return temp;
}
// Checks to see if there is a next value and if it is an int
bool Tokenizer::hasNextInt() const
{
return _token.isInteger() && hasNext();
}
// same as nextInt but long -I
long Tokenizer::nextLongInt()
{
long temp = _token.toLongInteger();
std::string t;
getline(stream, t, ' ');
_token = Token(t);
return temp;
}
// same as hasNextInt but Long
bool Tokenizer::hasNextLongInt() const
{
return _token.isLongInteger() && hasNext();
}
// same as nextInt but Float
float Tokenizer::nextFloat()
{
float temp = _token.toFloat();
std::string t;
getline(stream, t, ' ');
_token = Token(t);
return temp;
}
// same as hasNextInt but float
bool Tokenizer::hasNextFloat() const
{
return _token.isFloat() && hasNext();
}
//Returns the next token
std::string Tokenizer::next()
{
std::string temp = _token.get();
std::string t;
getline(stream, t, ' ');
_token = Token(t);
return temp;
}
//True when it is not the end of the file
bool Tokenizer::hasNext() const
{
return stream.eofbit != 1;
}
//Overloaded = operator
Tokenizer& Tokenizer::operator= (Tokenizer& t)
{
_token = t._token;
stream = t.stream;
_os = t._os; //error occurs here
}
我得到了错误
'std::basic_ostream>': no appropriate default >constructor available
为构造函数。稍后,当我使用 'stream = t.stream' 和 '_os = t._os' 时出现错误
function "std::basic_ostream<_Elem, _Traits>::operator=(const >std::basic_ostream<_Elem, _Traits>::_Myt &) [with _Elem=char, >_Traits=std::char_traits]" (declared at line 85 of >"x:\Visual\VC\include\ostream") cannot be referenced -- it is a deleted >function
我一直在尝试为第一个错误找到某种解决方案,但我不明白是什么导致了它。有人向我建议创建一个复制构造函数,但它似乎并没有解决这个问题。我还在下面包含了 Token.h。
#ifndef _TOKEN_GUARD
#define _TOKEN_GUARD 1
#include <string>
#include <cstdlib>
#include <climits>
#include <cfloat>
//
// Token class
//
class Token
{
public:
Token(const std::string& t) : _token(t) { }
virtual std::string get() const { return _token; }
virtual long toLongInteger() const;
virtual bool isLongInteger() const;
virtual int toInteger() const; // for int and is int functions
virtual bool isInteger() const; //
virtual float toFloat() const;
virtual bool isFloat() const;
virtual bool isNonNumeric() const;
Token() : _token("") {}
std::string _token;
protected:
};
#endif
如果我过于含糊或类似的话,我提前道歉。我非常乐意提供任何其他需要的东西(因为我现在卡住了)。
最佳答案
编译错误信息很清楚。当您使用时:
Tokenizer::Tokenizer(const std::string& s, std::ostream& o)
{ //error occurs here
stream.open(s);
std::string t;
getline(stream, t, ' ');
_token = Token(t);
}
成员变量os_
是默认构造的。由于std::ostream
中没有默认构造函数,os_
无法初始化。
我猜你的意思是用Tokenizer
的构造函数的输入参数来初始化成员变量os_
,比如:
Tokenizer::Tokenizer(const std::string& s, std::ostream& o) : os_(o)
{
...
即使这样也行不通,因为 std::ostream
没有复制构造函数。您需要将成员变量更改为引用对象。
std::ostream& os_;
然后,您可以安全地使用:
Tokenizer::Tokenizer(const std::string& s, std::ostream& o) : os_(o)
{
...
您只需确保输入参数 o
在 Tokenizer
被销毁之前没有被销毁。否则,Tokenizer
对象将留下悬空引用。
关于c++ - 类不存在默认构造函数的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35307756/
C语言sscanf()函数:从字符串中读取指定格式的数据 头文件: ?
最近,我有一个关于工作预评估的问题,即使查询了每个功能的工作原理,我也不知道如何解决。这是一个伪代码。 下面是一个名为foo()的函数,该函数将被传递一个值并返回一个值。如果将以下值传递给foo函数,
CStr 函数 返回表达式,该表达式已被转换为 String 子类型的 Variant。 CStr(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CSng 函数 返回表达式,该表达式已被转换为 Single 子类型的 Variant。 CSng(expression) expression 参数是任意有效的表达式。 说明 通常,可
CreateObject 函数 创建并返回对 Automation 对象的引用。 CreateObject(servername.typename [, location]) 参数 serv
Cos 函数 返回某个角的余弦值。 Cos(number) number 参数可以是任何将某个角表示为弧度的有效数值表达式。 说明 Cos 函数取某个角并返回直角三角形两边的比值。此比值是
CLng 函数 返回表达式,此表达式已被转换为 Long 子类型的 Variant。 CLng(expression) expression 参数是任意有效的表达式。 说明 通常,您可以使
CInt 函数 返回表达式,此表达式已被转换为 Integer 子类型的 Variant。 CInt(expression) expression 参数是任意有效的表达式。 说明 通常,可
Chr 函数 返回与指定的 ANSI 字符代码相对应的字符。 Chr(charcode) charcode 参数是可以标识字符的数字。 说明 从 0 到 31 的数字表示标准的不可打印的
CDbl 函数 返回表达式,此表达式已被转换为 Double 子类型的 Variant。 CDbl(expression) expression 参数是任意有效的表达式。 说明 通常,您可
CDate 函数 返回表达式,此表达式已被转换为 Date 子类型的 Variant。 CDate(date) date 参数是任意有效的日期表达式。 说明 IsDate 函数用于判断 d
CCur 函数 返回表达式,此表达式已被转换为 Currency 子类型的 Variant。 CCur(expression) expression 参数是任意有效的表达式。 说明 通常,
CByte 函数 返回表达式,此表达式已被转换为 Byte 子类型的 Variant。 CByte(expression) expression 参数是任意有效的表达式。 说明 通常,可以
CBool 函数 返回表达式,此表达式已转换为 Boolean 子类型的 Variant。 CBool(expression) expression 是任意有效的表达式。 说明 如果 ex
Atn 函数 返回数值的反正切值。 Atn(number) number 参数可以是任意有效的数值表达式。 说明 Atn 函数计算直角三角形两个边的比值 (number) 并返回对应角的弧
Asc 函数 返回与字符串的第一个字母对应的 ANSI 字符代码。 Asc(string) string 参数是任意有效的字符串表达式。如果 string 参数未包含字符,则将发生运行时错误。
Array 函数 返回包含数组的 Variant。 Array(arglist) arglist 参数是赋给包含在 Variant 中的数组元素的值的列表(用逗号分隔)。如果没有指定此参数,则
Abs 函数 返回数字的绝对值。 Abs(number) number 参数可以是任意有效的数值表达式。如果 number 包含 Null,则返回 Null;如果是未初始化变量,则返回 0。
FormatPercent 函数 返回表达式,此表达式已被格式化为尾随有 % 符号的百分比(乘以 100 )。 FormatPercent(expression[,NumDigitsAfterD
FormatNumber 函数 返回表达式,此表达式已被格式化为数值。 FormatNumber( expression [,NumDigitsAfterDecimal [,Inc
我是一名优秀的程序员,十分优秀!