注册 登录
编程论坛 VC++/MFC

求教这个该怎么改,谢谢

xl327134332 发布于 2013-05-25 20:40, 588 次点击
#include <iostream>
#include <string>
using std::string;
int main()
{
 int *pci_bad=new  int[100];
 const string *pci_ok=new const string[100]();
    return 0;
}
//error C2468: 'new' : cannot allocate 'const'/'volatile' objects (type is 'const class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > [100]')


[ 本帖最后由 xl327134332 于 2013-5-25 21:10 编辑 ]
9 回复
#2
xl3271343322013-05-25 21:11
目的是创建动态数组
#3
xl3271343322013-05-26 22:27
没有哪位高手帮我解答一下嘛,我纯新手不知道怎么创建动态数组
#4
RobinWang2013-06-03 20:54
VC++代码如下:(如有问题可继续讨论)
#include <iostream>
using namespace std;
#include <string.h>
class MyArray
{public:
MyArray(int len)
{p=new int[len];
length=len;
}
void in()
{cout<<"please inpit 4 int:";
for(int i=0;i<length;i++)
cin>>p[i];
}
void out()
{for(int i=0;i<length;i++)
cout<<p[i]<<"";
cout<<endl;
}
~MyArray()
{delete[]p;
}
private:
int*p;
int length;
};
int main()
{int len;
cout<<"len:";
cin>>len;
MyArray al(len);
al.in();
al.out();
return 0;
}
#5
邓士林2013-06-03 21:56
什么意思
#6
lonmaor2013-06-04 10:21
常量必须在定义的时候就赋值,怎么能生成动态的呢?
#7
q2152362132013-06-05 16:06
版主说的不错!
楼主可以看看 const 方面的内容
const string *pci_ok=new const string[100]();
这个左边是一个指向的对象是常量指针,常量指针在定义的时候就要初始化,这里不能用数组的!
#8
无梦小左2013-06-05 18:33
好像没问题吧,我vs2010可以编译啊
#9
雪狼633812013-06-06 19:45
去掉const试试看!
#10
miccy2013-06-13 17:12
没有赋值,赞同版主
1