如何在另一个类中创建对象
How to create objects inside another class?
我有这个代码。
#include "Stack.h"
template <class dataType>
class Queue2{
public:
Queue2(int size);
bool push(int data);
bool pop(int &data);
bool isEmpty();
bool isFull();
bool top(int &data);
~Queue2();
};
template <class dataType>
Queue2<dataType>::Queue2(int size = 10) : Stack <dataType> obj1(size), Stack <dataType> obj2(size) {//here i am facing an error. how can i fix it
}
我有一个完整的类Stack,其构造函数是这样的。
Stack(int size=10);
现在我想在Queue2类中创建两个Stack类的对象。
如果你给Queue2
类两个私有Stack
成员,你可以在构造函数初始化中初始化它们,并单独访问它们:
class Queue2{
Stack<dataType> left,right;
public:
Queue2(int size);
/* ... */
然后将构造函数定义为:
template<typename dataType>
Queue2<dataType>::Queue2(int size = 10)
: left(size), right(size) {}
相关文章:
- C++ 如何在将新对象分配给另一个对象时创建新对象
- 如何创建一个共享对象与另一个.所以在Cmake
- 我有一个类,它创建了另一个类的实例.如何将变量通过第一个类传递到第二个类的实例化中?
- 编译问题 C++ 同时,尝试通过调用另一个对象中的成员函数来创建 std:: 线程
- 从主函数中删除在另一个函数中使用 new 创建的一系列对象
- 在另一个对象 B 中创建对象 A 时,对象 A 是否是对象 B 的本地对象,对象 A 是否会存在于对象 B 的实例化之外?
- 无法将类对象创建为另一个类的成员
- 在类中创建指针到另一个对象
- 创建在另一个项目中定义的类对象
- 在矢量内创建一个对象,该对象由另一个对象包含
- 将向量从另一个对象添加到创建对象的对象
- 使用另一个(现有)对象创建新对象时会发生什么情况?
- 在一个线程中创建一个对象,并使用std :: Atomic访问另一个对象
- 用另一个对象作为构造函数中的参数创建对象
- 如何在另一个对象的构造函数中声明和创建对象?
- 如何使用在另一个类的构造函数中的堆栈上接受参数的构造函数创建对象
- 如何创建从另一个派生的两个ATL接口
- 创建新对象似乎会更改之前创建的另一个对象 (???)
- 删除对象(另一个..)时双重释放或损坏
- 创建指向另一个类中对象的链表时遇到问题