C++ 从基指针复制派生类的构造函数

C++ 从基指针复制派生类的构造函数,c++,pointers,casting,copy-constructor,derived-class,C++,Pointers,Casting,Copy Constructor,Derived Class,我环顾四周,到处都找不到我问题的答案。我试图使用基类指针数组的派生类的复制构造函数。我所学到的唯一一件事是,我可能应该使用dynamic_cast,但无法使其工作 到目前为止,这是我代码的重要部分(因为我有16个不同的文件,所以原版太大了,但这应该足够了) 编辑:以这种方式执行此操作时收到的错误是| 26 |错误:无法将“&properties[0]”(类型为“class Property**”)动态转换为“class Commercial*”(源不是指向类的指针)| 属性_a.cpp文件 #i

我环顾四周,到处都找不到我问题的答案。我试图使用基类指针数组的派生类的复制构造函数。我所学到的唯一一件事是,我可能应该使用dynamic_cast,但无法使其工作

到目前为止,这是我代码的重要部分(因为我有16个不同的文件,所以原版太大了,但这应该足够了)

编辑:以这种方式执行此操作时收到的错误是| 26 |错误:无法将“&properties[0]”(类型为“class Property**”)动态转换为“class Commercial*”(源不是指向类的指针)|

属性_a.cpp文件

#include "property_a.h"

Property::Property(){
   owner = "NULL";
   address = "NULL";
   suburb = "NULL";
   postcode = 0;
}

Property::Property(string theOwner, string theAddress,
                   string theSuburb, int thepostCode):
                     owner(theOwner), address(theAddress),
                     suburb(theSuburb), postcode(thepostCode){}

Property::~Property() {}

Property::Property(const Property& orig) :
                     owner(orig.getOwner()), address(orig.getAddress()),
                     suburb(orig.getSuburb()), postcode(getPostcode()) {}
属性_a.h文件

#ifndef __PROPERTY_A_H__
#define __PROPERTY_A_H__


/*TODO  REQUIRED HEADER FILES AND NAMESPACES*/
#include <string>
#include "utility1.h"

class Property
{
protected:
  string owner;
  string address;
  string suburb;
  int postcode;

public:
  Property();
  Property(string theOwner, string theAddress, string theSuburb, int thepostCode);
  virtual ~Property();
  Property(const Property& orig);
  virtual void input() ;   // Data input for a Property object
  virtual void print() ;  // Data output for a Property object

  string getOwner() const {return owner;};   //Note the use of const
  string getAddress() const {return address;};
  string getSuburb() const {return suburb;};
  int getPostcode() const {return postcode;};

  void setOwner(string newOwner) {owner = newOwner;};
  void setAddress(string newAddress) {address = newAddress;};
  void setSuburb( string  newSuburb) {suburb = newSuburb;};
  void setPostcode(int  newPostcode) {postcode = newPostcode;};
};
#endif
\ifndef\uuu属性\uu A\H__
#定义属性__
/*TODO所需的头文件和名称空间*/
#包括
#包括“效用1.h”
类属性
{
受保护的:
字符串所有者;
字符串地址;
弦郊区;
国际邮政编码;
公众:
属性();
属性(字符串为所有者,字符串为地址,字符串为子城市,整数为邮政编码);
虚拟财产();
财产(固定财产和原始财产);
虚拟void input();//属性对象的数据输入
virtual void print();//属性对象的数据输出
字符串getOwner()const{return owner;};//注意const的用法
字符串getAddress()常量{返回地址;};
字符串get郊区()常量{返回郊区;};
int getPostcode()常量{return postcode;};
void setOwner(字符串newOwner){owner=newOwner;};
void setAddress(字符串newAddress){address=newAddress;};
void setSuburb(字符串newSuburb){suburb=newSuburb;};
void setPostcode(int newPostcode){postcode=newPostcode;};
};
#恩迪夫

我希望这是足够的详细信息

看到错误会很好,但看起来您根本没有调用复制构造函数:

new Commercial(dynamic_cast<Commercial*>(properties[0]));
所以你需要

new Commercial(*dynamic_cast<Commercial*>(properties[0]));
新商业(*dynamic_cast(属性[0]);

很高兴看到错误,但看起来您根本没有调用复制构造函数:

new Commercial(dynamic_cast<Commercial*>(properties[0]));
所以你需要

new Commercial(*dynamic_cast<Commercial*>(properties[0]));
新商业(*dynamic_cast(属性[0]);

很高兴看到错误,但看起来您根本没有调用复制构造函数:

new Commercial(dynamic_cast<Commercial*>(properties[0]));
所以你需要

new Commercial(*dynamic_cast<Commercial*>(properties[0]));
新商业(*dynamic_cast(属性[0]);

很高兴看到错误,但看起来您根本没有调用复制构造函数:

new Commercial(dynamic_cast<Commercial*>(properties[0]));
所以你需要

new Commercial(*dynamic_cast<Commercial*>(properties[0]));
新商业(*dynamic_cast(属性[0]);
属性[2]=新的商业(动态_cast(属性[0]);// 属性[2]=新的商业(动态_cast(属性[0]);// 属性[2]=新的商业(动态_cast(属性[0]);//

属性[2]=新的商业(动态_cast(属性[0]);//抱歉,正在编辑以显示错误。在那个位置添加一个“*”并没有改变allEDIT的错误:我刚刚意识到,当我胡闹的时候,我有了一个新的商业(*dynamic_cast(&properties[0]);删除“&”后,它正在工作!非常感谢,新的它必须是一些小的东西,我根本无法理解。抱歉,编辑以显示错误。在那个位置添加一个“*”并没有改变allEDIT的错误:我刚刚意识到,当我胡闹的时候,我有了一个新的商业(*dynamic_cast(&properties[0]);删除“&”后,它正在工作!非常感谢,新的它必须是一些小的东西,我根本无法理解。抱歉,编辑以显示错误。在那个位置添加一个“*”并没有改变allEDIT的错误:我刚刚意识到,当我胡闹的时候,我有了一个新的商业(*dynamic_cast(&properties[0]);删除“&”后,它正在工作!非常感谢,新的它必须是一些小的东西,我根本无法理解。抱歉,编辑以显示错误。在那个位置添加一个“*”并没有改变allEDIT的错误:我刚刚意识到,当我胡闹的时候,我有了一个新的商业(*dynamic_cast(&properties[0]);删除“&”后,它正在工作!非常感谢,新的它必须是一些小的东西,我根本无法理解。您的
属性
类不需要用户定义的复制构造函数。所有成员都是int和std::strings——它们完全可以使用编译器提供的复制构造函数进行复制。与
Commercial
类一样,不需要用户定义的复制构造函数。出于某种原因,每当使用指针数组时,我都会使用带有-Wall-pedantic-ansi标志的code::blocks相信我,类中不需要这些函数。其次,指针不是对象。复制对象时使用复制操作,而不是指针。当我了解到,如果使用指针数组作为基本对象,则应始终定义自己的复制构造函数,因此我建议您不要在出现错误的代码中调用复制构造函数。请看@James Adkison给出的答案。一旦您修复了错误并实际调用了复制构造函数,您将(神奇地)看到您永远不需要自己编写一个——编译器的版本将被调用。您的
属性
类不需要用户定义的复制构造函数。所有成员都是int和std::strings——它们完全可以使用编译器提供的复制构造函数进行复制。与
Commercial
类一样,不需要用户定义的复制构造函数。出于某种原因,每当使用指针数组时,我都会使用带有-Wall-pedantic-ansi标志的code::blocks相信我,类中不需要这些函数。其次,指针不是对象。复制对象时使用复制操作,而不是指针。当我了解到,如果使用指针数组作为基本对象,则应始终定义自己的复制构造函数,因此我建议您不要在出现错误的代码中调用复制构造函数。请看@James Adkison给出的答案。一旦您修复了错误并实际调用了复制构造函数,您将(神奇地)看到您永远不需要自己编写一个——编译器的