C++ I';我收到一个错误,说只有虚拟函数可以标记为覆盖

C++ I';我收到一个错误,说只有虚拟函数可以标记为覆盖,c++,class,c++11,polymorphism,stdvector,C++,Class,C++11,Polymorphism,Stdvector,我经常遇到这样的错误,即只有虚拟函数可以标记为覆盖,但有问题的函数“norm()”和“字符串到字符串()”是虚拟的。这可能是什么原因造成的 在我的主函数中,我也得到了一个错误,没有匹配的成员函数来调用push-back,我是否在某个地方犯了一个错误,我只是没有看到它 #include <iostream> #include <cmath> #include <vector> using namespace std; class Group { public:

我经常遇到这样的错误,即只有虚拟函数可以标记为
覆盖
,但有问题的函数“
norm(
)”和“
字符串到字符串()
”是虚拟的。这可能是什么原因造成的

在我的主函数中,我也得到了一个错误,没有匹配的成员函数来调用push-back,我是否在某个地方犯了一个错误,我只是没有看到它

#include <iostream>
#include <cmath>
#include <vector>
using namespace std;

class Group
{
public:
   virtual string to_string() = 0;
   virtual int norm() = 0;
};

class Real 
{
   // add your code here
protected:
   int number;

public:
   Real(int num)
   {
      number = num;
   }
   int norm() override
   {
      return number;
   }

   string to_string() override 
   {
      return number;
   }

   int getNumber() const
   {
      return number;
   }

   void setNumber(int number)
   {
      Real::number = number;
   }
};

class Complex : public Real 
{
   // add your code here
protected:
   int imaginary;
public:
   Complex(int realNum, int imag) : Real(realNum)
   {}
   int norm() override
   {
      return sqrt(number * number + imaginary * imaginary) + 'i';
   }
   string to_string() override
   {
      return ::to_string(number) + '+' + ::to_string(imaginary) + 'i';
   }
};

class Trinomial : public Complex
{
   // add your code here
protected:
   int third;
public:
   Trinomial(int p1, int p2, int p3) : Complex(p1, p2) {
      third = p3;
   }
   int norm() override {
      return sqrt(number * number + imaginary * imaginary + third * third);
   }
   string to_string() override {
      return ::to_string(number) + "x^2+" + ::to_string(imaginary) + "x+" + ::to_string(third);
   }
};

class Vector : public Group
{
   // add your code here
protected:
   vector<int> v;
public:
   Vector(int num1, int num2, int num3)
   {
      v.push_back(num1);
      v.push_back(num2);
      v.push_back(num3);
   }
   int norm() override
   {
      int squared_sum = 0;
      for (int i = 0; i < v.size(); i++) {
         squared_sum += v[i] * v[i];
      }
      return sqrt(squared_sum);
   }
   string to_string() override
   {
      string str = "[";
      for (int i = 0; i < v.size() - 1; i++) {
         str += ::to_string(v[i]) + " ";
      }
      str += ::to_string(v[v.size() - 1]) + "]";
      return str;
   }
};


int main()
{
   vector<Group*> elements;

   elements.push_back(new Real{ 3 });
   elements.push_back(new Complex{ 3,4 });
   elements.push_back(new Trinomial{ 1,2,3 });
   elements.push_back(new Vector{ 1,2,3 });

   for (auto e : elements) 
   {
      cout << "|" << e->to_string() << "| = " << e->norm() << endl;
   }

   for (auto e : elements)
      delete e;

   return 0;
}
#包括
#包括
#包括
使用名称空间std;
班级
{
公众:
虚拟字符串到_字符串()=0;
虚拟整数范数()=0;
};
类真实
{
//在这里添加您的代码
受保护的:
整数;
公众:
实数(整数)
{
number=num;
}
int norm()重写
{
返回号码;
}
字符串到字符串()覆盖
{
返回号码;
}
int getNumber()常量
{
返回号码;
}
无效集合编号(整数编号)
{
实数::数=数;
}
};
阶级情结:公共真实
{
//在这里添加您的代码
受保护的:
int-想象的;
公众:
复数(int realNum,int imag):实数(realNum)
{}
int norm()重写
{
返回sqrt(数*数+虚*虚)+“i”;
}
字符串到字符串()覆盖
{
return::to_string(number)+'+'+::to_string(virtual)+'i';
}
};
类三项式:公共复合体
{
//在这里添加您的代码
受保护的:
第三名;
公众:
三项式(intp1,intp2,intp3):复数(p1,p2){
第三=p3;
}
int norm()重写{
返回sqrt(数字*数字+假想数*假想数+第三个*第三个);
}
字符串到字符串()覆盖{
return::to_string(数字)+“x^2+”+::to_string(假想)+“x+”+::to_string(第三个);
}
};
类向量:公共组
{
//在这里添加您的代码
受保护的:
向量v;
公众:
向量(整数m1,整数m2,整数m3)
{
v、 推回(num1);
v、 推回(num2);
v、 推回(num3);
}
int norm()重写
{
int平方和=0;
对于(int i=0;icout您的类real没有父类。因此您无法覆盖到\u string()

这里有几个问题:

  • Real
    必须继承自
    ,以便
    覆盖
    函数。这就是错误消息的原因
  • 其次,
    Real::to_字符串必须在末尾返回一个字符串
    可以使用转换整数
  • 最后但并非最不重要的一点是,
    必须有一个用于定义行为的虚拟析构函数。请阅读以下内容:
简而言之,你需要

#include <string>

class Group
{
public:
   // other code
   virtual ~Group() = default;
};

class Real: public Group  // --> inherited from base
 {
   // other codes
public:
   std::string to_string() override {
      return std::to_string(number);
   }

};
#包括
班级
{
公众:
//其他代码
virtual~Group()=默认值;
};
类Real:public Group/-->从基继承
{
//其他代码
公众:
std::string to_string()重写{
返回std::to_字符串(数字);
}
};


另一方面,请不要练习使用

覆盖
也会捕获丢失的继承问题:-)