【问题标题】:Create a new structure inside a class public method在类公共方法中创建新结构
【发布时间】:2013-10-28 18:59:49
【问题描述】:

您好,我正在尝试在我的班级中创建一个新结构,但我认为某种公共和私有范围存在问题。

typedef struct Currency
{
Currency(Coin *coin, Currency *next, int _position) : _coin(coin), _next(next),  _position(0) {}
Currency() : _next(NULL), _position(0) {}
Coin *_coin;
Currency *_next;
int _position;
};

这是我在班级公共部分中的结构 当我尝试这样做时

if(location <= exit)
{
start = location + 11;
begin = response.find("label", start);
end = begin - start - 3;
findStrings(start, end, s, &response);
curr._next = new Currency();
}

它表示 new Currency() 调用的预期类型说明符。 我有什么遗漏或者不应该以这种方式使用结构吗?

类交换 { 公开:

    typedef struct Badge
    {
        Badge(std::string id, Badge next, Badge prev, int length) : _id(id), _next(&next), _prev(&prev), _position(length) {}
        Badge() : _id(""), _next(NULL), _prev(NULL), _position(0) {}
        std::string _id;
        Badge *_next;
        Badge *_prev;
        int _position;
    };


    typedef struct Currency
    {
        Currency(Coin *coin, Currency *next, int _position) : _coin(coin), _next(next), _position(0) {}
        Currency() : _next(NULL), _position(0) {}
        Coin *_coin;
        Currency *_next;
        int _position;
    };

    /* constructor and destructor */
    Exchange();
    Exchange(std::string str);
    ~Exchange();

    /* Assignment operator */
    Exchange& operator =(const Exchange& copyExchange);
    void parseTradePairs(Currency curr, const std::string response, int begin, int exit);

private:        
    std::string _exch;
    Currency *_currencies;
    Badge *_ident;

};

endif

在类头中的^

Exchange::Exchange()
{
    _exch = "";
}
Exchange::Exchange(std::string str)
{
    _exch = str;
    _ident = new Badge;
    _currencies = new Currency;

    std::string pair;
    std::string response;
    CURL *curl;

    getTradePairs(curl, response);
    int exit = response.find_last_of("marketid");
    parseTradePairs(*_currencies, response, 0, exit);

}
void parseTradePairs(Exchange::Currency curr, std::string response, int begin, int exit)
{
     int start;
     int end;
     string s;
     int location = response.find("marketid", begin);
    if(location <= exit)
    {
        start = location + 11;
        begin = response.find("label", start);
        end = begin - start - 3;
        findStrings(start, end, s, &response);
        curr._next = new Currency();
    }

}

^显然是在cpp类中。

【问题讨论】:

  • 你能在你的类声明上下文中显示它吗?尤其是你提到的那些问题,如果没有看到就很难说出来。
  • 已编辑抱歉。如果您有任何问题,请告诉我
  • 如果您的使用代码在类之外,请将类型更改为Exchange::Currency,这样就可以了。
  • @JayBell 请在提交编辑之前查看预览并修复格式!有人修复了您问题的第一个版本,请参阅如何在添加的部分中执行相同操作。
  • 感谢您的修复,我做了四个空格缩进,不知道为什么它没有那样显示!下次生病通过尝试 CTRL+K 来确保它正常工作!

标签: c++ class inheritance structure


【解决方案1】:

.cpp 中的函数定义与Exchange 类无关。你需要写:

void Exchange::parseTradePairs
         (Exchange::Currency curr, std::string response, int begin, int exit)
{
    // ...
}

另外:在您的类范围之外的任何地方,您都需要使用 Exchange::Currency 来访问该类型。

【讨论】:

    【解决方案2】:

    如果您在类的方法中实例化Currency,那么这应该可以正常工作。 但是,如果您在其他地方实例化 Currency,则需要使用类的名称来确定它的范围。

    即ClassName::Currency

    当然,Currency 需要在您执行此操作的范围内可见,并且将其设置为 public 应该注意这一点。

    【讨论】:

    • 不!这不是主要问题。
    【解决方案3】:

    (注意:这不是对已编辑问题的回答,否决要求删除... :-)

    一个问题是这样的:

    typedef struct Currency {
    };
    

    它可以编译,但 C++ 编译器应该显示类似 warning: 'typedef' was ignored in this declaration 的内容(如果没有,请启用警告!)。您应该使用以下之一:

    struct Currency {
    }; // type 'struct Currency' With implicit typedef in C++
    

    在 C++ 中与以下基本相同:

    typedef struct Currency {
    } Currency; // explicit typedef
    

    或

    typedef struct {
    } Currency; // anonumous struct With typedef type name
    

    通常最好在 C++ 中使用第一种形式。在某些极端情况下可能存在细微差别,因此请保持简单。

    C 不执行隐式 typedef,因此有时使用第二或第三形式以避免需要在任何地方使用 struct 关键字。很高兴知道这一点,因为许多库对于 C 和 C++ 都有相同的 .h 文件。

    【讨论】:

    • 不!这不是主要问题。
    • @g-makulik 是的,在编辑之前写了这个......现在无法测试,没有新类型名称的 typedef 是否编译?
    • 我猜是这样,至少对于 c 合规性来说应该如此。
    • @g-makulik 是的,gcc 只是给出警告。
    猜你喜欢
    • 2013-01-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多