关于STL和类模板在.h下的定义

olegunnar1983 2009-03-03 07:39:48
本想用STL list或者自己定义的类模板 实现 一个长度为n的队列

ex:
A.h
class A
{
A();
~A();
list <int> list1;
list <int> list2(10);
}

list2(10)那行会报错 error C2059: 语法错误 : “常数” why?

弱问想要将指定长度的list实例作为一个类声明的属性应该如何做?

类模板也有同样如上问题
...全文
96 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
arong1234 2009-03-04
  • 打赏
  • 举报
回复
构造函数不能在类定义内调用吧,你要使用指定长度,需要在A的构造函数的初始化列表内做
A::A():list2(10) {};

[Quote=引用楼主 olegunnar1983 的帖子:]
本想用STL list或者自己定义的类模板 实现 一个长度为n的队列

ex:
A.h
class A
{
A();
~A();
list <int> list1;
list <int> list2(10);
}

list2(10)那行会报错 error C2059: 语法错误 : “常数” why?

弱问想要将指定长度的list实例作为一个类声明的属性应该如何做?

类模板也有同样如上问题
[/Quote]
jakqigle 2009-03-04
  • 打赏
  • 举报
回复

ex:
A.h
class A
{
A();
~A();
list <int> list1;//分号
list <int> list2(10);
}
  • 打赏
  • 举报
回复
我只说2个地方,首先,楼主类的声明结尾没有;符号
2.构造函数没有声明为public:,随便说下list <int> list1; 这个;号你用的中文输入法.
ysysbaobei 2009-03-04
  • 打赏
  • 举报
回复
太乙 2009-03-03
  • 打赏
  • 举报
回复
// constructing lists
#include <iostream>
#include <list>
using namespace std;

int main ()
{
// constructors used in the same order as described above:
list<int> first; // empty list of ints
list<int> second (4,100); // four ints with value 100
list<int> third (second.begin(),second.end()); // iterating through second
list<int> fourth (third); // a copy of third

// the iterator constructor can also be used to construct from arrays:
int myints[] = {16,2,77,29};
list<int> fifth (myints, myints + sizeof(myints) / sizeof(int) );

cout << "The contents of fifth are: ";
for (list<int>::iterator it = fifth.begin(); it != fifth.end(); it++)
cout << *it << " ";

cout << endl;

return 0;
}

65,210

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧