小编Par*_*aju的帖子

当按值发送参数时,为什么默认复制构造函数不调用?

该程序初始化一个类的对象并将其作为参数传递给另一个成员函数。当我发表声明时

Address a2=a1;
Run Code Online (Sandbox Code Playgroud)

它显示没有错误。但是当我将参数作为对象时

Employee(int id, string name, Address address);
Run Code Online (Sandbox Code Playgroud)

并调用它使用

Employee e1 = Employee(101,"Nakul",a2);
Run Code Online (Sandbox Code Playgroud)

它显示以下错误。

#include <iostream>  
using namespace std;  
class Address {  
    public:  
     string addressLine, city, state;    
     Address(string addressLine, string city, string state)    
     {    
        this->addressLine = addressLine;    
        this->city = city;    
        this->state = state;    
     }
    
};  
class Employee    
{    
     private:  
     Address address;  //Employee HAS-A Address   
     public:  
     int id;    
     string name;    
     Employee(int id, string name, Address address)    //Here it is showing an error
     {    
           this->id = id;    
           this->name = name; …
Run Code Online (Sandbox Code Playgroud)

c++ oop constructor

1
推荐指数
1
解决办法
76
查看次数

标签 统计

c++ ×1

constructor ×1

oop ×1