2016-12-06 58 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

相關問題