c++ - 重载运算符 == C++

标签 c++

我的问题的代码可以位于 http://matthewh.me/Scripts/c++/graphs/ .用户名是guest 密码是guest。这将为您提供对 svn 存储库的读取权限。我这样做是为了在阅读这篇文章时保护您的眼睛。

现在回答我的问题,这是关于在类上重载 == 运算符的问题。我拥有的代码似乎无法正常工作,但根据我对它的解释方式,应该可以(我需要更加谦虚并接受编译器所说的内容)。下面是代码中不起作用的部分,只要是导致它的代码段链。

template<typename S,typename T>
template<typename U,typename V>
bool Graph<S,T>::Node<U,V>::operator== ( Node<S,T> *comp ) {
    cout  << "Searching for";
    comp->print_self( );
    bool ret = false;
    if( this->key == comp->key && this->value == comp->value ) {
        ret = true;
    }
    return ret;
}

这在构建图表时使用,以确保我不会插入重复的节点。节点和图形类可以在存储库的代码中找到。继续是通向调用它的地方的 block 链。

首先是我的司机:

int main( int argc, char **argv ) {
    Graph<int, int> graph;
    pair<int,int> p1(3,4);
    pair<int,int> p2(3,4);

    graph.insert_adjacents( &p1,&p2 );

    graph.print_graph( );

    //graph.print_nodes( );
}

insert 函数(这里有内存泄漏不是问题 atm):

template<typename S,typename T>
void Graph<S,T>::insert_adjacents( pair<S,T> *from, pair<S,T> *to ) {
Node<S,T> *from_node = new Node<S,T>( from->first, from->second );
Node<S,T> *to_node = new Node<S,T>( to->first, to->second );

Node<S,T> *from_tmp = this->contains_already( from_node );
Node<S,T> *to_tmp = this->contains_already( to_node );

if( from_tmp==NULL ) {
    this->nodes.push_back( from_node );
    from_tmp = from_node;
}
if( to_tmp==NULL ) {
    this->nodes.push_back( to_node );
    to_tmp = to_node;
}

Node<S,T> *from_edge = from_tmp->contains_adjacent_node( to_tmp );
Node<S,T> *to_edge = to_tmp->contains_adjacent_node( from_tmp );

if( from_edge==NULL ) {
    from_tmp->insert_adjacent_node( to_node,rand( )%20 );
}
if( to_edge==NULL ) {
    to_tmp->insert_adjacent_node( from_node,rand( )%20 );
}

现在 Graph 和 Node 的 contains 函数是:

template<typename S,typename T>
Graph<S,T>::Node<S,T>* Graph<S,T>::contains_already( Node<S,T> *check ) {
    cout << "Searching for Node ";
    check->print_self( );
    cout << " in node list" << endl;
        typename list<Node<S,T>* >::iterator start = nodes.begin( );
        typename list<Node<S,T>* >::iterator end = nodes.end( );
        while( start != end ) {
                if( *(start) == check ) {
            cout << "Found ";
            (*start)->print_self( );
            cout << endl;
                        return (*start);
                }
                start++;
        }
        return NULL;
}

template<typename S,typename T>
template<typename U,typename V>
Graph<S,T>::Node<S,T>* Graph<S,T>::Node<U,V>::contains_adjacent_node( Node<S,T> *adj ) {
    cout << "Checking for adjacent node ";
    adj->print_self( );
    cout << " next to ";
    this->print_self( );
    cout << endl;
    typename list<Edge*>::iterator start = adj->adjacents.begin( );
    typename list<Edge*>::iterator end = adj->adjacents.end( );
    while( start != end ) {
        if( (*start)->adjacent == adj ) {
            return (*start)->adjacent;
        }
        start++;
    }
    return NULL;
}

两者都使用 == 运算符来比较两个节点是否相同。我是否需要尊重指针才能使这项工作正常进行?

我知道那里有很多知识渊博的人,请帮助年轻的 C++ 开发人员学习这些方法。

谢谢, 马修霍根


更新

我无法抗拒,而且妻子有点慢,所以我决定在我们离开之前尝试修复它。无论如何,更新的两段代码如下,但它们似乎仍然无法正常工作。非常感谢此时的任何帮助。

template<typename S,typename T>
template<typename U,typename V>
bool Graph<S,T>::Node<U,V>::operator== ( Node<S,T> &comp ) const {
        cout  << "Searching for";
        comp.print_self( );
        bool ret = false;
        if( this->key == comp.key && this->value == comp.value ) {
                ret = true;
        }
        return ret;
}

template<typename S,typename T>
template<typename U,typename V>
Graph<S,T>::Node<S,T>* Graph<S,T>::Node<U,V>::contains_adjacent_node( Node<S,T> *adj ) {
        cout << "Checking for adjacent node ";
        adj->print_self( );
        cout << " next to ";
        this->print_self( );
        cout << endl;
        typename list<Edge*>::iterator start = adj->adjacents.begin( );
        typename list<Edge*>::iterator end = adj->adjacents.end( );
        while( start != end ) {
                if( (*(*start)->adjacent) == (*adj) ) {
                        return (*start)->adjacent;
                }
                start++;
        }
        return NULL;
}

template<typename S,typename T>
Graph<S,T>::Node<S,T>* Graph<S,T>::contains_already( Node<S,T> *check ) {
        cout << "Searching for Node ";
        check->print_self( );
        cout << " in node list" << endl;
        typename list<Node<S,T>* >::iterator start = nodes.begin( );
        typename list<Node<S,T>* >::iterator end = nodes.end( );
        while( start != end ) {
                if( (*(*(start))) == (*check) ) {
                        cout << "Found ";
                        (*start)->print_self( );
                        cout << endl;
                        return (*start);
                }
                start++;
        }
        return NULL;
}

最佳答案

当然你应该取消对指针的引用。

在这段代码中:

(*start)->adjacent == adj

(假设 adjacentadj 都是指向您的类对象的指针)您正在比较指针,根本不会调用您的运算符。

顺便说一句:我一直认为你应该pass referenceoperator==,即:它应该是:

bool Graph<S,T>::Node<U,V>::operator== (const Node<S,T> &comp ) const

而不是你拥有的。

关于c++ - 重载运算符 == C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6697750/

相关文章:

c++ - 每次循环迭代都使用相同的随机数

c++ - 空/全链表

c++ - 固定大小常量数组的常量数组作为 C++ 中方法的参数

c++ - visual studio 2010 express 不同项目中的链接文件

php - 将 libmcrypt 编译为 C++ 库

c++ - 类型独立 vector 类成员

c++ - 线程数和每秒帧数有什么关系?

c++ - Linux 上的低延迟串行通信

c++ - 成员初始化列表,无参数指针初始化

c++ - 我的数据结构有误