2012-08-02 50 views
0

我在C++中创建了一个无效的环境函数中的向量调用核和单元。然后我调用创建病毒点向量的病毒函数。然后我想访问病毒调用的另一个功能中的载体细胞核和细胞。是否有可能在不通过病毒功能的情况下调用病毒?如果不是什么是最好的办法做到这一点?附件是我的代码。先谢谢了。我也没有添加的所有代码的函数内...只通过代码中的某些功能传递向量

struct point { 
     float x; 
     float y; 
    }; 
    struct space{ 
     point point1; 
     point point2; 
     point point3; 
     point point4; 
    }; 

     int gelato(vector<point> & virus,int& times,int& iv, ofstream& data_file) 
    { 
     int a; 
     int b; 

     bool nc,cc,cmc; 
     for(int i =0; i<virus.size(); i++) 
     { 
      a = virus[i].x; 
      b = virus[i].y; 

      nc = nucleus_check(nucleus,a,b); // **Need to call vector cell and nucleus** 
      cc = cytoplasm_check(cell,a,b); 
      cmc = cell_membrane_check(cell,a,b); 
     } 
    } 

int moves(char n) 
{ 
    int moved; 
    int trial; 

    if(n =='A') 
    { 
     moved = rand()%4; 
    } 
    else if(n=='B') 
    { 
     moved= rand()%3+1; 
    } 
    else if(n=='C') 
    { 
     trial= rand()%4; 
     if(trial ==1) 
     { 
      moves('C'); 
     } 
     else 
     { 
      moved = trial; 
     } 
    } 
    else if(n =='D') 
    { 
     trial = rand()%4; 
     if(trial == 2) 
     { 
      moves('D'); 
     } 
     else 
     { 
      moved = trial; 
     } 
    } 
    else if(n=='E') 
    { 
     moved = rand()%3; 
    } 

    return moved; 
} 
int v_move(vector<point>& virus, int& iv, ofstream& data_file) 
{ 

     gelato(virus,times,iv,data_file); 
} 
int rand_in(char a) 
{} 
void virus(ofstream& data_file) 
{ 

    v_move(virus, iv, data_file); 
} 
void cell_space(int r) 
{ 

    vector<point>cell; 
} 
void nucleus_space(int r) 
{ 

    vector<point>nucleus; 
} 

void environment() //**Where the vector nucleus and cell are created** 
{ 
    //cell 
    cell_space(16) 

    //nucleus 
    nucleus_space(4); 
    //cout<<"Environment"<<endl; 
} 
int main() 
{ 
    srand(time(NULL)); 
    data_file.open("data.csv"); 
    environment(); 
    virus(data_file); 

    return 0; 
} 

回答

0

我假设你还没有学会/掌握面向对象编程(OOP),以及您想使用来实现你的程序procedural programming paradigm。这很好,因为C++支持多种范例,并且不会强制您使用OOP。

由于“细胞空间”和“核空间”是指将您拨打的“环境”的实体一起使用时,你应该定义一个environment结构,结合了两个:

struct environment 
{ 
    vector<point> cells; 
    vector<point> nuclei; 
}; 

功能,准备环境看起来是这样的:

void prepare_environment(environment& env, // environment passed by reference 
         int cellCount, int nucleusCount) 
{ 
    prepare_cells(env.cells, cellCount); 
    prepare_nuclei(env.nuclei, nucleusCount); 
}; 

void prepare_cells(std::vector<point>& cells, // cell vector passed by reference 
        int cellCount) 
{ 
    cells.resize(cellCount); 
    // Do other stuff to initialize cells 
} 

void prepare_nuclei(std::vector<point>& nuclei, int cellCount) {...} 

在你的主,你通过环境结构需要操作它的功能:

int main() 
{ 
    Environment env; 

    prepare_environment(env, 16, 4); 
    move_virusus(env); 
} 

在程序编程范例中,数据与对该数据进行操作的程序保持分离。数据不断作为帮助函数链中的参数传递是正常的。抵制将数据存储在全局变量中的诱惑,并让您的函数直接访问这些全局变量。全局变量导致代码难以测试,难以阅读并且难以重复使用。

您应该在执行动作后指定您的程序。名称中应该有某种动作动词,例如prepare_environment。如果该过程对某种数据进行操作,则该数据的名称成为该动词的对象,例如, prepare_environment

相反,你应该在你的结构在他们建模的实体之后命名。名称中应该有一个名词,例如environment

一旦你了解了过程编程的原理,我想你会发现它更容易理解面向对象编程。