2016-12-06 4 views
-1

コーディングの問題で助けてもらえますか? 私は同じエラー、問題のタイトルを与える3行をマークしました。私はあなたが私が何をしようとしているのかを見るのを助けるために、他のコードのほとんどを含めました。 はい、コードは未完成ですが、この問題を完了する前に把握したいと思います。エラー:「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の構造体の2番目の番号から情報を使用しようとしています。私はそれをより明確にしようとするためにコードを更新しました。 – spssde

関連する問題