从向量中输出对象<object*> C++
Outputting the objects out of a vector<object*> c++
我有两个 classes:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
class account
{
private:
double balance = 0;
std::vector <Transactions*> history{};
public:
double get_balance() { return balance; }
void set_balance(double value) { balance = value; }
void toString();
std::vector <Transactions*> get_history() { return history; }
void set_history(Transactions* His) { history.push_back(His); }
};
我正在通过以下方式实现 toString() 方法:
void account::toString() {
std::cout <<" | Current Account | Balance:" << std::to_string(get_balance()) << " | " << std::endl
<< "Transaction History: ";
for (Transactions* transaction : get_history())
{
std::cout << transaction->toString();
}
}
但是我在 for 循环中的“<<”出现错误。我也尝试使用普通的 for 循环,但得出了相同的结论“C++ 没有运算符匹配这些操作数。操作数类型是:std::ostream << void”。我对 c++ 相当陌生,来自 c#/python 背景,其中指针不是一回事,所以我只能假设这就是问题所在?那个或那个向量是空的,但是当程序是运行时它将被填充所以我不确定如何规避这个问题
这也是创建帐户和交易对象的地方:
account* curAccount = new account(std::stoi(parameters[2]), accID++);
accountStore.push_back(curAccount);
std::cout << "account open! The account number is " << accID;
Transactions* openingCTrans = new Transactions("Opening_Balance", std::stoi(parameters[2]));
curAccount->set_history(openingCTrans);
我知道我没有在 class 中显示帐户或交易构造函数,但我认为它们不是问题所在
这个:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
应该是
class Transactions
{
public:
std::string type= "";
double value = 0;
std::string toString(); <<<<<============
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
因为你在做
std::cout << transaction->toString();
即“请打印此函数的输出”,因此它必须 return 可打印的内容
并使用 std::shared_ptr - 你会感谢我
我有两个 classes:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
class account
{
private:
double balance = 0;
std::vector <Transactions*> history{};
public:
double get_balance() { return balance; }
void set_balance(double value) { balance = value; }
void toString();
std::vector <Transactions*> get_history() { return history; }
void set_history(Transactions* His) { history.push_back(His); }
};
我正在通过以下方式实现 toString() 方法:
void account::toString() {
std::cout <<" | Current Account | Balance:" << std::to_string(get_balance()) << " | " << std::endl
<< "Transaction History: ";
for (Transactions* transaction : get_history())
{
std::cout << transaction->toString();
}
}
但是我在 for 循环中的“<<”出现错误。我也尝试使用普通的 for 循环,但得出了相同的结论“C++ 没有运算符匹配这些操作数。操作数类型是:std::ostream << void”。我对 c++ 相当陌生,来自 c#/python 背景,其中指针不是一回事,所以我只能假设这就是问题所在?那个或那个向量是空的,但是当程序是运行时它将被填充所以我不确定如何规避这个问题
这也是创建帐户和交易对象的地方:
account* curAccount = new account(std::stoi(parameters[2]), accID++);
accountStore.push_back(curAccount);
std::cout << "account open! The account number is " << accID;
Transactions* openingCTrans = new Transactions("Opening_Balance", std::stoi(parameters[2]));
curAccount->set_history(openingCTrans);
我知道我没有在 class 中显示帐户或交易构造函数,但我认为它们不是问题所在
这个:
class Transactions
{
public:
std::string type= "";
double value = 0;
void toString();
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
应该是
class Transactions
{
public:
std::string type= "";
double value = 0;
std::string toString(); <<<<<============
Transactions(std::string transT, double transVal) {
type = transT;
value = transVal;
}
};
因为你在做
std::cout << transaction->toString();
即“请打印此函数的输出”,因此它必须 return 可打印的内容
并使用 std::shared_ptr - 你会感谢我