您好,我想对对象使用范围基循环,但它似乎不起作用。这是我的代码:
#include <iostream>
#include <vector>
using namespace std;
class animal
{
public:
animal ();
void speak() {cout << "hi" ;}
};
int main()
{
animal a;
animal b;
animal c;
for ( animal * ptr : { &a , &b , &c } )
{
ptr->speak();
}
}
最佳答案
您需要为您的构造函数提供一个定义:
animal () { }
或者删除它,因为它什么都不做。编译器会为您提供一个。
关于c++ - 对象的 Range Base 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20552033/