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

  • Home
  • SEARCH
  • 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 6005453
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:23:58+00:00 2026-05-23T01:23:58+00:00

I am having trouble using a container with my custom class. Every time I

  • 0

I am having trouble using a container with my custom class. Every time I try to make an object and push it, the member variables in the pushed object show up as zero. So, myConnection.value appears 0 instead of its value. I suspect there is something wrong with my copy constructor.

Global Definition

vector<connection> Connections;

The trouble spot

    while ( dataInput.good() )
{
    getline (dataInput,line);
    sscanf(line.c_str(),"%lg %lg %d %d",&k,&order,&final,&initial);
    printf("%lg %lg %d %d\n",k,order,final,initial);//Looks Good
    Connections.push_back(connection(k,order,final,initial));
    printf("%lg %lg %s %d\n",Connections[i].k,Connections[i].order,Connections[i].to,Connections[i].from);//Everything is zero!?
    i++;
}

Connection.cpp

#include "../include/connection.h"
#include <stdio.h>
connection::connection(double kin, double orderin, int fromin,int toin)
{
    k=kin;
    order=orderin;
    from = fromin;
    to=toin;
printf("%d %d %i %i\n",kin,orderin,fromin,toin);

}

connection::~connection()
{
    //dtor
}

connection::connection(const connection &c)
{
    connection(c.k,c.order,c.from,c.to);
}

Connection.h

#ifndef CONNECTION_H
#define CONNECTION_H


class connection
{
public:
    connection(double, double, int,int);
    connection(const connection& c);
    virtual ~connection();
    double k;
    double order;
    //From which mass to which mass
    int from;
    int to;
};

#endif // CONNECTION_H

Output

100 1 1 2
1 2 1 1
1 2 1 450294344
0 0 (null) 0
100 1 2 3
2 3 2 1
2 3 2 450294376
0 0 0 1
0 0 (null) 0
100 1 1 3
1 3 1 1
1 3 1 450294440
0 0 0 1
0 0 0 1
0 0 (null) 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-05-23T01:23:58+00:00Added an answer on May 23, 2026 at 1:23 am

    Your copy-constructor creates a temporary connection instance with the specified values and immediately destroys it, leaving the newly copy-constructed object completely uninitialized. A correct, idiomatic copy-constructor would be:

    connection::connection(const connection& c)
      : k(c.k),
        order(c.order),
        from(c.from),
        to(c.to)
    { }
    

    That said, it’s worth noting that in this case you don’t actually need a copy-constructor at all — because you don’t have any data member allocation/lifetime issues to worry about (read: no raw pointers), the compiler will generate an ideal one for you (the same goes for your destructor). So, the best fix here is simply to get rid of your copy-constructor (and destructor) altogether.


    Unrelated to your real problem, note that you should prefer member-initializer-lists to assignment in constructors. So, this would be preferable to your current constructor:

    connection::connection(double kin, double orderin, int fromin, int toin)
      : k(kin),
        order(orderin),
        from(fromin),
        to(toin)
    {
        printf("%f %f %i %i\n", k, order, from, to);
    }
    

    (Also note that %d is an invalid format-specifier for double.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble using os.utime to correctly set the modification time on the mac
I am having trouble using a private std::map variable from within a class template.
Im having trouble using a .NET COM in vb6, It compiles ok and I
I am using mssql and am having trouble using a subquery. The real query
I am having trouble using the attribute XPath Selector in ElementTree, which I should
I'm having trouble using the flowlayoutPanel in a C# winform application. What I basically
I've been having trouble using cucumber and webrat to test authlogic-openid authentication in a
I am having trouble using EnumSet on the client side. I get this runtime
I'm new to development using Xcode, and am having trouble using the built-in Leaks
I'm having some trouble using constraints correctly. I have three tables, 'item', 'store' and

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.