【问题标题】:C++ Mistaking a string for a charC ++将字符串误认为是字符
【发布时间】:2012-03-20 03:45:58
【问题描述】:

我对 C++ 比较陌生,并且在传递字符串时遇到了问题。我有 2 个构造函数用于一个类,Transaction。”一个构造函数接受一个字符串和一个 double 作为其参数,而另一个只接受一个字符串。

当我尝试通过下面的行时,我得到一个错误,说:

no matching function for call to 'Account::addTransaction(const char [14])'

no matching function for call to 'Account::addTransaction(const char [11], double&)'

我知道没有匹配的函数,因为我正在传递一个字符串!这是我要传递的内容:

bank.getAccount(index).addTransaction("Close Account");
bank.getAccount(index).addTransaction("Withdrawal", amount_to_withdraw);

我不知道如何更明确地表明第一个参数是一个字符串。任何建议将不胜感激。

谢谢, 亚当

根据@g24l 的请求进行更新:

这里是事务类:

#ifndef TRANSACTION_H
#define TRANSACTION_H

#include <iostream>
#include <string>
using namespace std;

class Transaction {

private:
    string transType;
    double transAmount;

/*
 public constructors:
 * 1st constructor is the default constructor
 * 2nd constructor is for non-monetary transactions
 * 3rd constructor is for transactions involving money
 */
public:
    Transaction() {
        transType = "";
    }
    Transaction(string tType) {
        transType = tType;
    }

    Transaction(string tType, double tAmount) {
        transType = tType;
        transAmount = tAmount;
    }

    void setTransType(string);
    void setTransAmount(double);

    string getTransType() const;
    double getTransAmount() const;
};
#endif  /* TRANSACTION_H */

在为事务数组使用动态内存分配的 Account 类中,我有:

class Account{
private:
    Depositor depositor;
    int accountNum;
    string accountType;
    double accountBalance;
    string accountStatus;
    Transaction *transptr;
    int numTransactions; //number of transactions

public:
    // public member functions prototypes

    // Constructors

    /* Account default constructor:
     * Input:
     *  Depositor() - calls the default depositor constructor
     * Process:
     *  sets object's data members to default values
     * Output:
     *  object's data members are set
     */

    Account()
    {
        //cout << "Account default constructor is running" << endl;
        Depositor();
        accountNum = 0;
        accountType = "";
        accountBalance = 0.0;
        accountStatus = "open";
        transptr = new Transaction[100];
        numTransactions = 0;
    }

我想知道,当我声明事务数组时,它是否使用默认构造函数参数填充所有事务。当我“添加”一个事务时,我实际上是在重写现有事务。

【问题讨论】:

  • 也许我错了,但可以将“...”解释为 char* 而不是字符串吗?
  • 你的类定义在哪里???
  • 您说您有两个 Transaction 类的构造函数,但您没有提供它们的声明。此外,您不是直接构造 Transaction 对象;您正在调用一些单独的 addTransaction 方法。是不是也超载了?它们是如何声明的?

标签: c++ string parameters character


【解决方案1】:

bank.getAccount.addTransactionTransaction 作为参数。所以传一个:

bank.getAccount(index).addTransaction(Transaction("Close Account"));
bank.getAccount(index).addTransaction(Transaction("Withdrawal", amount_to_withdraw));

【讨论】:

  • 这就是诀窍!谢谢@Mankarse!也感谢所有其他回答者。我学到了很多。
【解决方案2】:

只需将其包装在 std::string 中

bank.getAccount(index).addTransaction(std::string("Close Account"));

【讨论】:

  • 谢谢@lmmilewski。现在我越来越近了!但是,由于某种原因,在 2 参数函数上,它现在说:没有匹配函数调用 'Account::addTransaction(std::string, double&)' 因为我是 C++ 新手,' 是什么意思双倍的&'?它认为我在传递一个地址吗?
  • double& 是一种类型 - 对 double 类型变量的引用。删除'&',你会没事的。您可能会收到错误(我猜这里),因为您调用了类似 bank.getAccount(index).addTransaction(std::string("Withdrawal"), 5.0); 的函数- 5.0 是右值,你不能得到它的地址,因此你得到了错误。从 double& 中删除 '&' 或使用如下变量:double x = 5.0; bank.getAccount(index).addTransaction(std::string("Withdrawal"), x);
  • 顺便说一句。这都是猜测 - 如果你显示你的类的定义,那么给出准确的答案会容易得多
  • 谢谢!我刚刚添加了我的类定义。我在任何地方都没有'double&',这就是为什么我不明白它是从哪里得到的,作为构造函数之一。
  • @Adam_G:您调用的是addTransaction,而不是您的构造函数。您需要创建一个Transaction 以传递给addTransaction(请参阅我的答案)。此答案适用于一个参数,因为您的单参数构造函数不是explicit,因此它可用于将std::string 隐式转换为Transaction
【解决方案3】:

不,"Close Account" 指向以 NUL 字符终止的 char 数组的指针 - 这使它成为 C 样式的字符串,但 不是 C++ 字符串。如果您想强制它成为一个字符串,请显式创建一个:

bank.getAccount(index).addTransaction(std::string("Close Account"));

(或添加addTransaction 的变体,也接受char * 作为第一个参数,尽管我自己不建议这样做)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-02-03
    • 1970-01-01
    • 1970-01-01
    • 2020-03-08
    • 1970-01-01
    • 2015-02-18
    • 1970-01-01
    相关资源
    最近更新 更多