关于vector的const问题

degree_37 2009-03-09 05:00:36
我在使用QVector的时候(QT的内容,好象和STL的vector一样的),一旦对QVector中的内容进行了初始化,就无法改变

QVector<QGrid3d> grids;

在应用时,会出现如下的错误:

error C2662: 'QVector<T>::append' : cannot convert 'this' pointer from 'const QVector<T>' to 'QVector<T> &'

error C3892: 'gridID' : you cannot assign to a variable that is const

请问如何解决这个问题呢? 因为我必须对已经初始化的QVector其中的内容进行改变.
...全文
296 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
degree_37 2009-03-10
  • 打赏
  • 举报
回复
会是因为这个么?
const T & QVector::at ( int i ) const
degree_37 2009-03-10
  • 打赏
  • 举报
回复

class QGrid3d : public QBBox3d
{
public:
//publid Datas
long int GridID;
QVector<int> includeGroupID;
QVector<int> includeFaceID;
bool includeFaces;
QVertex3d center;
};

//下面是第一次用到,没问题
void QScene::InitGrids()
{
CalStepNum();
QGrid3d curGrid;
int i,j,k,currentID;
int steps = pow(2.0,stepNum);
QVertex3d orginalPoint = sceneBox.pMin;

float xDirStep = (sceneBox.pMax.x - sceneBox.pMin.x)/(float)steps;
float yDirStep = (sceneBox.pMax.y - sceneBox.pMin.y)/(float)steps;
float zDirStep = (sceneBox.pMax.z - sceneBox.pMin.z)/(float)steps;

QVertex3d orginalCenterpoint = QVertex3d(orginalPoint.x+xDirStep/2.0, orginalPoint.y+yDirStep/2.0, orginalPoint.z+zDirStep/2.0);

for (i=0; i<steps; i++)
for(j=0; j<steps; j++)
for (k=0; k<steps; k++)
{
currentID = i*steps*steps + j*steps +k;
curGrid.GridID = currentID;
curGrid.includeFaces = false;
curGrid.center = QVertex3d(orginalCenterpoint.x+xDirStep*i,
orginalCenterpoint.y+yDirStep*j,
orginalCenterpoint.z+zDirStep*k);
}
}

//下面是对它进行了修改,就出现了题目中的错误
void QScene::PutFacesintoGrid()
{
QGroup3d curGroup;
QFace3d curFace;
long int gridID;
CalDivValues(sceneBox.pMin.x, sceneBox.pMax.x, stepNum, xdivValue);
CalDivValues(sceneBox.pMin.y, sceneBox.pMax.y, stepNum, ydivValue);
CalDivValues(sceneBox.pMin.z, sceneBox.pMax.z, stepNum, zdivValue);
for (int i=0; i<numgroups; i++)
{
curGroup = groups.at(i);
for (int j =0; j<curGroup.numfaces; j++)
{
curFace = curGroup.m_ArrayFace[j];
CalGridID(curFace.faceCenter, gridID);
grids.at(gridID).includeGroupID.append(i);
grids.at(gridID).includeFaceID.append(j);
grids.at(gridID).includeFaces = true;
}
}
}

howema 2009-03-10
  • 打赏
  • 举报
回复
const修饰的变量不能被修改的
xxweilw 2009-03-10
  • 打赏
  • 举报
回复
不明白的话看http://www.stlchina.org/download/Eff_STL_CN.pdf条款27
xxweilw 2009-03-10
  • 打赏
  • 举报
回复
用这种迭带器技术
advance(i,distance<ConstIter>(i,ci));
degree_37 2009-03-10
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 akirya 的回复:]
就是这个.at返回的内容是不可更改的
[/Quote]

那我怎么修改它,让它达到可以更改的呢? 用const_cast? 不过具体如何用呢? 我之前也尝试过 不过还是出错...
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 degree_37 的回复:]
会是因为这个么?
const T & QVector::at ( int i ) const
[/Quote]

就是这个.at返回的内容是不可更改的
  • 打赏
  • 举报
回复
有QVector?
QT4里加的吧?先把你用到QVector的代码发出来看看..
xuhb95083023 2009-03-09
  • 打赏
  • 举报
回复
容器定义成const有意义吗?是不是函数参数里有const qvector<T>&类型?
yangch_nhcmo 2009-03-09
  • 打赏
  • 举报
回复
类型不匹配问题吧,楼主把代码贴出来看看
lq651659889 2009-03-09
  • 打赏
  • 举报
回复
QT的内容,好象和STL的vector一样的.楼主问题都出达应该不一样吧!
sagegz 2009-03-09
  • 打赏
  • 举报
回复
信息太少,LZ贴下错误代码吧.
sagegz 2009-03-09
  • 打赏
  • 举报
回复
信息太少,LZ贴下错误代码吧.
bitwwzhang130 2009-03-09
  • 打赏
  • 举报
回复
多贴点代码
  • 打赏
  • 举报
回复
你光贴这个看不出来啥,你最好弄一个能出现这种错误的代码贴出来.

65,211

社区成员

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

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