首页 > 解决方案 > 指针作为值输出是如何工作的?

问题描述

我想输出如下:

The Stack & the Class Template (in-lab portion)
test nodes: 101(-> / ) 212(-> / )
test next:  101(->212) 212(-> / )

但我的输出将如下所示:

The Stack & the Class Template (in-lab portion)
test nodes: 101 (-> / )  212 (-> / ) 
test next:  101 (->212 (-> / ) )  212 (-> / )

我的标头 IntNode 如下所示:

class IntNode {
public:
    IntNode( int new_value );
    int      get_data( ) const;
    void     set_next( IntNode* new_next );
    IntNode* get_next( ) const;
    void     debug_write( ostream& outfile ) const;

private:
    int      payload;
    IntNode* next = nullptr;
};

ostream& operator<<( ostream& outfile, const IntNode& node );

write函数的实现如下:

void IntNode::debug_write( ostream& outfile ) const{
        outfile << payload;
        if(next == nullptr)
            outfile << "(-> / ) ";  
        else
            outfile << "(->" << *next << ") ";
}

ostream& operator<<( ostream& outfile, const IntNode& node ){
    node.debug_write(outfile);
    return outfile;
}

主要功能如下:

int main(){

    cout << "The Stack & the Class Template (in-lab portion)\n";
    IntNode int_item_1( 101 );
    IntNode int_item_2( 212 );
    cout << "test nodes: " << int_item_1 << ' ' << int_item_2 << "\n";
    int_item_1.set_next( &int_item_2 );
    cout << "test next:  " << int_item_1 << ' ' << int_item_2 << "\n\n";

    return 0;
}

标签: c++

解决方案


您的直接问题是试图在您的函数中传递一个取消引用的next指针(例如*next)作为输出。debug.write()根据您显示的输出,您需要将有效负载传递给下一个节点以进行输出,例如next->payload. 这将提供您显示的输出。

此外,您的代码中必须有using namespace std;某处。请看为什么是“using namespace std;” 被认为是不好的做法?.

您没有显示其余成员函数的实现,因此我们不得不猜测您的类实现类似于:

class IntNode {
  public:
    IntNode (int new_value) { payload = new_value; }
    int      get_data() const { return payload; }
    void     set_next (IntNode* new_next) { this->next = new_next; }
    IntNode *get_next() const { return next; }
    void     debug_write (std::ostream& outfile) const;
    
  private:
    int      payload = 0;
    IntNode *next = nullptr;
};

通过以正确的顺序排列你的函数定义,你可以取消原型的重载<<,例如

std::ostream& operator <<(std::ostream& outfile, const IntNode& node)
{
    node.debug_write (outfile);
    
    return outfile;
}

void IntNode::debug_write (std::ostream& outfile) const {
        outfile << payload;
        if(next == nullptr)
            outfile << "(-> / ) ";  
        else
            outfile << "(->" << next->payload << ") ";
}

然后与main()你显示:

int main (void) {

    std::cout << "The Stack & the Class Template (in-lab portion)\n";
    
    IntNode int_item_1 (101);
    IntNode int_item_2 (212);
    
    std::cout << "test nodes: " << int_item_1 << ' ' << int_item_2 << "\n";
    
    int_item_1.set_next (&int_item_2);
    std::cout << "test next:  " << int_item_1 << ' ' << int_item_2 << "\n\n";
}

示例使用/输出

您会收到您描述想要的输出:

$ ./bin/ll-intnode
The Stack & the Class Template (in-lab portion)
test nodes: 101(-> / )  212(-> / )
test next:  101(->212)  212(-> / )

在重载<<>>类使用时,您还可以选择将重载函数声明为friend允许被重载运算符直接访问受保护成员的函数。在这里,您只需提供重载运算符使用的公共“getter”函数,该函数 100% 没问题。

如果您还有其他问题,请查看并告诉我。


推荐阅读