gpt4 book ai didi

c++ - 错误 : Conversion to non-scalar type

转载 作者:太空狗 更新时间:2023-10-29 23:52:21 25 4
gpt4 key购买 nike

我正在为一项作业制作一组派生类。我被指示使用 char 数组(c 字符串)。当我编译时,我不断收到错误:

Homework11.cpp: In function âint main()â:
Homework11.cpp:72: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:73: error: conversion from âchar [10]â to non-scalar type âBusinessâ requested
Homework11.cpp:74: error: conversion from âchar [10]â to non-scalar type âAccountâ requested
Homework11.cpp:75: error: conversion from âchar [10]â to non-scalar type âAccountâ requested

我相当确定我的问题出在我尝试将实例变量 Name 设置为发送的参数的地方。这是我的代码,其中包含我认为可能存在问题的注释。

#include <iomanip>
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;

class Person{
public:
Person() {}
Person(char theName[]) {strcpy(name,theName);}
void getName(char theName[]) // I think the problem may be here or in the line above
{ theName = name;}
private:
char name[80];
};

class Account : public Person{
public:
Account() :accountNum(0),balance(0) {}
Account(int actNo, char theName[])
:Person(theName),accountNum(actNo),balance(0) {}
void setBal(float theBalance)
{balance = theBalance;}
void deposit(float numDeposited)
{ balance = balance + numDeposited;}
float withdraw(float numWithdrawn)
{ balance = balance -numWithdrawn;
return numWithdrawn;}
float getBal() {return balance;}
void printBal();
private:
int accountNum;
float balance;
};

class Business : public Account{
public:
Business() : checkFee(0.0) {}
Business(int actNo, char theName[])
: Account(actNo, theName),checkFee(0.0) {}
float withdraw(float numWithdrawn)
{float newBalance = getBal()-numWithdrawn-checkFee;
setBal(newBalance);
return numWithdrawn;}
void setFee(float fee) {checkFee = fee;}
private:
float checkFee;
};

void Account::printBal()
{
char name[80];
getName(name);
cout<<setw(10)<<"Account # "<<accountNum<<setw(10)<<
name<<setw(10)<<balance<<endl;
}


int main()
{
char businessName1[10]="Business1";
char businessName2[10] ="Business2";
char regularName1[10] = "Regular1";
char regularName2[10] = "Regular2";

//The following 4 lines are the ones I am getting the error for
Business bs1 = (1,businessName1);
Business bs2 = (2,businessName2);
Account rg1 = (1, regularName1);
Account rg2 = (2, regularName2);

cout<<"Intially: "<<endl;
rg1.printBal();
rg2.printBal();
bs1.printBal();
bs2.printBal();

bs1.deposit(1000.00);
bs2.deposit(1000.00);
rg1.deposit(1000.00);
rg2.deposit(1000.00);

cout<<"----------------------------------------"<<endl;
cout<<"After adding 1000.00 to all accounts:"<<endl;
rg1.printBal();
rg2.printBal();
bs1.printBal();
bs2.printBal();

bs1.setFee(1.00);
bs1.withdraw(500);
bs2.withdraw(500);
bs1.deposit(250);
bs2.deposit(250);
rg1.withdraw(500);
rg2.deposit(500);

cout<<"---------------------------------------"<<endl;
cout<<"Finially:"<<endl;
rg1.printBal();
rg2.printBal();
bs1.printBal();
bs2.printBal();

return 0;
}

最佳答案

正确的语法是 Business bs1(1,businessName1);。如果要使用=,也可以使用复制初始化Business bs2 = Business(2,businessName2);

前者称为直接初始化。但它们并不完全相同,请参阅 Is there a difference in C++ between copy initialization and direct initialization?获取深入信息。

Business bs1 = (1,businessName1); 中,1 和数组 businessName1comma operator 分隔.逗号运算符计算第一个操作数,即 1 并丢弃结果并返回第二个操作数的值,在您的情况下是一个数组。换句话说,您的代码相当于 Business bs1 = businessName1;。这就是错误消息说它无法将 char[10] 转换为 Business 对象的原因。

关于c++ - 错误 : Conversion to non-scalar type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16179592/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com