我有一个B类,它创建一个A类的对象并调用该对象的方法.
啊
#ifndef A_H
#define A_H
class A
{
public:
A(int);
void function();
};
#endif // A_H
Run Code Online (Sandbox Code Playgroud)
a.cpp
#include "a.h"
A::A(int x)
{
}
void A::function(){
//Do something.
}
Run Code Online (Sandbox Code Playgroud)
BH
#ifndef B_H
#define B_H
#include <QVector>
#include <a.h>
class B
{
public:
B(int);
QVector<A> list;
};
#endif // B_H
Run Code Online (Sandbox Code Playgroud)
b.cpp
#include "b.h"
B::B(int y)
{
list.append(A(y));
list[0].function();
}
Run Code Online (Sandbox Code Playgroud)
问题是这不能编译.它返回"没有匹配的函数来调用'A:A()'".我知道这可以通过前向声明来解决,但这在这里不起作用,因为我想调用函数"function".我也不想把全班A都包括在B班.
我创建了一个新的数据类型:
data Human = Human [Names] Age
deriving(Eq,Show)
type Names = String
type Age = Int
Run Code Online (Sandbox Code Playgroud)
现在我想访问Human类型对象的[Names]元素:
human1 = Human ["FirstName","LastName"] 22
Run Code Online (Sandbox Code Playgroud)
有一个简单的方法可以为我的例子做这个Names human1
吗?