C++ c++;抽象基类与继承

C++ c++;抽象基类与继承,c++,inheritance,pointers,abstract-class,C++,Inheritance,Pointers,Abstract Class,我试图让这个程序编译,但我得到了各种奇怪的错误 它说我正在创建一个抽象基类的实例。但我相信这只是说,因为当我声明派生类时,它试图创建基类的一个抽象实例 我也得到了一些方法的多重声明 非常感谢你的帮助。我太迷路了 我之所以把所有内容都包括在内,是因为我真的不知道是什么导致了这些问题。如果您想具体查看我在哪些文件中获取错误,则错误MSG位于底部 //main.cpp #include <iostream> #include "Polygon.h" using namespace std;

我试图让这个程序编译,但我得到了各种奇怪的错误 它说我正在创建一个抽象基类的实例。但我相信这只是说,因为当我声明派生类时,它试图创建基类的一个抽象实例

我也得到了一些方法的多重声明

非常感谢你的帮助。我太迷路了

我之所以把所有内容都包括在内,是因为我真的不知道是什么导致了这些问题。如果您想具体查看我在哪些文件中获取错误,则错误MSG位于底部

//main.cpp
#include <iostream>
#include "Polygon.h"
using namespace std;
 int main() {
    Polygon rect("Rectangle", 4);
    rect(0) = Point(1, 1);
    rect(1) = Point(2, 1);
    rect(2) = Point(2, 4);
    rect(3) = Point(1, 4);

    cout << rect << endl;

    return 0;
 }
//point.cpp
#include<ostream>
#include"point.h"
using namespace std;

Point::Point()
{
  set_x(0);
  set_y(0);
}

Point::Point(double varX, double varY)
{
  set_x(varX);
  set_y(varY);
}

void Point::set_x(double var)
{
  x=var;
}

void Point::set_y(double var)
{
  y=var;
}

ostream& Point::display(ostream& os) const
{
 os << '(' << x << ',' << y << ')' <<endl;
 return os;
}

friend ostream& operator<<(ostream& os,Point& a)
{
  a.display(os);
}

double Point::xget()
{
 return x;
}
double Point::yget()
{
  return y;
}

Point& operator+(Point& b)
{
  double tempX=x()+b.x();
  double tempY=y()+b.y();
  Point c(tempX,tempY);
  return *c;
}

Point& operator/(Point& o,int value)
{
  double tempX=o.x()/value;
  double tempY=o.y()/value;
  Point c(tempX,tempY);
  return *c;
}
//point.h
#ifndef _point_h
#define _point_h
class Point : public Showable
{
double x;
double y;
public:
Point();
Point(double,double);
ostream& display(ostream&) const;
double xget();
double yget();
void set_x(double);
void set_y(double);
friend ostream& operator<<(ostream&,Point&);
};

Point& operator+(Point&);
Point& operator/(Point&,int);
#endif
//polygon.cpp
//abstract Base Classes
#include <iostream>
#include "Polygon.h"
#include "point.h"
#include "shape.h"
using namespace std;

Polygon::Polygon(char* str , int V):Shape(str)
{
  points=V;
  Point* vertex= new Point[V];
}

Point& Polygon::operator()(int i)
{
  return vertex[i];
}

int Polygon::pointsNum()
{
 return points;
}

Polygon& Polygon::operator=(Polygon& b)
{
  points=b.pointsNum();
  if(vertex)
  delete[] vertex;

  if(this!=&b && b!=NULL)
  {
    vertex=new Point[b.pointNum()];
    for(int i=0;i<b.pointNum();i++)
    {
      vertex[i]=b.vertex[i];
    }
  }
  else
  {
    vertex=NULL;
  }
  return *this;
}
ostream& Polygon::display(ostream& os) const
{
  os << (Shape)(*this) <<" Centroid " << centroid();
}

friend ostream& operator<<(ostream& os,Polygon& a)
{
  a.display(os);
  return os;
}


Point& Polygon::centroid() const
{
  int tempX=0,tempY=0;
  for(int i=0;i<points;i++)
  {
    tempX+= vertex[i].xget();
  }
  for(int i=0;i<points;i++)
  {
    tempY+= vertex[i].yget();
  }
  Point c(tempX/points,tempY/points);
  return *c;
}
//polygon.h
#ifndef _polygon_h
#define _polygon_h
#include "shape.h"
#include "point.h"

class Polygon : public Shape
{
  int points;
  Point* vertex;
  public:
  Polygon(char* , int);
  Point& operator()(int);
  Polygon& Polygon::operator=(Polygon& b);
  int pointsNum();
  ostream& display(ostream& os) const;
  Point& centroid() const;
  friend ostream& operator<<(ostream&,Polygon&);
};


#endif
//shape.cpp
#include<cstring>
#include<ostream>
#include "shape.h"
using namespace std;
Shape::Shape()
{
  name[0]='\0';
}
Shape::Shape(char* str)
{
  strcpy(name,str);
}

ostream& Shape::display(ostream& os) const
{
  os << name;
  return os;
}

char* Shape::nameget()
{
  return name;
}

Shape& operator=(Shape& a)
{
  strcpy(name,a.nameget());
}
//shape.h
#ifndef _shape_h
#define _shape_h
#include "showable.h"
#include "point.h"
class Shape : public Showable
{
  char name[30+1];
  public:
  Shape();
  Shape(char*);
  ostream& display(ostream&) const;
  char* nameget();
  virtual Point& centroid() const=0;
};
#endif
//showable.cpp
#include<ostream>
#include "showable.h"
using namespace std;

ostream& operator<<(ostream& os,Showable& a)
{
  a.display(os);
}
//showable.h
#ifndef _showable_h
#define _showable_h
class Showable
{
  public:
  virtual ostream& display(ostream&) const=0;

};

ostream& operator<<(ostream& os, Showable&);
#endif
//main.cpp
#include <iostream>
#include "Polygon.h"
using namespace std;
 int main() {
    Polygon rect("Rectangle", 4);
    rect(0) = Point(1, 1);
    rect(1) = Point(2, 1);
    rect(2) = Point(2, 4);
    rect(3) = Point(1, 4);

    cout << rect << endl;

    return 0;
 }
//point.cpp
#include<ostream>
#include"point.h"
using namespace std;

Point::Point()
{
  set_x(0);
  set_y(0);
}

Point::Point(double varX, double varY)
{
  set_x(varX);
  set_y(varY);
}

void Point::set_x(double var)
{
  x=var;
}

void Point::set_y(double var)
{
  y=var;
}

ostream& Point::display(ostream& os) const
{
 os << '(' << x << ',' << y << ')' <<endl;
 return os;
}

friend ostream& operator<<(ostream& os,Point& a)
{
  a.display(os);
}

double Point::xget()
{
 return x;
}
double Point::yget()
{
  return y;
}

Point& operator+(Point& b)
{
  double tempX=x()+b.x();
  double tempY=y()+b.y();
  Point c(tempX,tempY);
  return *c;
}

Point& operator/(Point& o,int value)
{
  double tempX=o.x()/value;
  double tempY=o.y()/value;
  Point c(tempX,tempY);
  return *c;
}
//point.h
#ifndef _point_h
#define _point_h
class Point : public Showable
{
double x;
double y;
public:
Point();
Point(double,double);
ostream& display(ostream&) const;
double xget();
double yget();
void set_x(double);
void set_y(double);
friend ostream& operator<<(ostream&,Point&);
};

Point& operator+(Point&);
Point& operator/(Point&,int);
#endif
//polygon.cpp
//abstract Base Classes
#include <iostream>
#include "Polygon.h"
#include "point.h"
#include "shape.h"
using namespace std;

Polygon::Polygon(char* str , int V):Shape(str)
{
  points=V;
  Point* vertex= new Point[V];
}

Point& Polygon::operator()(int i)
{
  return vertex[i];
}

int Polygon::pointsNum()
{
 return points;
}

Polygon& Polygon::operator=(Polygon& b)
{
  points=b.pointsNum();
  if(vertex)
  delete[] vertex;

  if(this!=&b && b!=NULL)
  {
    vertex=new Point[b.pointNum()];
    for(int i=0;i<b.pointNum();i++)
    {
      vertex[i]=b.vertex[i];
    }
  }
  else
  {
    vertex=NULL;
  }
  return *this;
}
ostream& Polygon::display(ostream& os) const
{
  os << (Shape)(*this) <<" Centroid " << centroid();
}

friend ostream& operator<<(ostream& os,Polygon& a)
{
  a.display(os);
  return os;
}


Point& Polygon::centroid() const
{
  int tempX=0,tempY=0;
  for(int i=0;i<points;i++)
  {
    tempX+= vertex[i].xget();
  }
  for(int i=0;i<points;i++)
  {
    tempY+= vertex[i].yget();
  }
  Point c(tempX/points,tempY/points);
  return *c;
}
//polygon.h
#ifndef _polygon_h
#define _polygon_h
#include "shape.h"
#include "point.h"

class Polygon : public Shape
{
  int points;
  Point* vertex;
  public:
  Polygon(char* , int);
  Point& operator()(int);
  Polygon& Polygon::operator=(Polygon& b);
  int pointsNum();
  ostream& display(ostream& os) const;
  Point& centroid() const;
  friend ostream& operator<<(ostream&,Polygon&);
};


#endif
//shape.cpp
#include<cstring>
#include<ostream>
#include "shape.h"
using namespace std;
Shape::Shape()
{
  name[0]='\0';
}
Shape::Shape(char* str)
{
  strcpy(name,str);
}

ostream& Shape::display(ostream& os) const
{
  os << name;
  return os;
}

char* Shape::nameget()
{
  return name;
}

Shape& operator=(Shape& a)
{
  strcpy(name,a.nameget());
}
//shape.h
#ifndef _shape_h
#define _shape_h
#include "showable.h"
#include "point.h"
class Shape : public Showable
{
  char name[30+1];
  public:
  Shape();
  Shape(char*);
  ostream& display(ostream&) const;
  char* nameget();
  virtual Point& centroid() const=0;
};
#endif
//showable.cpp
#include<ostream>
#include "showable.h"
using namespace std;

ostream& operator<<(ostream& os,Showable& a)
{
  a.display(os);
}
//showable.h
#ifndef _showable_h
#define _showable_h
class Showable
{
  public:
  virtual ostream& display(ostream&) const=0;

};

ostream& operator<<(ostream& os, Showable&);
#endif
//main.cpp

32 main.cpp point.cpp Polygon.cpp shape.cpp showable.cpp
Borland C++ 5.5.1 for Win32 Copyright (c) 1993, 2000 Borland
main.cpp:
Error E2462 showable.h 8: 'virtual' can only be used with non-template memb
er functions
Error E2303 showable.h 8: Type name expected
Error E2139 showable.h 8: Declaration missing ;
Error E2141 showable.h 12: Declaration syntax error
Error E2303 point.h 12: Type name expected
Error E2139 point.h 12: Declaration missing ;
Error E2061 point.h 17: Friends must be functions or classes
Error E2139 point.h 17: Declaration missing ;
Error E2321 point.h 18: Declaration does not specify a tag or an identifier

Error E2303 shape.h 13: Type name expected
Error E2139 shape.h 13: Declaration missing ;
Error E2303 Polygon.h 17: Type name expected
Error E2139 Polygon.h 17: Declaration missing ;
Error E2061 Polygon.h 19: Friends must be functions or classes
Error E2139 Polygon.h 19: Declaration missing ;
Error E2321 Polygon.h 20: Declaration does not specify a tag or an identifi
er
Error E2094 main.cpp 13: 'operator<<' not implemented in type 'std::ostream
' for arguments of type 'Polygon' in function main()
*** 17 errors in Compile ***
point.cpp:
Error E2303 point.h 6: Type name expected
Error E2303 point.h 12: Type name expected
Error E2139 point.h 12: Declaration missing ;
Error E2061 point.h 17: Friends must be functions or classes
Error E2139 point.h 17: Declaration missing ;
Error E2321 point.h 18: Declaration does not specify a tag or an identifier

Error E2316 point.cpp 29: 'display' is not a member of 'Point'
*** 7 errors in Compile ***
Polygon.cpp:
Error E2462 showable.h 8: 'virtual' can only be used with non-template memb
er functions
Error E2303 showable.h 8: Type name expected
Error E2139 showable.h 8: Declaration missing ;
Error E2141 showable.h 12: Declaration syntax error
Error E2303 point.h 12: Type name expected
Error E2139 point.h 12: Declaration missing ;
Error E2061 point.h 17: Friends must be functions or classes
Error E2139 point.h 17: Declaration missing ;
Error E2321 point.h 18: Declaration does not specify a tag or an identifier

Error E2303 shape.h 13: Type name expected
Error E2139 shape.h 13: Declaration missing ;
Error E2303 Polygon.h 17: Type name expected
Error E2139 Polygon.h 17: Declaration missing ;
Error E2061 Polygon.h 19: Friends must be functions or classes
Error E2139 Polygon.h 19: Declaration missing ;
Error E2321 Polygon.h 20: Declaration does not specify a tag or an identifi
er
Warning W8004 Polygon.cpp 14: 'vertex' is assigned a value that is never us
ed in function Polygon::Polygon(char *,int)
Error E2094 Polygon.cpp 32: 'operator!=' not implemented in type 'Polygon'
for arguments of type 'int' in function Polygon::operator =(Polygon &)
Error E2316 Polygon.cpp 34: 'pointNum' is not a member of 'Polygon' in func
tion Polygon::operator =(Polygon &)
Error E2316 Polygon.cpp 35: 'pointNum' is not a member of 'Polygon' in func
tion Polygon::operator =(Polygon &)
Error E2015 Polygon.cpp 46: Ambiguity between 'ostream' and 'std::ostream'
Error E2238 Polygon.cpp 46: Multiple declaration for 'ostream'
Error E2344 showable.h 12: Earlier declaration of 'ostream'
Error E2141 Polygon.cpp 46: Declaration syntax error
*** 23 errors in Compile ***
shape.cpp:
Error E2462 showable.h 8: 'virtual' can only be used with non-template memb
er functions
Error E2303 showable.h 8: Type name expected
Error E2139 showable.h 8: Declaration missing ;
Error E2141 showable.h 12: Declaration syntax error
Error E2303 point.h 12: Type name expected
Error E2139 point.h 12: Declaration missing ;
Error E2061 point.h 17: Friends must be functions or classes
Error E2139 point.h 17: Declaration missing ;
Error E2321 point.h 18: Declaration does not specify a tag or an identifier

Error E2303 shape.h 13: Type name expected
Error E2139 shape.h 13: Declaration missing ;
Error E2015 shape.cpp 16: Ambiguity between 'ostream' and 'std::ostream'
Error E2238 shape.cpp 16: Multiple declaration for 'ostream'
Error E2344 showable.h 12: Earlier declaration of 'ostream'
Error E2141 shape.cpp 16: Declaration syntax error
*** 15 errors in Compile ***
showable.cpp:
Error E2462 showable.h 8: 'virtual' can only be used with non-template memb
er functions
Error E2303 showable.h 8: Type name expected
Error E2139 showable.h 8: Declaration missing ;
Error E2141 showable.h 12: Declaration syntax error
Error E2015 showable.cpp 7: Ambiguity between 'ostream' and 'std::ostream'
Error E2238 showable.cpp 7: Multiple declaration for 'ostream'
Error E2344 showable.h 12: Earlier declaration of 'ostream'
Error E2141 showable.cpp 7: Declaration syntax error
*** 8 errors in Compile ***
//main.cpp
#包括
#包括“Polygon.h”
使用名称空间std;
int main(){
多边形矩形(“矩形”,4);
rect(0)=点(1,1);
rect(1)=点(2,1);
rect(2)=点(2,4);
rect(3)=点(1,4);
你有吗

 virtual Point& centroid()=0;
形状中

 virtual Point& centroid() const;
在多边形中,不同的函数。cv限定符是函数签名的一部分。

此成员函数:

class Shape : public Showable
{
public:
    virtual Point& centroid()=0;
};
不是由
Polygon
实现的
Polygon
具有以下成员函数声明:

class Polygon : public Shape
{
public:
    Point& centroid() const;
};
这是与
Shape::centroid()
(注意
const
)不同的签名,并为
多边形
引入了一个新的成员函数。这意味着
多边形
本身是抽象的,因为它没有为
Shape
中声明的纯
虚拟
函数提供实现

要修复此问题,请从声明和定义
Polygon::centroid()
中删除
const
限定符(并确保返回类型在定义中是
Point&
,因为它当前不是)。

类可显示
{
公众:
虚拟ostream&display(ostream&)const=0;
/**/

ostream&Operator请将示例程序简化为一个完整的最小示例。我希望您可以创建一个20行的单文件示例,让我们更容易理解。有关更多信息,请参阅。您不能使用
double x;
double x();
在同一个类中。@Lpc_dark-如果派生类没有实现所有抽象方法,那么派生类也是抽象的,这就是为什么会出现错误:)@michaelkrein hacker,刚刚输入了这个。谢谢。我现在明白了;-)嗯,我不知道你是否做到了;-)
class Showable
{
  public:
  virtual ostream& display(ostream&) const=0;
  /**/
  ostream& operator<<(ostream& os);
};