C++:虚拟无效函数错误 "explicit type is missing (int assumed)"
C++: virtual void functions error "explicit type is missing (int assumed)"
此程序包含一个名为 collection 的 class,其中包含虚拟 void 函数。我在 main 中制作的动态数组应该能够从文件中接受任意数量的整数,每行都有一个文件编号。它允许用户指定他们想要读取的文件名。这是 header 文件:
#ifndef Collection_H
#define Collection_H
class collection{
public:
collection(); //constructor
virtual void add(int value) = 0; //adds the value to the array
virtual void remove(int index) = 0; //removes and item from the appropriate index location
virtual void print() = 0; //prints item of the array comma seperated.
virtual int get(int index) = 0; //gets item at a particular index.
virtual int sum() = 0; //gets the sum of the array
virtual int size() = 0; //gets the size of the array
};
我对这个程序的第一个问题比较概念化:virtual 实际上做了什么,为什么要使用它以及如何实际实现它?我确实知道,因为您必须创建派生的 class 才能实现虚函数。因此,这里是我导出的 class header:
// this is the derived collection
#include "Collection.h"
#ifndef derivedcollection_H
#define derivedcollection_H
class derivedcollection: public collection
{
public:
collection(); //constructor (error is at this line)
virtual void add(int value) = 0; //adds the value to the array
virtual void remove(int index) = 0; //removes and item from the appropriate index location
virtual void print() = 0; //prints item of the array comma seperated.
virtual int get(int index) = 0; //gets item at a particular index.
virtual int sum() = 0; //gets the sum of the array
virtual int size() = 0; //gets the size of the array
};
#endif
我的下一个也是最后一个问题更像是一个我不明白的简单错误。对于 collection(); 所在的行在我的派生 class 派生collection 中声明,我收到一个错误,指出 "explicit type is missing ('int' assumed)"。虽然这通常是一个很容易修复的错误,但老实说,我有点困惑为什么会出现这个错误。当我在我的派生 class header 而不是我的基础 class header?
中声明默认构造函数时,它是如何给我这个错误的?
有大量关于 virtual
方法的文献和 YouTube 视频。关于错误,您的构造函数应该是 derivedcollection()
,而不是 collection()
。
有助于实现动态多态性(OOP 的支柱)的虚函数在网络上的许多教程中都有解释。
其中之一是explained here
virtual void add(int value) = 0;
表示它是一个纯虚函数,可能没有任何定义。派生 class 必须 实现其定义。
请注意,collection
不是派生的 class derivedcollection
.
的构造函数
因此,编译器假定其 return 类型为整数。即它是一种简单的方法。
构造必须是 derivedcollection()
而不是 collection()
。
此程序包含一个名为 collection 的 class,其中包含虚拟 void 函数。我在 main 中制作的动态数组应该能够从文件中接受任意数量的整数,每行都有一个文件编号。它允许用户指定他们想要读取的文件名。这是 header 文件:
#ifndef Collection_H
#define Collection_H
class collection{
public:
collection(); //constructor
virtual void add(int value) = 0; //adds the value to the array
virtual void remove(int index) = 0; //removes and item from the appropriate index location
virtual void print() = 0; //prints item of the array comma seperated.
virtual int get(int index) = 0; //gets item at a particular index.
virtual int sum() = 0; //gets the sum of the array
virtual int size() = 0; //gets the size of the array
};
我对这个程序的第一个问题比较概念化:virtual 实际上做了什么,为什么要使用它以及如何实际实现它?我确实知道,因为您必须创建派生的 class 才能实现虚函数。因此,这里是我导出的 class header:
// this is the derived collection
#include "Collection.h"
#ifndef derivedcollection_H
#define derivedcollection_H
class derivedcollection: public collection
{
public:
collection(); //constructor (error is at this line)
virtual void add(int value) = 0; //adds the value to the array
virtual void remove(int index) = 0; //removes and item from the appropriate index location
virtual void print() = 0; //prints item of the array comma seperated.
virtual int get(int index) = 0; //gets item at a particular index.
virtual int sum() = 0; //gets the sum of the array
virtual int size() = 0; //gets the size of the array
};
#endif
我的下一个也是最后一个问题更像是一个我不明白的简单错误。对于 collection(); 所在的行在我的派生 class 派生collection 中声明,我收到一个错误,指出 "explicit type is missing ('int' assumed)"。虽然这通常是一个很容易修复的错误,但老实说,我有点困惑为什么会出现这个错误。当我在我的派生 class header 而不是我的基础 class header?
中声明默认构造函数时,它是如何给我这个错误的?有大量关于 virtual
方法的文献和 YouTube 视频。关于错误,您的构造函数应该是 derivedcollection()
,而不是 collection()
。
有助于实现动态多态性(OOP 的支柱)的虚函数在网络上的许多教程中都有解释。
其中之一是explained here
virtual void add(int value) = 0;
表示它是一个纯虚函数,可能没有任何定义。派生 class 必须 实现其定义。
请注意,collection
不是派生的 class derivedcollection
.
因此,编译器假定其 return 类型为整数。即它是一种简单的方法。
构造必须是 derivedcollection()
而不是 collection()
。