C++ 在C+中传递给构造函数的值错误+;

C++ 在C+中传递给构造函数的值错误+;,c++,C++,我一直在到处寻找,但我找不到解决方案,虽然这可能是一个简单的,因为我刚刚开始。基本上,我试图通过构造函数传入两个值,但是我传入的值在运行或调试时都不正确 Transaction.h #include <string> class Transaction { private: int amount; std::string type; public: Transaction(int amt, std::string kind); std::

我一直在到处寻找,但我找不到解决方案,虽然这可能是一个简单的,因为我刚刚开始。基本上,我试图通过构造函数传入两个值,但是我传入的值在运行或调试时都不正确

Transaction.h

#include <string>

class Transaction {
  private:
    int amount;
    std::string type; 

  public:
    Transaction(int amt, std::string kind);
    std::string Report() const;

    // ...irrelevant code...
};
参数。cpp

#include "Transaction.h"
using namespace std;

Transaction::Transaction(int amt, std::string kind) { };

string Transaction::Report() const {
  string report;
  report += " ";
  report += type;  // supposed to concat "Deposit" to string
  report += " ";
  report += to_string(amount);  // supposed to concat amount to string

  return report;
  // This doesn't return the word "Deposit", nor does
  // it return the correct amount. I don't think that
  // this is adding "Deposit" to 50, because the values
  // change every time I run the program.
}
#include "Transaction.h"
#include <iostream>
using namespace std;

// ...irrelevant code...

int main() {

  int money = 50;
  cout << "Depositing $" << money << endl;

  Transaction deposit(money, "Deposit");  
  // For some reason, this doesn't pass in int money.

  cout << "Original: " << deposit.Report() << endl;
  // And this cout prints some large value instead of 50.

  // ...irrelevant code...
}
任何能帮我指出正确方向(或只是一个直截了当的答案)的人都会很棒

值正在传递给构造函数,好的。问题是你没有对他们做任何事

查看构造函数的实现:

Transaction::Transaction(int amt, std::string kind) { };
这没什么用。特别是,它不会保存(存储)传递的参数值

你可能想要这个:

Transaction::Transaction(int amt, std::string kind)
   : amount(amt)
   , type(kind)
{ }
这种奇怪的冒号语法称为a,它的作用与听起来的一样

请注意,您应该能够在调试器中看到这一点。您要做的是在构造函数的定义上设置一个断点,然后检查参数的值是什么。您将看到它们被正确地传递(并且值没有“错误”)。然后你必须弄清楚它们为什么没有被保存,你可以很容易地看到这一点,从那一点开始单步遍历代码。

值正在传递给构造函数,好吗。问题是你没有对他们做任何事

查看构造函数的实现:

Transaction::Transaction(int amt, std::string kind) { };
这没什么用。特别是,它不会保存(存储)传递的参数值

你可能想要这个:

Transaction::Transaction(int amt, std::string kind)
   : amount(amt)
   , type(kind)
{ }
这种奇怪的冒号语法称为a,它的作用与听起来的一样


请注意,您应该能够在调试器中看到这一点。您要做的是在构造函数的定义上设置一个断点,然后检查参数的值是什么。您将看到它们被正确地传递(并且值没有“错误”)。然后,您必须找出它们没有被保存的原因,并且您可以很容易地看到这一点,从那时起,通过单步遍历代码。

您不需要在类变量中传递参数值的副本

以下是正确的构造函数代码:

Transaction::Transaction(int amt, std::string kind) { 
      this->amount=amt; //copy the value of amt to Transaction.amount
      this->type=kind;  //copy the value of kind to Transaction.kind

};

您不会将类变量中参数的值传递给副本

以下是正确的构造函数代码:

Transaction::Transaction(int amt, std::string kind) { 
      this->amount=amt; //copy the value of amt to Transaction.amount
      this->type=kind;  //copy the value of kind to Transaction.kind

};

为什么要使用此?这里没有必要。这是我的习惯,因为我使用相同的名称调用变量,因此我使用构造函数体中的this.initialization指定类变量的类?令人反感-1为什么?对不起,我不明白原因,因为在构造函数中初始化cariable是错误的。请向我解释为什么不需要使用此。这是我的习惯,因为我使用相同的名称调用变量,因此我使用构造函数体中的this.initialization指定类变量的类?令人反感-1为什么?对不起,我不明白原因,因为在构造函数中初始化cariable是错误的。请向我解释你太棒了。谢谢你!作为跟进,我的程序是从哪里获取号码的?内存中有什么?取决于初始化的类型。在构造对象时,非基元类型的成员变量将调用其默认构造函数,除非进行了初始化。在
std::string
的情况下,这将生成空字符串。对于
int
原语类型,该值未定义,因此编译器可以在该位置放置它喜欢的任何值。感谢您的澄清!你太棒了。谢谢你!作为跟进,我的程序是从哪里获取号码的?内存中有什么?取决于初始化的类型。在构造对象时,非基元类型的成员变量将调用其默认构造函数,除非进行了初始化。在
std::string
的情况下,这将生成空字符串。对于
int
原语类型,该值未定义,因此编译器可以在该位置放置它喜欢的任何值。感谢您的澄清!