当我输出以下代码时,它会向控制台显示空白字符串,作为对象的名称和地址。我想知道为什么会这样,而且我发布99%代码的原因是因为有一些地方我认为错误可能是。一切都与其他我不想错过的东西融为一体。我已经尝试了我所知道的一切,但我对C ++很陌生,所以任何帮助都会非常感激。
// Declare the necessary include(s)
#include <iostream>
#include <vector>
#include "Account.h"
void displayAccounts(const vector<Account>);
int main()
{
// Declare local variables
int i = 0;
int size = 3;
// Create a vector of Account objects.
vector <Account> awesomeVector;
// Create three person objects.
Person orc("Lurtz", "Sarumon's Tower");
Person nazgul("Witch King of Angmar", "Palace of Angmar");
Person dragon("Smaug", "746 Misty Mountain Way");
// Create three Account objects, where each account object contains a Person object.
Account lurtz(orc, 1, 500);
Account witchKing(nazgul, 2, 1000);
Account smaug(dragon, 3, 1200);
// Push these Account objects into the vector.
awesomeVector.push_back(lurtz);
awesomeVector.push_back(witchKing);
awesomeVector.push_back(smaug);
// Pass the vector to the displayAccounts function
displayAccounts(awesomeVector);
// Use endl to make the console more readable
cout << endl << endl;
// Keep the console window open with PAUSE
system("PAUSE");
// Return a 0
return 0;
}// End main
// Implementation of displayAccounts function
void displayAccounts(vector<Account> aVector)
{
// Declare local variables
int i = 0;
int size = 3;
// Display intro information
cout << endl;
cout << "Bank of Darkness" << endl <<endl;
cout << "Account\t\tAccount" << endl;
cout << "Number\t\tName\t\tAddress\t\tBalance" <<endl <<endl;
// Create a for loop to display the information stored in each Account object
for (i = 0; i < size; i++)
{
// Display account info
cout << aVector[i].getAccountNum(); cout << "\t\t";
cout << aVector[i].getPerson().getName(); cout << "\t\t";
cout << aVector[i].getPerson().getAddress(); cout << "\t\t$";
cout.precision(2);
cout <<fixed << aVector[i].getAccountBalance() << endl << endl;
}
}// End the displayAccounts function
// Declare necessary include(s)
#pragma once
#include <string>
using namespace std;
// Declare the person class
class Person
{
// Declare private data
private:
string name;
string address;
// Declare public data
public:
Person();
Person(const string, const string);
string getName();
string getAddress();
};// End Person class
// Include the header file for the Person class
#include "Person.h"
Person::Person()
{
}
Person::Person(const string n, const string a)
{
name = n;
address = a;
}
string Person::getName()
{
return name;
}
string Person::getAddress()
{
return address;
}
// Include pragma once
#pragma once
#include "Person.h"
// Declare the Account class
class Account
{
// Declare private data
private:
Person aPerson;
int accountNum;
double accountBalance;
// Declare public data
public:
Account();
Account(const Person, int, double);
Person getPerson();
int getAccountNum();
double getAccountBalance();
};// End Account class
// Include the Account header file
#include "Account.h"
Account::Account()
{
}
Account::Account(const Person p, int accNum, double accBal)
{
aPerson = p;
accountNum = accNum;
accountBalance = accBal;
}
Person Account::getPerson()
{
return Person();
}
int Account::getAccountNum()
{
return accountNum;
}
double Account::getAccountBalance()
{
return accountBalance;
}
答案 0 :(得分:3)
Accout::getPerson()
正在返回一个全新的Person
对象,这就是原因。请改为aPerson
。