错误:转换为非标量类型 [英] Error: Conversion to non-scalar type

查看:335
本文介绍了错误:转换为非标量类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为一个赋值创建了一组派生类。我被指示使用字符数组(c字符串)。当我编译我不断收到错误:

I am making a set of derived classes for an assignment. I am instructed to use char arrays (c-strings). When I compile I keep getting the error:

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

我相当确定我的问题是在我尝试设置实例变量名称为参数发送。这是我的代码与意见,我相信问题可能在哪里。

I am fairly certain that my problem is originating up where I try to set the instance variable Name to the argument sent in. Here is my code with comments in where I believe the problems may be.

#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;
}


推荐答案

code>商业bs1(1,businessName1); 。如果要使用 = ,您还可以使用复制初始化 Business bs2 = Business(2,businessName2);

The proper syntax would be Business bs1(1,businessName1);. If you want to use =, you can also use copy intialization Business bs2 = Business(2,businessName2);.

前者称为直接初始化。他们是不一样的东西,但

The former is known as direct initialization. They aren't exactly the same thing though, see Is there a difference in C++ between copy initialization and direct initialization? for in-depth information.

商务bs1中,在初始化和直接初始化之间的C ++中有区别吗? =(1,businessName1); 1 和数组 businessName1 a href =http://en.wikipedia.org/wiki/Comma_o​​perator>逗号运算符。逗号运算符计算第一个操作数,即 1 并丢弃结果,并返回第二个操作数的值,这是一个数组。换句话说,你的代码相当于 Business bs1 = businessName1; 。这是为什么错误消息说它不能将 char [10] 转换为 Business 对象。

In Business bs1 = (1,businessName1); the 1 and array businessName1 are separated by the comma operator. The comma operator evaluates the first operand, i.e. 1 and throws away the results and returns the value of the second operand, which is an array in your case. In other words, your code is the equivalent of Business bs1 = businessName1;. This is why the error message says it cannot convert a char[10] to a Business object.

这篇关于错误:转换为非标量类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆