Visual Studio2013编程“<<”是不是匹配操作数?

Visual Studio 2013 programming "<<" is not matching operands?

我收到一条错误消息,指出操作数“<<”(就在主函数中的 times3(x) 之前)与该行中输出的操作数类型不匹配。我究竟做错了什么?我搜索了与它类似的错误,发现它是一个包含错误,但我认为可以修复它。此外,主函数中的倒计时(秒)未被识别并给我一个错误。这是为什么?使用 void 时,问题不断出现。

'

   #include <iostream>
   #include <string>
   #include <cstdlib>
   #include <limits>
   using namespace std;

   bool die(const string & msg);
   double triple(double x);
   double times9(double x);
   void triple(double & result, double x);
   void times3(double & x);
   void countdown(unsigned seconds);
   bool restore();


   int main(){
       double x;
       cout << "x: " << endl;
       cin >> x;
       cout << "The triple of " << x << " is " << triple(x) << endl;
       cout << "9 times of " << x << " is " << times9(x) << endl;
       cout << "3 times of " << x << " is " << times3(x) << endl;
       unsigned seconds;
       cout << "seconds: " << endl;
       cin >> seconds;
       cout << countdown(seconds) << endl;
   }

   bool die(const string & msg){
       cout << "Fatal error: " << msg << endl;
       exit(EXIT_FAILURE);
   }

   double triple(double x){
       return 3 * x;
   }
   double times9(double x){
    return 3 * triple(x);
   }

   void triple(double & result, double x){
       x = 3 * x;
   }

   void times3(double & x){
       x = triple(x);
   }

   void countdown(unsigned & seconds){
       unsigned count = seconds;
       cin >> seconds || die("input failure");
       for (unsigned i = seconds; i <= size; i--){
           cout << i << endl;
       }
       cout << "Blast off! " << endl;
   }

   bool resotre(){
       cin.clear();
       cin.ignore(numeric_limits<streamsize>::max(), '\n');
       return cin.good();
   }'

times3returns void。尝试:

times3(x);
cout << "3 times of " << x << " is " << x << endl;

或者有 times3() return double 而不是通过引用传递。

double times3(double x);

如之前的回答所述,您需要将函数的 return 类型从 void 更改为您尝试打印的变量的数据类型。

您的代码中的另一个问题是函数 void countdown(unsigned & seconds) 函数的声明和定义是不同的。 您已将其声明为 void countdown(unsigned seconds);,但在定义它时您使用的是 void countdown(unsigned & seconds)。在声明中你声明它接受参数 by value 但在定义中你让它接受参数 by reference.

同样在函数countdownfor loop你写了

for (unsigned i = seconds; i <= 0; i--),这不会打印任何输出,因为您的条件是 i<=0,我认为您尝试输入 i >= 0。 :)