x; cout,c++,C++" /> x; cout,c++,C++" />

C++;空洞函数练习错误 我是C++新手,我现在正在学习空虚函数。 我试着用一个空函数写一个数平方的函数。这是我的密码 #include "std_lib_facilities.h" void square(int); int main() { int x = 0; cout << "Please enter a number. It will be squared."; cin >> x; cout << x << 't' << square(x); } void square(int x) { int y = x*x; cout << y; } #包括“std_lib_facilities.h” 空方块(int); int main() { int x=0; cout>x; cout

C++;空洞函数练习错误 我是C++新手,我现在正在学习空虚函数。 我试着用一个空函数写一个数平方的函数。这是我的密码 #include "std_lib_facilities.h" void square(int); int main() { int x = 0; cout << "Please enter a number. It will be squared."; cin >> x; cout << x << 't' << square(x); } void square(int x) { int y = x*x; cout << y; } #包括“std_lib_facilities.h” 空方块(int); int main() { int x=0; cout>x; cout,c++,C++,无效函数不能直接返回值。大多数人会使用非无效函数来实现平方,如下所示: int square(int x) { return x * x; } 我看到您的square函数将平方值写入std::cout本身。这很奇怪,但是如果您确实想这样做,您应该将main函数的最后一行替换为: cout << x << 't'; square(x); coutsquare()无效-即它不返回任何内容 cout我猜更改返回类型会使错误消失。编译器错误是因为没有重载版本的函数,您正在

无效函数不能直接返回值。大多数人会使用非无效函数来实现平方,如下所示:

int square(int x)
{
  return x * x;
}
我看到您的
square
函数将平方值写入
std::cout
本身。这很奇怪,但是如果您确实想这样做,您应该将
main
函数的最后一行替换为:

cout << x << 't';
square(x);
cout
square()
无效-即它不返回任何内容


cout我猜更改返回类型会使错误消失。编译器错误是因为没有重载版本的函数,您正在尝试打印调用函数的结果。
cout << x << 't';
square(x);
int square(int x)
{
    return  x*x;
}