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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T07:14:39+00:00 2026-06-04T07:14:39+00:00

My program collapse when I run it. If I comment out line if((str1->compare(*str2))==0 ){…}

  • 0

My program collapse when I run it. If I comment out line if((str1->compare(*str2))==0 ){...} it works OK. I don’t know how to compare two elements of string * which I create and delete after comparing.

main.cpp: In function `int operator==(const Integer&, const Integer&)':
main.cpp:18: warning: taking address of temporary
main.cpp:19: warning: taking address of temporary

Integer.h

class Integer {
public:
    Integer(int val, char *opis):m_val(val),m_opis(opis)
        {
            this->m_val = 0;
            this->m_opis = strdup("0");
        }

    friend int operator==(const Integer&,const Integer&);

      private:
        int m_val;
        char *m_opis;
}

main.cpp

    int operator==(const Integer&a, const Integer&b){
        string *str1 = &string ( a.m_opis );
        string *str2 = &string ( b.m_opis );

        if((str1->compare(*str2))==0 ){return 1 ;} //<- Here is my problem i think.

        delete str1;
        delete str2;

        return 0;
    }
}
//Objects and comparing

    Integer o1(15,"lala");
    Integer o2(150,"lala");
    Integer o3;

    cout<<o1==o2;
  • 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-04T07:14:40+00:00Added an answer on June 4, 2026 at 7:14 am

    The problem is that str1 and str2 are dangling pointers, as the temporary objects to which they point no longer exist by the time str1->compare() is invoked: this is what the compiler is warning about.

    Don’t use dynamically objects here, use stack allocated objects:

    string str1(a.m_opis);
    string str2(b.m_opis);
    

    Other points:

    • prefer to use std::string instead of char* (Integer.m_opis). Related What is The Rule of Three?
    • m_opis is being set twice in the constructor and all instances of Integer will have the same string "0" (same not meaning same buffer but same content). Ditto for m_val.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My program opens a socket on port 80, but if I don't run it
I program Django/Python in emacs, and I would like things like {% comment %}
This program I use has it's own variables to set when you run it,
My program needs to know when a user ejects cd disc. Is there some
I want to make a program that collapse rows when the header is clicked.
Program works fine (with random crashes) and Memory Validator reports Uninitialized read problem in
The program I'm writing is suppose to open two directories and read through the
Program returns an error of, expected PWideChar instead of string procedure TForm1.Button1Click(Sender: TObject); var
Program runs once and it both throws data to the pipe and gets it
The program requires an input of an arbitrary large unsigned integer which is expressed

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.