如何在c ++中声明Vector对象?

时间:2016-01-29 22:59:02

标签: c++ vector

我刚开始学习c ++和iam,现在正处于创建包含类对象向量的类的阶段,使用方法添加新对象并将它们全部打印出来。

到目前为止,这是我的代码:

BankAccount.h:

In [25]: import lxml.etree as ET

In [26]: from lxml.etree import XMLParser

In [27]: my_xml = """
   ....:     <documents>
   ....:           <record>Hello< &O >World</record>
   ....:     </documents>
   ....: """

In [28]: parser = XMLParser(recover=True)

In [29]: element = ET.fromstring(my_xml, parser=parser)

In [30]: for text in element.itertext():
   ....:     print(text)
   ....:     


Hello  >World

BankAccount.cpp:

#ifndef BANKACCOUNT_H
#define BANKACCOUNT_H
#include <string>
using namespace std;

class BankAccount
{
    public:
        BankAccount(string C_Name,int C_Balance);
        /*
        void SetCustomerName(string C_Name);
        String GetCustomerName();
        void SetCustomerBalance(int C_Balance);
        int GetCustomerBalance();
        */
        int deposit(int deposit_);
        int withdraw(int withdraw_);
    private:
        string customer_name;
        int customer_balance = 0;
        int Deposit = 0;
        int Withdraw = 0;

};

#endif // BANKACCOUNT_H

Bank.h

BankAccount::BankAccount(string C_Name,int C_Balance)
{
    customer_name = C_Name;
    customer_balance = C_Balance;
}


int BankAccount :: deposit(int deposit_){

        Deposit = deposit_;
        Deposit = Deposit + customer_balance;
        cout << "\nDeposit Balance = " << Deposit;
        customer_balance = Deposit;
        return customer_balance;

    }
int BankAccount :: withdraw(int withdraw_){
        Withdraw = withdraw_;
        Withdraw = customer_balance - Withdraw;
        customer_balance = Withdraw;
        cout<<"After Withdraw Balance is "<<customer_balance;
        return customer_balance;
}

Bank.cpp:

#ifndef BANK_H
#define BANK_H
#include <vector>
#include "BankAccount.h"
using namespace std;


class Bank
{
    public:
        //variables , lists
        vector<BankAccount> newAccount;
        BankAccount bk;

        // constructor
        Bank();

};

#endif // BANK_H

BankAccount类很好,主要问题在Bank类中。

我创建了银行类来创建BankAccount的向量,其方法是添加所有BankAccount并将它们全部打印出来。

但是这个错误一直出现在Bank.cpp的构造函数下:

#include "Bank.h"
#include <iostream>
#include <string>
#include <vector>
using namespace std;

Bank :: Bank()
{
    string Customer_name = " ";
    int Customer_balance = 0;

    cout << "Add name please ";
    cin >> Customer_name ;

    cout << "How much balance?";
    cin >> Customer_balance;

 newAccount.push_back(bk(Customer_name,Customer_balance));


}

似乎每当我试图在BankAccount向量中声明类对象时,错误就会继续发生。有人可以解释一下我做错了什么以及如何解决这个问题?

5 个答案:

答案 0 :(得分:3)

问题是没有var mfDoc = activeDocument; var mflayer = activeDocument.activeLayer; // currently active layer var mfnewdLayer = mfDoc.activeLayer.duplicate(); // Dublicates active layer or group (creating a temp layer) mfDoc.activeLayer = mfnewdLayer; // sets the temp layer as the active layer mfnewdLayer.merge(); // merges it, this leaves only visible layers var mfmlayer = activeDocument.activeLayer; //Grab the currently selected layer if (hasVectorMask() == true){ // Only if it has a layer mask selectVectorMask(); // Select the vector mask rasterizeVectorMask(); // rasterize the vector mask applyLayerMask(); // Apply the layer mask } if (hasLayerMask() == true){ // Only if it has a layer mask selectLayerMask(); // Select the layer mask applyLayerMask(); // Apply the layer mask } if (hasFilterMask() == true){ // Only if it has a Smart Filter mask } var mfheight = mfmlayer.bounds[2]-mfmlayer.bounds[0]; //Grab the H value var mfwidth = mfmlayer.bounds[3]-mfmlayer.bounds[1]; //Grab the W value mfmlayer.remove(); // delete the temp layer mfDoc.activeLayer = mflayer; // gets back to the layer that was active at the begining mflayer.name = mfheight + " x " + mfwidth // set it's name to the dimintions we now have function hasLayerMask() { var hasLayerMask = false; try { var ref = new ActionReference(); var keyUserMaskEnabled = app.charIDToTypeID( 'UsrM' ); ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyUserMaskEnabled ); ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) ); var desc = executeActionGet( ref ); if ( desc.hasKey( keyUserMaskEnabled ) ) { hasLayerMask = true; } }catch(e) { hasLayerMask = false; } return hasLayerMask; } function hasVectorMask() { var hasVectorMask = false; try { var ref = new ActionReference(); var keyVectorMaskEnabled = app.stringIDToTypeID( 'vectorMask' ); var keyKind = app.charIDToTypeID( 'Knd ' ); ref.putEnumerated( app.charIDToTypeID( 'Path' ), app.charIDToTypeID( 'Ordn' ), keyVectorMaskEnabled ); var desc = executeActionGet( ref ); if ( desc.hasKey( keyKind ) ) { var kindValue = desc.getEnumerationValue( keyKind ); if (kindValue == keyVectorMaskEnabled) { hasVectorMask = true; } } }catch(e) { hasVectorMask = false; } return hasVectorMask; } function hasFilterMask() { var hasFilterMask = false; try { var ref = new ActionReference(); var keyFilterMask = app.stringIDToTypeID("hasFilterMask"); ref.putProperty( app.charIDToTypeID( 'Prpr' ), keyFilterMask); ref.putEnumerated( app.charIDToTypeID( 'Lyr ' ), app.charIDToTypeID( 'Ordn' ), app.charIDToTypeID( 'Trgt' ) ); var desc = executeActionGet( ref ); if ( desc.hasKey( keyFilterMask ) && desc.getBoolean( keyFilterMask )) { hasFilterMask = true; } }catch(e) { hasFilterMask = false; } return hasFilterMask; } function selectLayerMask() { try{ var id759 = charIDToTypeID( "slct" ); var desc153 = new ActionDescriptor(); var id760 = charIDToTypeID( "null" ); var ref92 = new ActionReference(); var id761 = charIDToTypeID( "Chnl" ); var id762 = charIDToTypeID( "Chnl" ); var id763 = charIDToTypeID( "Msk " ); ref92.putEnumerated( id761, id762, id763 ); desc153.putReference( id760, ref92 ); var id764 = charIDToTypeID( "MkVs" ); desc153.putBoolean( id764, false ); executeAction( id759, desc153, DialogModes.NO ); }catch(e) { ; // do nothing } } function selectVectorMask() { try{ var id55 = charIDToTypeID( "slct" ); var desc15 = new ActionDescriptor(); var id56 = charIDToTypeID( "null" ); var ref13 = new ActionReference(); var id57 = charIDToTypeID( "Path" ); var id58 = charIDToTypeID( "Path" ); var id59 = stringIDToTypeID( "vectorMask" ); ref13.putEnumerated( id57, id58, id59 ); var id60 = charIDToTypeID( "Lyr " ); var id61 = charIDToTypeID( "Ordn" ); var id62 = charIDToTypeID( "Trgt" ); ref13.putEnumerated( id60, id61, id62 ); desc15.putReference( id56, ref13 ); executeAction( id55, desc15, DialogModes.NO ); }catch(e) { ; // do nothing } } function applyLayerMask() { try{ var id765 = charIDToTypeID( "Dlt " ); var desc154 = new ActionDescriptor(); var id766 = charIDToTypeID( "null" ); var ref93 = new ActionReference(); var id767 = charIDToTypeID( "Chnl" ); var id768 = charIDToTypeID( "Ordn" ); var id769 = charIDToTypeID( "Trgt" ); ref93.putEnumerated( id767, id768, id769 ); desc154.putReference( id766, ref93 ); var id770 = charIDToTypeID( "Aply" ); desc154.putBoolean( id770, true ); executeAction( id765, desc154, DialogModes.NO ); }catch(e) { ; // do nothing } } std::vector。问题是你的BankAccounts类定义了一个数据成员:Bank由于你没有任何显式的构造函数参数,它会尝试使用默认的构造函数BankAccount bk;。没有声明构造函数,因此您收到编译错误。

我怀疑您实际上并不需要BankAccount()数据成员,而应该删除它。

下一个问题是,当您尝试bk调用push_back对象而不是构造对象时。你想要的只是

bk

如果您使用的是newAccount.push_back(BankAccount(Customer_name,Customer_balance)); 或更高版本(您看起来很像),则可以使用C++11代替

emplace_back

这将使您的银行帐户课程如下:

newAccount.emplace_back(Customer_name,Customer_balance);

答案 1 :(得分:0)

您只是忘记实现BankAccount类的构造函数。

BankAccount::BankAccount(string C_Name,int C_Balance)
    : customer_name(C_name)
    , customer_balance(C_Balance)
{
    // TODO;
}

作为旁注,您可能希望输入参数C_nameconst std::string& cName(使用camelCase是连贯的,因此变量绝不应以大写字母开头,它是&#39; s仅用于类和结构。)

通常,我们对私人成员使用variable_(你做了相反的反应)。 另一个提示,变量应该从构造函数的主体中初始化。

编辑:

// This is wrong
newAccount.push_back(bk(Customer_name,Customer_balance));

应该是:

// Construct a valid BankAccount object using it's constructor to fill the vector
newAccount.push_back(BankAccount(Customer_name, Customer_balance));

变量bk没用,需要默认构造函数BankAccount::BankAccount(),因此错误。

答案 2 :(得分:0)

似乎您的BankAccount.h中没有定义标准构造函数BankAccount::BankAccount()

但是当你声明标准构造函数时,你会遇到以下错误

错误1错误LNK2019:未解析的外部符号&#34; public:__ thishisall BankAccount :: BankAccount(void)&#34; (?? 0BankAccount @@ QAE @ XZ)在函数&#34; public中引用:__ thistall Bank :: Bank(void)&#34; (?? 0Bank @@ QAE @ XZ)[...]

表示声明了成员变量Bank::bk但从未定义过。

因此,为了成功编译代码,我建议删除类bk中成员变量Bank的声明。这没有道理。然后在您的银行班中执行以下操作。

Bank::Bank()
{
   string Customer_name = " ";
   int Customer_balance = 0;

   cout << "Add name please ";
   cin >> Customer_name;

   cout << "How much balance?";
   cin >> Customer_balance;

   BankAccount bankAccount(Customer_name, Customer_balance);
   newAccount.push_back(bankAccount);
}

您将成功验证向量newAccount是否填充了新的BankAccount对象。

答案 3 :(得分:0)

您尚未定义默认构造函数。当您使用push_back时,它将尝试默认构造对象,然后复制您传入的对象。您还默认在Bank中构造成员bk。您有2个选项可以解决此问题。

  1. 定义默认构造函数,即。 BankAccount() {
  2. 如果您使用的是c ++ 11,则可以使用向量emplace_back例程。您需要重新设计以从银行中删除bk成员。

答案 4 :(得分:0)

类所包含的所有对象都是在构造函数体之前构造的。在这种情况下,您的Bank对象直接包含BankAccount对象bk,该对象是在{构造函数中打开Bank之前构建的:

Bank :: Bank()
// ... bk is initialized here....
{

尝试稍后在bk的调用中给newAccount.push_back一些参数,但为时已晚; bk对象已初始化。

要控制如何在构造函数中初始化对象,必须在初始化列表中调用其构造函数

Bank :: Bank()
  : bk(Customer_name,Customer_balance)
{

...但当然您尚未定义Customer_nameCustomer_balance

请稍等一下:为什么您在初始化Bank时将帐户推入Bank?为什么此时帐户甚至存在?为什么Bankbk作为会员? (当vector还包含整个 Bank 帐户时,为什么要对这一个帐户进行特殊处理?)您将如何添加?以后会有更多账号吗?

所以,试试这个:首先,默认初始化Bank

Bank :: Bank() {}

...或者,在标题中(如果你正在使用C ++ 11或C ++ 14,你应该这样做):

Bank(void) =default;

然后,添加添加帐户的方法:

Bank::addAcount(void)
{
    // ... use stdin/stdout to get `customer_name` and `customer_balance`...
    newAccount.emplace_back(customer_name,customer_balance);
}

(请注意,如果您不使用C ++ 11或C ++ 14,则需要添加push_back而不是emplace_back。)