【发布时间】:2019-06-08 10:12:58
【问题描述】:
我有一个 B 类,它创建一个 A 类的对象并调用该对象的一个方法。
啊.h
#ifndef A_H
#define A_H
class A
{
public:
A(int);
void function();
};
#endif // A_H
a.cpp
#include "a.h"
A::A(int x)
{
}
void A::function(){
//Do something.
}
b.h
#ifndef B_H
#define B_H
#include <QVector>
#include <a.h>
class B
{
public:
B(int);
QVector<A> list;
};
#endif // B_H
b.cpp
#include "b.h"
B::B(int y)
{
list.append(A(y));
list[0].function();
}
问题是这不能编译。它返回“没有匹配的函数来调用'A:A()'”。我知道这可以通过前向声明来解决,但这在这里不起作用,因为我想调用函数“函数”。我也不想将整个 A 类包含在 B 类中。
【问题讨论】:
-
看起来 Qt 要求使用默认构造函数。
-
您的解释不太正确。 “没有匹配的函数来调用'A:A()'”意味着
A没有默认构造函数,但你正在尝试调用它