2016-08-17 83 views
-2

我想传递一个类作为另一个类的构造函数的参数,似乎无论我做什么,虽然我拿出了2个错误:看似无法通过类的构造函数的参数

'ice_type': no appropriate default constructor available

no default constructor exists for class "ice_type"

lexer.h

#pragma once 
#include <string> 
#include "token.h" 

class ice_token; 

class ice_lexer { 
public: 
    std::string source; 
    unsigned int line; 
    unsigned int pos; 
    unsigned int ptr; 
    ice_lexer(std::string file_name); 
    ice_token lexer_next(); 
}; 

token.h

#pragma once 
#include <string> 
#include "lexer.h" 

class ice_lexer; 

enum ice_type_enum { 
    ICE_NONE, 
    ICE_EOF, 
    ICE_MODULE, 
    ICE_IDENT, 
    ICE_FLT, 
    ICE_NUM, 
    ICE_STR, 
    // SYMBOLS 
    ICE_RPARA, 
    ICE_LPARA, 
    ICE_RBRAC, 
    ICE_LBRAC, 
    ICE_RCURL, 
    ICE_LCURL, 
    ICE_PERIOD, 
    ICE_COLON, 
    ICE_SEMIC, 
    ICE_COMMA, 
    // COMPARISON 
    ICE_EQLTO, 
    ICE_NOTEQL, 
    ICE_GRT, 
    ICE_GRTEQL, 
    ICE_LES, 
    ICE_LESEQL, 
    // ASSIGNMENT 
    ICE_EQL, 
    ICE_ADDEQL, 
    ICE_SUBEQL, 
    ICE_MULEQL, 
    ICE_DIVEQL, 
    ICE_CAREQL, 
    // URNARY 
    ICE_URNARYNOT, 
    ICE_URNARYSUB, 
    // BINARY 
    ICE_ADD, 
    ICE_SUB, 
    ICE_MUL, 
    ICE_DIV, 
    ICE_MOD, 
    ICE_CAR, 
    ICE_CAT, 
    // TERNARY 
    ICE_TERNARY, 
    // KEYWORDS 
    ICE_IMPORT, 
    ICE_AS, 
    ICE_WHILE, 
    ICE_FOR, 
    ICE_BREAK, 
    ICE_IF, 
    ICE_RETURN, 
    ICE_TRUE, 
    ICE_FALSE, 
    // TYPES 
    ICE_TYPEDEF, 
    ICE_CLASS, 
    ICE_ENUM, 
    ICE_INT, 
    ICE_FLOAT, 
    ICE_STRING, 
    ICE_BOOLEAN, 
    ICE_VOID, 
    ICE_STRUCT 
}; 

static const std::string ice_type_enum_strings[] = { 
    // ABSTRACT 
    "no type", 
    "end of file", 
    "module", 
    "identifier", 
    "floating", 
    "number", 
    "string", 
    // SYMBOLS 
    "left para", 
    "right para", 
    "left brac", 
    "right brac", 
    "left curl", 
    "right curl", 
    "period", 
    "colon", 
    "semi", 
    "comma", 
    // COMPARISON 
    "eql to", 
    "not eql", 
    "grt", 
    "grt eql", 
    "les", 
    "les eql", 
    // ASSIGNMENT 
    "eql", 
    "add eql", 
    "sub eql", 
    "mul eql", 
    "div eql", 
    "car eql", 
    // URNARY 
    "urnarynot", 
    "urnarysub", 
    // BINARY 
    "add", 
    "sub", 
    "mul", 
    "div", 
    "mod", 
    "car", 
    "cat", 
    // TERNARY 
    "ternary", 
    // KEYWORDS 
    "import", 
    "as", 
    "while", 
    "for", 
    "break", 
    "if", 
    "return", 
    "true", 
    "false", 
    // TYPES 
    "typedef", 
    "class", 
    "enum", 
    "int", 
    "float", 
    "string", 
    "bool", 
    "void", 
    "struct" 
}; 

class ice_type { 
public: 
    std::string string; 
    ice_type_enum type; 
    ice_type(ice_type_enum t); 
}; 

class ice_token { 
public: 
    ice_type type; 
    size_t line; 
    size_t pos; 

    union { 
     std::string str; 
     double flt;  
     size_t num; 
    }; 

    ice_token::ice_token(ice_lexer *const lexer, ice_type_enum t); 
}; 

上在ice_token的构造下面的文件时发生错误,视觉工作室智能感知穿着构造的开口大括号的squigglies。

#include <string> 
#include "token.h" 
#include "lexer.h" 

ice_type::ice_type(ice_type_enum t) { 
    string = ice_type_enum_strings[t]; 
    type = t; 
} 

ice_token::ice_token(ice_lexer *const lexer, ice_type_enum t) { 
    type = ice_type(t); 
    line = lexer->line; 
    pos = lexer->pos; 
    num = 0; 
} 

谢谢,帮助表示感谢!

+0

'ice_lexer *'不是类的类型。 – chris

+4

'ice_token'类不包含成员'type',并且标题中的'ice_token'构造函数不是cpp文件中的构造函数。你应该告诉我们你遇到了什么错误。 – 1201ProgramAlarm

+1

请发布[MCVE](http://stackoverflow.com/help/mcve)。发布提供错误的确切代码非常重要。 –

回答

1

问题很简单。您以Java-esque方式初始化您的成员变量。但是C++不能这样工作。一个构造函数总是在所有基类和成员之前调用之前它甚至达到了它自己的身体。您可以使用member initializer lists指定参数给各个c'tors。

但是,您的构造函数没有,所以编译器首先尝试调用默认构造函数(一个没有参数的构造函数)。所以一个修复会是:

ice_token::ice_token(ice_lexer *const lexer, ice_type_enum t): 
    type(t), line(lexer->line), pos(lexer->pos), num(0) 
{} 
相关问题