Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8050637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:07:45+00:00 2026-06-05T07:07:45+00:00

I feel this question may be a bit trivial, but I simply cannot wrap

  • 0

I feel this question may be a bit trivial, but I simply cannot wrap my head around it. I currently have a class, Node, which is trying to point to what node occurs before the current node using the pointer prevNode. However I seem unable to access any variables within prevNode.

When running Main.cpp from the following code, it prints the result ‘15340756’. Where am I going wrong? Appologies as Im still a bit new to C++.

Node.h

#include "stdafx.h"

class Node
{
public:
    Node();
    void setPrevNode(Node n);
    Node getPrevNode();
    int i;
private:
    Node *prevNode;
};

Node.cpp

#include "stdafx.h"
#include "Node.h"

Node::Node(){
    i = 0;
}

void Node::setPrevNode(Node n){
    prevNode = &n;
}

Node Node::getPrevNode(){
    return *prevNode;
}

Main.cpp

#include "stdafx.h"
#include "Node.h"


int _tmain(int argc, _TCHAR* argv[])
{
    Node nodes[] = {Node(), Node()};

    nodes[0].i = 1;
    nodes[1].setPrevNode(nodes[0]);
    printf("%i", nodes[1].getPrevNode().i); 
    while(true){

    }
    return 0;
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T07:07:46+00:00Added an answer on June 5, 2026 at 7:07 am
    void setPrevNode(Node n);
    

    Here setPrevNode is declared to take a copy of the node passed as an argument, and point to such node. After the function returns, the pointed to node no longer exist and what you get is undefined behavior.

    What you want is to take the Node either as a reference or a pointer instead:

    void setPrevNode(Node& n)
    {
        prevNode = &n;
    }
    
    void setPrevNode(Node* n)
    {
        prevNode = n;
    }
    

    On the same line, getPrevNode is defined to return a copy of the previous node. You most certainly want to return a reference here instead, although you can also return a pointer:

    Node& getPrevNode()
    {
        return *prevNode;
    }
    
    Node* getPrevNode()
    {
        return prevNode;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This may sound a bit provocative but it actually is a real question. Feel
I feel like this may be a dumb question, but it's late and my
There are a few questions around that circle around this question but I feel
This may be a bit of an odd question, and what I have in
I feel stupid asking this question, but I've been wrestling with it for hours
I feel silly asking this question... Sometimes I have a domain that I want
I feel this is an awfully obtuse question to ask, but strangely, this problem
I feel this is not a very good question to post on SO, but
I know this question has been asked a zillion times, but I still feel
This question may expose my ignorance as a web developer, but that wouldn't exactly

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.