错误:在"{"标记之前预期"",""或";"和"不能用作函数"
error: expected ',' or ';' before '{' token" and "cannot be used as a function
我正在用C++编写一个从意大利里拉到欧元的转换器:
#include <iostream>
using namespace std;
float x;
int converter (x)
{
y = x/1936,27;
return y;
}
int main()
{
cout << "Give me the value: ";
cin >> x;
converter (x);
}
我试图编译它,但我看到两个错误。第一个是:
lire-euro.cpp:8: error: expected ‘,’ or ‘;’ before ‘{’ token
我在括号前定义了一个函数。为什么我应该把','
或';'
放在'{'
之前?第二个是:
lire-euro.cpp: In function ‘int main()’:
lire-euro.cpp:17: error: ‘converter’ cannot be used as a function
为什么我不能使用converter
作为函数?这与另一个错误有关吗?
您的函数参数列表缺少参数类型:
int converter (float x) { ...
// ^^^^^
除此之外,在函数的主体中,您使用未声明的y
。您可以通过返回表达式来解决此问题,但可能需要将浮点文字中的,
替换为.
,具体取决于您的语言环境。
return x/1936.27;
请注意,返回浮点数可能比返回int
更有意义。
最后,我认为x
没有理由是全球性的。您可以在main()
:中声明
#include <iostream>
int converter(float x)
{
return x/1936.27;
}
int main()
{
float x;
std::cout << "Give me the value: ";
std::cin >> x;
int z = converter(x);
}
函数定义中有两个错误。首先,它的参数x没有类型说明符,它的局部变量y也没有定义。
我想你是指
float x;
int converter()
{
int y = x/1936,27;
return y;
}
虽然我不确定y(和函数返回类型)是否应该定义为int.
函数的相应调用可能看起来像
cout << converter() << endl;
相关文章:
- 递归模板化函数不能分配给具有常量限定类型"const tt &"的变量"state"
- 为什么虚函数不能是静态的和全局的?
- 为什么继承的受保护构造函数不能公开?
- C++为什么类成员函数不能重新声明,但普通函数可以
- 为什么在C++中使用关系运算符创建的模板函数不能对字符串正常工作?
- 成员函数不能为集合迭代器和const_iterator的输入重载(但可以为其他 STL 迭代器重载)
- 为什么模板函数不能作为模板模板参数传递?
- 为什么 free 函数不能在 C 数组上运行,而 std::begin 在某些情况下可以在 C++14 中运行?
- 构造函数不能用于启发性化
- C 错误 - 函数不能超载
- 有没有办法使成员函数不能从构造函数调用
- 为什么模板函数不能是模板类的友元模板函数
- "Framework::Graphics::Material"的默认构造函数不能被引用 - 它是一个已删除的函数
- lower_bound()函数不能在调试模式下编译
- 为什么函数不能正确强制转换指针(从基类到派生类)
- 显式默认函数不能声明为 constexpr,因为隐式声明不是 constexpr
- 琐碎的默认构造函数不能是 constexpr?
- 函数不能嵌套,那么如何在C++中执行此操作
- 错误:成员函数不能在其类之外声明
- 错误-仅返回类型不同的函数不能重载.c++