2016-12-06 83 views
-1

您能帮我解决我的编码问题吗? 我已经标记了三条线,给我同样的错误,问题的标题。我已经包含了该程序的其他大部分代码,以帮助您了解我正在尝试做什么,如果它很混乱,则很抱歉。 是的,代码是未完成的,但我想在完成它之前找出这个问题。错误:在'x'之前预期的初级表达式


#include <iostream> 
#include "fractions.h" 


int main() 
{ 
    cs231::Fraction x{1, 2}; 
    cs231::Fraction y{4, 8}; 

    if (x.equals(y)) 
    { 
     std::cout << "1/2 = 4/8\n"; 
    } 
    else 
    { 
     std::cout << "1/2 != 4/8\n"; 
    } 

    cs231::Fraction z{2,3}; 
    cs231::Fraction w = x.add(z); 
    cs231::Fraction v = x.subtract(z); 
    cs231::Fraction u = x.multiply(z); 
    cs231::Fraction t = x.divide(z); 

    std::cout << "1/2 + 2/3 = " << w.to_string() << "\n"; 
    std::cout << "1/2 - 2/3 = " << v.to_string() << "\n"; 
    std::cout << "1/2 * 2/3 = " << u.to_string() << "\n"; 
    std::cout << "1/2/2/3 = " << y.to_string() << "\n"; */ 

    std::cout << x.to_string() << "\n"; 
    std::cout << y.to_string() << "\n"; 
} 

---------- 

    #include <sstream> 
    #include <string> 
    #include <stdexcept> 
    #include "fractions.h" 

    namespace cs231 
    { 
     //default condtructor 
     Fraction::Fraction() 
     { 
      this->n = 0; 
      this->d = 1; 
     } 

     //regular constructor 
     Fraction::Fraction(int n, int d) 
     { 
      if (d < 1) 
      { 
       throw std::runtime_error{"bad denominator"}; 
      } 

      this->n = n; 
      this->d = d; 
     } 

     std::string Fraction::to_string() 
     { 
      // convert numbers to strings 
      std::stringstream builder; 

      builder << this->n << "/"; 
      builder << this->d; 

      std::string result = builder.str(); 

      return result; 
     } 

      //member functions 
     Fraction add(const Fraction& other) 
     { 
      int d1, x1, z1, n1; 



    /* 1 */  d1= cs231::Fraction x.d * cs231::Fraction z{3}; 
    /* 2 */  x1= cs231::Fraction x(1) * cs231::Fraction z(2); 
    /* 3 */  z1= cs231::Fraction x(2) * cs231::Fraction z(1); 

      n1=x1+z1; 
      return (n1, d1); 

     } 

----------------------------- 

    #pragma once 
    #include <string> 

    namespace cs231 
    { 
     struct Fraction 
     { 
      int n; 
      int d; 

      Fraction(); // default 
      Fraction(int n, int d); 

      // turns to string 
      std::string to_string(); 

      //member variables 
      Fraction add(const Fraction& other); 
      Fraction subtract(const Fraction& other); 
      Fraction multiply(const Fraction& other); 
      Fraction divide(const Fraction& other); 

      // true or false value to check if equal 
      bool equals(const Fraction& other); 
     }; 
    } 
+3

是什么让你觉得这些行在句法上应该是正确的? –

回答

0

由于我没有足够的积分发表评论,所以在这里回答它。

/* 1 */  d= Fraction x.n * cs231::Fraction z{3}; 

你想用x做什么? 举一个例子,你认为下面会有效吗?

d = int x * int y; 

尝试编译它。

+0

没有,这将无法正常工作。我试图使用X的结构中的第二个数字的信息。我已更新代码以尝试使其更清晰。 – spssde

相关问题