【发布时间】:2014-12-21 08:46:10
【问题描述】:
我想做的是定义一个结构等于运算符。但这似乎有些不对劲。如何修复此代码?
struct Rectangle
{
public:
double w;
double h;
Rectangle& operator=(int wh)
{
w=wh;
h=wh;
return *this;
}
};
int main()
{
Rectangle rect=5;
return 0;
}
命令:
$ g++ -std=c++11 test.cpp
错误:
test.cpp: In function ‘int main()’:
test.cpp:16:17: error: conversion from ‘int’ to non-scalar type ‘Rectangle’ requested
Rectangle rect=5;
^
【问题讨论】:
标签: c++ struct type-conversion operator-overloading