【问题标题】:Unvalid use of non-static data member非静态数据成员的使用无效
【发布时间】:2014-01-22 16:52:25
【问题描述】:

您好,我在理解编译器对这段 sn-p 代码的抱怨时遇到了一些问题。

template<typename T> struct sEventColl {
T __d__;
list<string>  event_list;


sEventColl(T d, const string& eventStr) :
        __d__(d) {
    event_list.push_back(eventStr);
}

template<typename U>
sEventColl(const sEventColl<U>& other) {
    __d__ = other.__d__;

     this->event_list.clear();

    for (typename list<string>::const_iterator it =
            other.event_list.crbegin(); it != other.event_list.crend();
            it++) {
        event_list.push_back(*it);
    }
}

 ~sEventColl(){}

bool operator<=(const T & d) const {
    return ((d == __d__) || (__d__ < d));
}

bool operator>=(const T & d) const {
    return ((__d__ == d) || (__d__ > (d)));
}

bool removeEvent(string eventText) {

    for (typename list<string>::iterator it = event_list.begin();
            it != event_list.end(); it++) {
        if (*it == eventText) {
            event_list.erase(it);
            return true;
        }
    }
    return false;
}

bool addEvent(const string event) {

    /* Check if object already exist */
    for (typename list<string>::const_iterator it = event_list.begin();
            it != event_list.end(); it++) {
        if (event == *it)
            return false;
    }
    event_list.push_back(event);
    return true;
}

bool isEmpty() {
    return event_list.empty();
}

friend ostream& operator<<(ostream & os, const sEventColl & ecoll) {
    for (typename list<string>::const_iterator it = ecoll.cbegin();
            it != event_list.cend(); it++) {
        os << __d__ << " : " << *it << endl;
    }
    return os;
}

};

编译器的输出:

calendar.h: 在函数'std::ostream& lab2::operatord' 的使用无效 calendar.h:111:10:错误:来自这个位置

【问题讨论】:

标签: c++


【解决方案1】:

解决了..

friend ostream& operator<<(ostream & os, const sEventColl & ecoll) {
for (typename list<string>::const_iterator it = ecoll.cbegin();
        it != event_list.cend(); it++) {
    os << __d__ << " : " << *it << endl;
}
return os;

}

我应该通过 ecoll 引用访问成员!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-12
    • 2023-03-12
    • 2013-10-14
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多