English 中文(简体)
int变量在向量( C++) 中被推回后更改值( C++) 。
原标题:int variables change value after being pushed back in a Vector (C++)

This is the class using the Time class and where the "magic" happens. the data is taken out from a text file

    while(i<flightsNumber){
        if(ist>>nameArr>>arr>>airline>>fare>>time){
            Flight flight(dep,arr,nameArr,airline,fare,time);
            flightVector.push_back(flight);
            //pre-check
            cout<<flight.getTime()<<endl;
        }
        else
            error("Error: programData.dat contains invalid data");  
        //post-check
        cout<<flightVector[i].getTime()<<endl;
        i++;
    }

这是我在《我的时间》课上的课

#include "MyTime.h"

MyTime::MyTime()
    :h(0),m(0){
}

MyTime::MyTime(int hh,int mm)
    :h(hh),m(mm){

    if(hh<0 || mm<0 || mm>59)
        error("Time(): invalid construction");
}

void MyTime::setTime(int hh,int mm){
    if(hh<0 || mm<0 || mm>59)
        error("setTime(): invalid time");
    h=hh;
    m=mm;
}

int MyTime::getHour() const{
    return h;
}

int MyTime::getMinute() const{
    return m;
}


istream& operator>>(istream& is,MyTime& time){
    char ch1;
    int hour,minute;
    is>>hour>>ch1>>minute;
    if(is){
        if(ch1== : ){
            time.h = hour;
            time.m = minute;
        }
        else
            is.setstate(ios_base::failbit);
    }
    else
        is.setstate(ios_base::failbit);
    return is;
}

ostream& operator<<(ostream& os,const MyTime& time){
    return os<<time.h<<":"<<time.m;
}

输出为 :

1:12 1:12

-33686019: -1414812757

这怎么可能呢?

执行回溯() 函数之后的 MyTime 更改实例值 。

问题回答

您正在使用 < code> victor , 带有 < code> Flight 类, 所以, 为了让矢量正确复制 < code> Flight 对象, 它需要一个 < code> operator {/ code > 和一个 < code> 复制构建器 。 您是否在 < code> Flight 类中有这些内容 :

class Flight {
public:
  Flight(const Flight &copy);
  const Flight &operator=(const Flight &);
  virtual ~Flight(); /* Good practice, esp. when using containers */
  // etc
}

因此,请与副本构建器和 操作员确保您复制您的 Time 值。

你唯一可以检查的点是 i 的值: 我假设你投注到 0?

我要在您的 < code>Time 类上演示拷贝构建器和操作员=, 简单简单:

class Time {
protected:
  int h,m;
public:
  Time() { h=m=0; }
  Time(int hour, int minute) : h(hour), m(minute) {}
  Time(const Time &rhs) { operator=(rhs); }
  virtual ~Time() {}
  const Time &operator=(const Time &rhs) {
    h = rhs.h;
    m = rhs.m;
    return rhs;
  }
};

因此,使用此代码, 我可以将 Time 值存储到 victor<Time> 中。 我也可以使用 自然的时间 :

Time a(12,0);
Time b = a;
Time c;
c = b
Time d(a);

vitual devior 表示当 vistor 删去它持有的 Time 实例时,它将使用虚拟毁灭器。这个例子并不特别有用(因为我使毁灭器空着),但也许衍生的类别需要特定的毁灭器。 例如 :

class AtomicTime : public Time {
public:
    AtomicTime() {
      lockNuclearReactor();
    }
    virtual ~AtomicTime() {
      releaseNuclearReactor();
    }
};

如果您有 victor Time 类的 :

vector<Time> times;

你可以愉快地离开:

AtomicTime at;
times.push_back(at);

你不必担心核反应堆,

以上答案的每个注释,假设您的时间和飞行类只包含值或自我管理对象,您应该将实例放入 < code> victor 。 这似乎表明您的矢量指数 < code> 可能有错。 如果您更改行会发生什么情况:

cout<<flightVector[i].getTime()<<endl;

至:

cout << (flightVector.rbegin())->getTime() << endl;

另外, 您能否为您的 < code> Flight 类发布代码?





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签