-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);
};
}
是什麼讓你覺得這些行在句法上應該是正確的? –