首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >对象向量上迭代器的分割错误

对象向量上迭代器的分割错误
EN

Stack Overflow用户
提问于 2013-06-23 00:54:40
回答 1查看 134关注 0票数 0

使用->和*时,通过迭代器访问类参数和函数都会失败。我找了很多帖子都没找到。它不会让我提交问题,除非添加更多的细节,所以,嗯,co代表连接,并且一整天都在重复检查const和指针引用。

截图:

代码语言:javascript
运行
复制
vector<Axon>::iterator ait;
ait=axl.begin();
const Neuron* co=ait->get_conn();

饮食可编译代码:

代码语言:javascript
运行
复制
#include <iostream>
#include <vector>
using namespace std;

class Neuron;
class Axon;
class Neuron{
friend class Axon;
public:
    Neuron(int);
    void excite(double);
    void connect(const Neuron *);
    //void grow_ax();
private:
    double _exc;
    vector<Axon> axl;   
};
class Axon{
    friend class Neuron;
public:
    Axon();
    const Neuron* get_conn();
private:
    double cond; //conductivity
    const Neuron* connection;
};
Neuron::Neuron(int numax){
        _exc=0;
    vector<Axon> axl (numax,Axon());
}
Axon::Axon(){
    cond=1;
    connection=0;
}
void Neuron::connect(const Neuron * nP){
    vector<Axon>::iterator ait;
    ait=axl.begin();
    const Neuron* co=ait->get_conn(); //error is here,
    //if (ait->connection==0)           these don't work
            //ait->connection=nP;       <===
}
const Neuron* Axon::get_conn(){
    return connection;
}

int main(){
    Neuron n1=Neuron(1);
    Neuron n2=Neuron(0);
    Neuron* n2p=&n2;
    n1.connect(n2p);
}

提前感谢

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-23 00:58:38

代码语言:javascript
运行
复制
ait=axl.begin();
const Neuron* co=ait->get_conn();

axl不包含任何元素,因此,如果是axl.begin() == axl.end(),您将取消对它的引用。取消定义std::vector::end()是一种未定义的行为。

看看你的构造函数:

代码语言:javascript
运行
复制
Neuron::Neuron(int numax){
    _exc=0;
    vector<Axon> axl (numax,Axon());
    // this is a declaration of local object
    // which will be destroyed at the end of the constructor
}

另请注意,这一点:

代码语言:javascript
运行
复制
Neuron n1=Neuron(1);
Neuron n2=Neuron(0);

应该是

代码语言:javascript
运行
复制
Neuron n1(1);
Neuron n2(0);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17253120

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档