我正在阅读 WikiBook“在 48 小时内为自己编写一个计划”。
Haskell 库 Parsec 用于解析基本表达式,例如数字(如下面的代码所示)。
import Lib
import Text.ParserCombinators.Parsec hiding (spaces)
import System.Environment
import Control.Monad
import Data.Typeable ( typeOf )
import Debug.Trace
data LispVal = Atom String
| List [LispVal]
| DottedList [LispVal] LispVal
| Number Integer
| String String
| Bool Bool
deriving Show
-- ...
parseNumber :: Parser LispVal
parseNumber = do
x <- many1 digit
return $ Number (read x)
Run Code Online (Sandbox Code Playgroud)
书中的一个练习要求读者parseNumber改用>>=符号重写。但是,我不断遇到看起来很可怕的类型不匹配错误。有人可以告诉我如何使用>>=符号重写函数吗?或者至少给我一个提示?
我尝试为类实现构造函数Person(请参阅文章末尾的代码)。Codeblocks 给出了第 37 行的错误(即const Date &date)):
error: no matching function for call to 'Date::Date()'
Run Code Online (Sandbox Code Playgroud)
为什么Date要调用构造函数?如何修复错误?
class Date
{
public:
Date(int day, int month, int year);
int GetYear() const;
private:
int Day;
int Month;
int Year;
};
Date::Date(int day, int month, int year){
Day = day;
Month = month;
Year = year;
}
class Person
{
public:
Person(const string &name, const string &address, const Date &date);
string GetAddress() const;
string GetName() const;
private:
string Name; …Run Code Online (Sandbox Code Playgroud)