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 6831855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:47:13+00:00 2026-05-26T22:47:13+00:00

I have an (apparently) very simple class initialization, with two constructors, and when I

  • 0

I have an (apparently) very simple class initialization, with two constructors, and when I want to create a new Friendship, the this pointer of the first argument has the appropriate pointer value, but the second doesn’t.

I’ll explain better in code – this is one my constructors:

Friendship::Friendship(const User &u1, const User &u2){
    *user1 = u1;
    *user2 = u2;
}

When I do this in my Test class:

bool Test::insertFriendship(User *user1, User *user2) {
    bool friendshipExists = verifyFriendship(user1, user2);

    if(!friendshipExists) {
        friendships.push_back(new Friendship(*user1, *user2)); // this is a ptr_vector container
        return true;
    }
    return false;
}

It gives me a EXC_BAD_ACCESS error at the push_back line because apparently the ‘this’ pointer of the user1 WORKS when it gets to the constructor and it points to a valid memory address, but when it goes to do the user2, the ‘this’ pointer of user2 points to 0x0 and it fails because that’s basically trying to re-deference NULL when it gets to the operator = overload.

Ex: when it tries to create a new Friendship:

this->user1’s mem address = 0x100100b20
this->user2’s mem address = 0x0 – error!

while

u1’s mem address = 0x7fff5fbff900
u2’s mem address = 0x7fff5fbff898

I mean, these u1 and u2 mem addresses are valid so why the heck doesn’t it work when I try to do:

*user1 = u1; //works cos this is 0x100100b20
*user2 = u2; // fails cos this is 0x0 even though u2 is a valid mem address!

Anyone know why this strange behavior? Even if I switch user1 and user2 in my push_back, it still won’t work at the SECOND one. It’s always that pesky second one that never works.

Ah, this is my Friendship header file, nothing too complex.

#include "User.h"

#ifndef Friendship_h
#define Friendship_h

class Friendship {
    friend class Test;
protected:
    User *user1;
    User *user2;

public:
    Friendship(const User &u1, const User &u2);
    Friendship(const Friendship &a);
    ~Friendship();
};

#endif
  • 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-05-26T22:47:14+00:00Added an answer on May 26, 2026 at 10:47 pm
    Friendship::Friendship(const User &u1, const User &u2)
    {
        *user1 = u1;
        *user2 = u2;
    }
    

    Since user1 and user2 aren’t initialized, you cannot dereference them yet. I think you meant:

    Friendship::Friendship(const User &u1, const User &u2)
    {
        user1 = &u1;
        user2 = &u2;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm still struggling with this concept. I have two different Person objects, very simply:
So, this is a seemingly simple question, but I'm apparently very very dull. I
I have a very simple viewmodel class, and a strongly typed view (which uses
I know this is very simple question, but I'm apparently terrible at coding and
Question: I have a question that is apparently not answered by this already-asked Bash
Paramiko's SFTPClient apparently does not have an exists method. This is my current implementation:
I have this sql: ALTER TABLE dbo.ChannelPlayerSkins DROP CONSTRAINT FK_ChannelPlayerSkins_Channels but apparently, on some
My needs are very simple: I have a Tip table to receive comments and
This isn't a very simple question, but hopefully someone has run across it. I
I'm sure this is something very simple that I'm screwing up, but here goes:

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.