Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/136.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
c++;返回对象一半的程序 我是C++新手,学习C++。我正在编写一个简单的程序,返回对象的一半。我的代码看起来像 #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator+(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator+(int factor) { double half = amount / factor; return half; } ostream& operator+(ostream& o, TuitionBill& t) { o << t.student << " Tuition: $" << t.amount << endl; return o; } int main() { int a = 47; double b = 39.25; TuitionBill tb("Smith", 4000.00); cout << "Half of " << a << " is " << half(a) << endl; cout << "Half of " << b << " is " << half(b) << endl; cout << "Half of " << tb << " is " << half(tb) << endl; return 0; } #包括 #包括 使用名称空间std; 样板 双半(整数x) { 双h=x/2; 返回h; } 班级学费单 { friend ostream&operator+(ostream,学费账单); 私人: 弦乐学生; 双倍金额; 公众: 学费账单(字符串,双); 双运算符/(int); }; 学费账单::学费账单(字符串学生,双倍金额) { 学生=学生; 金额=金额; } 双tutionBill::运算符+(整数因子) { 双半=金额/系数; 退一半; } ostream&operator+(ostream&o、TutionBill&t) { o_C++ - Fatal编程技术网

c++;返回对象一半的程序 我是C++新手,学习C++。我正在编写一个简单的程序,返回对象的一半。我的代码看起来像 #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator+(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator+(int factor) { double half = amount / factor; return half; } ostream& operator+(ostream& o, TuitionBill& t) { o << t.student << " Tuition: $" << t.amount << endl; return o; } int main() { int a = 47; double b = 39.25; TuitionBill tb("Smith", 4000.00); cout << "Half of " << a << " is " << half(a) << endl; cout << "Half of " << b << " is " << half(b) << endl; cout << "Half of " << tb << " is " << half(tb) << endl; return 0; } #包括 #包括 使用名称空间std; 样板 双半(整数x) { 双h=x/2; 返回h; } 班级学费单 { friend ostream&operator+(ostream,学费账单); 私人: 弦乐学生; 双倍金额; 公众: 学费账单(字符串,双); 双运算符/(int); }; 学费账单::学费账单(字符串学生,双倍金额) { 学生=学生; 金额=金额; } 双tutionBill::运算符+(整数因子) { 双半=金额/系数; 退一半; } ostream&operator+(ostream&o、TutionBill&t) { o

c++;返回对象一半的程序 我是C++新手,学习C++。我正在编写一个简单的程序,返回对象的一半。我的代码看起来像 #include<iostream> #include<string> using namespace std; template <class T> double half(int x) { double h = x / 2; return h; } class TuitionBill { friend ostream& operator+(ostream, TuitionBill); private: string student; double amount; public: TuitionBill(string, double); double operator/(int); }; TuitionBill::TuitionBill(string student, double amt) { student = student; amount = amt; } double TuitionBill::operator+(int factor) { double half = amount / factor; return half; } ostream& operator+(ostream& o, TuitionBill& t) { o << t.student << " Tuition: $" << t.amount << endl; return o; } int main() { int a = 47; double b = 39.25; TuitionBill tb("Smith", 4000.00); cout << "Half of " << a << " is " << half(a) << endl; cout << "Half of " << b << " is " << half(b) << endl; cout << "Half of " << tb << " is " << half(tb) << endl; return 0; } #包括 #包括 使用名称空间std; 样板 双半(整数x) { 双h=x/2; 返回h; } 班级学费单 { friend ostream&operator+(ostream,学费账单); 私人: 弦乐学生; 双倍金额; 公众: 学费账单(字符串,双); 双运算符/(int); }; 学费账单::学费账单(字符串学生,双倍金额) { 学生=学生; 金额=金额; } 双tutionBill::运算符+(整数因子) { 双半=金额/系数; 退一半; } ostream&operator+(ostream&o、TutionBill&t) { o,c++,C++,你的代码中有很多错误。其中许多看起来像是打字错误,但严重的错误是 未在函数签名中使用模板参数T。这就是half(tb)未编译的原因,因为您的half版本始终需要int 不理解构造函数中的student=student;只是将一个变量分配给它自己。您可以通过在类变量的名称前加前缀this->来确保类变量被分配(或者您可以确保类变量和参数名称与amount和amt不同 您还可以检查赋值(在构造函数中执行的操作)和初始化(在构造函数中应该执行的操作)之间的区别 下面是代码的工作版本 #include&

你的代码中有很多错误。其中许多看起来像是打字错误,但严重的错误是

未在函数签名中使用模板参数
T
。这就是
half(tb)
未编译的原因,因为您的
half
版本始终需要
int

不理解构造函数中的
student=student;
只是将一个变量分配给它自己。您可以通过在类变量的名称前加前缀
this->
来确保类变量被分配(或者您可以确保类变量和参数名称与
amount
amt
不同

您还可以检查赋值(在构造函数中执行的操作)和初始化(在构造函数中应该执行的操作)之间的区别

下面是代码的工作版本

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

template <class T>
double half(T x)
{
    double h = x / 2;
    return h;
}

class TuitionBill
{
    friend ostream& operator<<(ostream&, const TuitionBill&);
private:
    string student;
    double amount;
public:
    TuitionBill(string, double);
    double operator/(int);
};

TuitionBill::TuitionBill(string student, double amt)
{
    this->student = student;
    amount = amt;
}

double TuitionBill::operator/(int factor)
{
    double half = amount / factor;
    return half;
}

ostream& operator<<(ostream& o, const TuitionBill& t)
{
    o << t.student << "  Tuition: $" << t.amount;
    return o;
}

int main()
{
    int a = 47;
    double b = 39.25;
    TuitionBill tb("Smith", 4000.00);
    cout << "Half of " << a << " is " << half(a) << endl;
    cout << "Half of " << b << " is " << half(b) << endl;
    cout << "Half of " << tb << " is " << half(tb) << endl;
    return 0;
}
#包括
#包括
使用名称空间std;
样板
双半(T x)
{
双h=x/2;
返回h;
}
班级学费单
{

朋友OfStand运算符:“什么是错误的”?什么不是你的工作?你期待的结果是什么?你得到的是什么?你的函数模板(不是“模板函数”)从不使用类型<代码> t>代码>。为什么它是模板?(如果你是C++新手,那么实现模板不是一个好的开始的地方。从第一章开始)TypOS:你写了代码>操作符+ <代码>,而不是<代码>运算符/<代码>,还有<代码>操作符+<代码>,而不是< C++ >操作器,在构造函数中,你将参数分配给自己。