C++函数之运算符重载(二)

c++进行函数重载时报错:此运算符函数的参数太多C/C++(344) 问题描述 #c++进行函数重载时报错:此运算符函数的参数太多C/C++(344) 原因分析: C++中类内运算符重载时只能有且仅有一个参数 解决方案: 方案一:将重载函数作为友元函数进行实现 class Person { public: Person(string name, int age) { this->m_name = age; this->m_age = name; } friend ostream &amp 阅读详情

C++函数之运算符重载(二)

七、友元函数的重载

1、在第一个参数需要隐式转换的情形下,使用友元函数重载
    运算符是正确的选择
2、 友元函数没有 this 指针,所需操作数都必须在参数表显式
    声明,很容易实现类型的隐式转换
3、 C++中不能用友元函数重载的运算符有
         =   ()    []    ->

#include<iostream>
using namespace std;
class Complex
{ public:
Complex( double r =0, double i =0 ) { Real = r ; Image = i ; }
Complex(int a) { Real = a ; Image = 0 ; }
void print() const ;
friend Complex operator+ ( const Complex & c1, const Complex & c2 ) ;
friend Complex operator- ( const Complex & c1, const Complex & c2 ) ;
friend Complex operator- ( const Complex & c ) ;
private:
double Real, Image ;
};


Complex operator + ( const Complex & c1, const Complex & c2 )
{ double r = c1.Real + c2.Real ; double i = c1.Image+c2.Image ;
return Complex ( r, i ) ;
}
Complex operator - ( const Complex & c1, const Complex & c2 )
{ double r = c1.Real - c2.Real ; double i = c1.Image - c2.Image ;
return Complex ( r, i ) ;
}
Complex operator- ( const Complex & c )
{ return Complex ( -c.Real, - c.Image ) ; }
void Complex :: print() const
{ cout << '(' << Real << " , " << Image << ')' << endl ; }

八、运算符前置与后置

举例:

#include<iostream>
using namespace std;
class Increase
{ public :
Increase ( ) { value=0; }
void display( ) const { cout<<value<<'\n'; } ;
Increase operator ++ ( ) ; // 前置
Increase operator ++ ( int ) ; // 后置
private: unsigned value ;
};
Increase Increase :: operator ++ ( )
{ value ++ ; return *this ; }
Increase Increase :: operator ++ ( int )
{ Increase temp; temp.value = value ++ ; return temp; }



C++单目运算符的重载与双目运算符的重载【案例】 类和对象是 C++ 的重要特性,它们使得 C++ 成为面向对象的编程语言,因此学习C++的类是掌握面向对象编程的一个非常好的途经。本文主要关于运算符重载的相关知识,单目运算符重载和双目运算符的重载。 阅读详情

相关推荐

Tessy学习系列(三):单元测试——官方例程isValueInRange

注意:工程名称以及路劲不能包含空格和中文

不吃鱼的猫的博客 2348

【C++入门】不能重载为友元函数的4个运算符(=, ->, [ ], ( ))

【C++入门】运算符重载详解》;

weixin_42031299的博客 1861

SolidWorks软件安装管理程序在创建该注册表项时遇到错误: UNKNOWN\Components的解决方案

SolidWorks软件安装管理程序在创建该注册表项时遇到错误: UNKNOWN\Components的解决方案

Q82556499的博客 4169

C++规定有四个运算符 =, ->, [], ()不可以是全局域中的重载(即不能重载为友员函数)理解。

以下是对C++中不能重载为友元函数的四个运算符进行了详细的分析介绍,需要的朋友可以过来参考下   C++规定有四个运算符 =, ->, [], ()不可以是全局域中的重载(即不能重载为友员函数),这是为什么呢? 现在先说说赋值运算符“=”的重载 C++规定赋值运算符“=”只能重载为类的非静态成员函数,而不可以重载为类的友元函数。 不能重载为类的静态成员应该比较容易理解,因为静态成

u014610226的专栏 4141

从ISCXTor2016到CICIDS2017:手把手教你下载和使用UNB的网络安全数据集

本文详细介绍了UNB大学发布的ISCXTor2016和CICIDS2017等网络安全数据集的下载与使用方法,特别适合加密流量识别和网络行为分析研究。文章提供了多种高效获取路径、文件结构解析及实战分析示例,帮助研究者快速上手这些真实场景捕获的精细标注数据集。

weixin_42524945的博客 632

L298N电机驱动模块的接线使用与代码实现

#include "iostm8s208mb.h" #define u8 uint8_t #define u16 uint16_t #define u32 uint32_t typedef unsigned char uint8_t; typedef unsigned short uint16_t; typedef unsigned long uint32_t; // 关于LED灯的宏定义 #define LED2 PI_ODR_ODR2 //输出量 #define LE

Clichong 13万+

流运算符为什么不能重载为成员函数,只能用友元函数重载

一、 为什么operator 如果是重载双目操作符(即为类的成员函数),就只要设置一个参数作为右侧运算量,而左侧运算量就是对象本身。。。。。。 而 >>  或 如果一定要声明为成员函数,只能成为如下的形式: ostream & operator {   return output; } 所以在运用这个 不合符人的习惯。   二、 cout //用重

snowsnowsnow1991的博客 1万+

基于风机和水轮机互补发电系统的Simulink仿真

随着全球对清洁能源的需求不断增长,风力发电和水力发电作为两种重要的可再生能源发电方式,受到了广泛关注。风机和水轮机互补发电系统结合了两者的优势,能够在不同的自然条件下稳定地输出电能,提高能源利用效率和供电可靠性。本文将详细介绍风机和水轮机互补发电的原理,包括风力发电原理、水力发电原理以及两者互补的机制和优势,并通过相关数学公式进行深入分析。水轮机也有其特定的特性曲线,包括流量 - 水头曲线、功率 - 流量曲线等。在不同的水头和流量条件下,水轮机的输出功率会发生变化。

平时工作较为繁忙,私信消息一般晚上回复,谢谢大家~~ 168

有些运行符不能重载为友元函数,它们是:=,(),[]和->。

原因:有人说是因为 C++规定赋值运算符“=”只能重载为类的非静态成员函数,而不可以重载为类的友元函数。 不能重载为类的静态成员应该比较容易理解,因为静态成员函数是属于整个类的,不是属于某个对象的,它只能去操作类静态数据成员。而赋值运算符“=”是基于对象操作的。 当把赋值运算符重载为类的友员函数,在程序中执行类对象的赋值语句时,程序就会出现两种矛盾的选择。 (1)因为它认为

imxiangzi的专栏 4416

基于深度学习的文字识别与检测算法研究

基于深度学习的文字识别与检测算法研究 1. 背景介绍 1.1 文字识别与检测的重要性 在当今信息时代,文字作为人类交流和传递信息的主要载体,无处不在。从日常生活中的路牌、广告牌,到办公场所的文件、合同,再到互联网上的网页、图片等

AI天才研究院 1043

深度学习技巧应用26-CNN中多种注意力机制的嵌入方法,终于搞懂注意力机制了

大家好,我是微学AI,今天给大家介绍一下深度学习技巧应用26-CNN中多种注意力机制的嵌入方法,终于搞懂注意力机制了。CNN是一种能够有效处理图像和其他二维数据的深度学习模型。在传统的CNN中,每个卷积核都会对输入的所有位置进行相同的操作,这可能导致网络无法针对特定区域或特征进行更加关注和重点处理。 注意力机制被引入卷积神经网络中,旨在使网络能够自动地学习和聚焦于输入中的重要区域或特征。通过引入注意力机制,可以增强网络对不同区域或特征的关注度,提取更有意义和相关的特征,从而提高模型的性能和表达能力。

微学AI的博客 3313

C++ 重载运算符和重载函数(二)

您可以重定义或重载大部分 C++ 内置的运算符。这样,您就能使用自定义类型的运算符。 重载的运算符是带有特殊名称的函数,函数名是由关键字 operator 和其后要重载的运算符符号构成的。与其他函数一样,重载运算符有一个返回类型和一个参数列表。 Box operator+(const Box&); 声明加法运算符用于把两个 Box 对象相加,返回最终的 Box 对象。大多数的重载运...

red_ear的博客 359

极智AI | 详解 ViT 算法实现

大家好,我是极智视界,本文详细介绍一下 ViT 算法的设计与实现,包括代码。

极智视界 5604

C++的函数重载篇(二)——运算符的重载

       实际上,C++在设计初就运用了函数的重载,如“+”就可以对整数,浮点数进行运算;“&lt;&lt;”左移运算符可以当作提取运算符使用。下面要介绍的便是运算符的重载。        运算符重载有以下规则:            1、C++不允许用户定义新的运算符,只能对C++已有的运算符进行重载;            2、C++不能重载的运算符只有5个,分别是.(成员运算符)、*(成...

pshdhx的博客 5774

pscad IEEE标准系统搭建说明

pscad IEEE标准系统搭建说明,包含典型的IEEE 9节点、14节点、30节点、39节点和118节点系统。内含标准测试系统的原始数据和潮流结果。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

【C++】类和对象(二)默认成员函数之拷贝构造函数、运算符重载、赋值运算符重载

如果一个构造函数的第一个参数是自身类类型的引用,且任何额外的参数都有默认值,则此构造函数也叫做拷贝构造函数,也就是说拷贝构造是一个特殊的构造函数。拷贝构造函数的作用和构造函数相同,均是将对象初始化化,区别是拷贝构造函数是拷贝一个对象来初始化另一个对象!int _month;int _day;public://全缺省构造函数_day = day;int main()//构造函数aa.print();//拷贝构造。

2302_79531041的博客 1703

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

C++初步之核心编程篇二:函数与运算符重载

C++初步之核心编程篇二:重载 文章目录C++初步之核心编程篇二:重载一、函数重载二、运算符重载1、加、减号2、左、右移运算符3、 前、后自增运算符4、赋值运算符5、关系判断(小于号) 一、函数重载   对于函数来说,重载定义如下:   函数重载是一种特殊情况,C++允许在同一作用域中声明几个类似的同名函数,这些同名函数的形参列表(参数个数,类型,顺序)必须不同,常用来处理实现功能类似数据类型不同的问题。   函数重载的满足条件: 同一个作用域下 函数名相同

qq_51340322的博客 1224

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

【C++】运算符重载 ② ( 类内部定义云算符重载 - 成员函数 | 类外部定义运算符重载 - 全局函数 | 可重载的运算符 )

一、运算符重载本质 二、运算符重载语法 - 类内部定义云算符重载 ( 成员函数 ) 1、运算符重载函数语法说明 2、运算符重载函数调用 3、代码示例 - 运算符重载函数调用 三、运算符重载语法 - 类外部定义运算符重载 ( 全局函数 ) 1、运算符重载函数语法说明 2、运算符重载函数调用 四、可重载的运算符

让 学习 成为一种 习惯 ( 韩曙亮 の 技术博客 ) 939

【c++初阶】类和对象②默认成员函数以及运算符重载初识

默认的拷贝构造函数对象按内存存储按 字节序完成拷贝,这种拷贝叫做浅拷贝,或者值拷贝。注意:在编译器生成的默认拷贝构造函数中,内置类型是按照字节方式直接拷贝的,而自定 义类型是调用其拷贝构造函数完成拷贝的。

m0_71503225的博客 1216

C++ 进阶理论(二) 重载运算符和重载函数 ,多态

C++ 允许在同一作用域中的某个函数和运算符指定多个定义,分别称为函数重载和运算符重载。 函数重载 #include <iostream> using namespace std; class printData { public: void print(int i) { cout << "整数为: " << i << endl; } void print(double f) {

JIAN_ZANG1125的博客 226

C++学习之路—运算符重载(二)运算符重载作为类的成员函数和友元函数

(根据《C++程序设计》(谭浩强)整理,整理者:华科小涛,@http://www.cnblogs.com/hust-ghtao转载请注明) 对运算符重载的函数有两种处理方式:(1)把运算符重载的函数作为类的成员函数;(2)运算符重载的函数不是类的成员函数,在类中把它声明为友元函数。 1 把运算符重载函数作为类的成员函数 在上一篇博客(http://www.cnblog...

weixin_30693183的博客 286

C++运算符重载、友元函数重载

实验要求: 1、设计一个类,用自己的成员函数重载运算符,使对整型的运算符=、+、-、*、/ 适用于分数运算。要求: (1)输出结果是最简分数(可以是带分数); (2)分母为1,只输出分子。#include<iostream> using namespace std; class score{ public: score(int X=0,int Y=0){ x=X;

shuxingcq的博客 4552

C++重载运算符

#include "stdafx.h" using namespace std; class Complax { public: Complax(int a = 0, int b = 0) { this-&gt;a = a; this-&gt;b = b; } void printC() { cout &lt...

Hans的博客 423

C++ —— 运算符重载

C++ —— 运算符重载

MaoSiri的博客 778

C++中有些重载运算符为什么要返回引用

C++

sinat_31608641的博客 1503

C++的运算符重载

C++中预定义的运算符的操作对象只能是基本数据类型。但实际上,对于许多用户自定义类型(例如类),也需要类似的运算操作。这时就必须在C++中重新定义这些运算符,赋予已有运算符新的功能,使它能够用于特定类型执行特定的操作。运算符重载的实质是函数重载,它提供了C++的可扩展性,也是C++最吸引人的特性之一。    运算符重载是通过创建运算符函数实现的,运算符函数定义了重载的运算符将要进行的操作

小蝈蝈的博客 273
上一篇: C++函数之运算符重载(一)
下一篇: C++函数之运算符重载(三)
丘山小菜鸡
博客等级 码龄9年 18粉丝 35原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值