【问题标题】:conversion from `double' to non-scalar type requested and some other errors [closed]请求从“双”转换为非标量类型和一些其他错误[关闭]
【发布时间】:2011-10-13 19:44:00
【问题描述】:

请阅读我的代码,并帮助我调试它。因为Dev-C++发现很多错误....

#include<iostream.h>
#include<conio.h>
using namespace std;

struct iWorker{
       double salary;
}

double calSalary(iWorker worker){
    double money = worker.salary;
    return money;
}

int main(){
    iWorker worker;

    cout << "Enter salary: ";
    cin >> worker.salary;

    double salary = calSalary(worker, 0);
    cout << salary;

    getch();
    return 0;
}

和错误:

9: error: new types may not be defined in a return type
9: error: two or more data types in declaration of `calSalary'
 In function `iWorker calSalary(iWorker)':
11: error: conversion from `double' to non-scalar type `iWorker' requested

 In function `int main()':
9: error: too many arguments to function `iWorker calSalary(iWorker)'
20: error: at this point in file
20: error: cannot convert `iWorker' to `double' in initialization

谢谢...

【问题讨论】:

  • &lt;iostream.h&gt; 早于 C++ (stackoverflow.com/questions/214230/…) 的标准化 - 1998 年!任何新代码绝对不应该使用它,任何使用它的书籍都可怕过时了!
  • calSalary 只取一个参数。在调用函数时,为什么要向函数传递一个额外的参数 0?是错字吗?
  • @Mahesh,是的,我想检查函数是如何工作的。 (我删除了它)
  • 请尽快摆脱 Dev-C++。阅读Dev-C++ tag info
  • @R.MartinhoFernandes,好的,我已经在我的 Windows 上安装了 Dev,在 linux 中我有 qt。但我也应该将它安装在 Windows 上。

标签: c++


【解决方案1】:

在声明结构后,您缺少 ;

struct iWorker{
   ...
};  // <- this one here

【讨论】:

  • 哦!天哪!非常感谢!!
【解决方案2】:
struct iWorker{
       int id;
       char name[100];
       float hours;
       double salary;
} <--- missing semicolon here ...

【讨论】:

    【解决方案3】:

    该死的,我来晚了。但你也有可能在这一行缓冲区溢出:

    cin >> worker.name;
    

    你为什么不用std::string

    【讨论】:

      猜你喜欢
      • 2019-01-22
      • 1970-01-01
      • 1970-01-01
      • 2015-12-01
      • 2012-10-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多