[英]Member variable initialization with non-default constructor gives error
robots.h:11:20: 錯誤:數字常量隊列 SQ(10) 之前的預期標識符;
struct Robot
{
string m_name; //robot name
Queue<string> SQ(10); //queue of services
int m_timer; //time til robot is done
string m_lastService; //most recent service
};
我不明白為什么我會在這方面出錯。 當我拿走 (10) 時,它使用了默認構造函數並且工作正常。 這是 Queue 類。
template <typename T>
class Queue : public AbstractQueue<T>
{
private:
T* m_array;
int m_front;
int m_back;
int m_capacity;
int m_size;
public:
Queue();
Queue(int max);
void setMax(int max);
bool isEmpty() const;
const T& front() const throw (Oops);
const T& back() const throw (Oops);
void enqueue(const T& x);
void dequeue();
void clear();
~Queue();
};
我使用了 Queue 類的另一個聲明,它在 main 中工作,但由於某種原因它在結構中不起作用,這是我在 main 中聲明的
Queue<Robot> R(10);
您不能像這樣指定用於成員變量初始化的非默認構造函數。
您可以提供一個帶有成員初始值設定項列表的構造函數來指定要使用的Queue
的構造函數。
struct Robot
{
string m_name; //robot name
Queue<string> SQ(); //queue of services
int m_timer; //time til robot is done
string m_lastService; //most recent service
Robot() : SQ(10) {}
};
或者使用默認成員初始值設定項(c++11 起):
Queue<string> SQ{10};
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.