我有以下問題:我想從好老C
項目重做C++
,使一切class(y) :)
並保持它從一開始可擴展性。c + +使用一個類中的一類(一類),並調用其功能
它是細胞的網格上的一個模擬(A羣被部分),所以決定以下結構:
class Simulation has an instance of
class Grid has an instance of
class Swarm has an instance of
class Cell
我在單獨的頭文件中定義的類。那麼當然,我需要能夠在grid,swarm和cell中調用函數。我想要做的直截了當:
Simulation mysim;
mysim.get_grid(0).any_function_here();
與電網作爲返回參數
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->test;//##//this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
它調用的函數,並且只要在電網未進行任何更改的作品。這些似乎在太空中消失了。可能是一個指針錯誤,但我不能找到一個錯誤,因爲完全相同的代碼工作的Simulation
類...
更多來源:
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
我可以隨時調用create_swarm
功能我想要,但是羣體永遠不會出現,並且櫃檯不會在該網格中提升,只要功能在那裏即可。我錯過了什麼嗎?它真的只是一個指針錯誤?爲什麼這段代碼工作,如果我這樣稱呼它:
Grid newgrid;
newgrid.create_swarm();
一個快速ç& p'ed MWE
#include <iostream>
#include <string>
#include <vector>
class Sim
{
public:
Sim();
virtual ~Sim();
Grid get_grid(int grid_no);
protected:
private:
std::vector<Grid> gridlist;
int amount_of_grids = -1;
};
class Grid
{
public:
Grid();
virtual ~Grid();
int set_size(int x, int y);
int create_swarm(std::string name);
Swarm get_swarm(int swarm_no);
void print_swarms();
protected:
private:
std::vector<Swarm> swarmlist;
int amount_of_swarms = -1;
/*static const*/ int size_x;
/*static const*/ int size_y;
std::vector<std::vector<Field>> fields;
std::string gridname;
};
Grid Sim::get_grid(int grid_no)
{
std::cout << "sim.get_grid(" << grid_no << ") called." << std::endl;
if (grid_no <= amount_of_grids)
return this->gridlist[grid_no];
else
std::cout << "you have not created this grid number yet" << std::endl;
Grid dummy;
return dummy;
}
int Grid::create_swarm(std::string name)
{
Swarm new_swarm;
new_swarm.set_name("Protoswarm");
swarmlist.push_back(new_swarm);
this->amount_of_swarms ++;
std::cout << "amount_of_swarms = " << amount_of_swarms << std::endl;
return 0;
}
Swarm Grid::get_swarm(int swarm_no)
{
std::cout << "grid.get_swarm(" << swarm_no << ") called." << std::endl;
if (swarm_no <= amount_of_swarms)
return swarmlist[swarm_no];
else
std::cout << "oh oh - you have not this swarm in here..." << std::endl;
Swarm dummy;
return dummy;
}
using namespace std;
int main(int argc, char* argv[])
{
Sim mysim;
mysim.create_grid();
mysim.get_grid(0).create_swarm("Alpha-Swarm");
mysim.get_grid(0).create_swarm("Betaa-Swarm"); //doesn't work
Grid newgrid;
newgrid.create_swarm("Gamma-Swarm");
newgrid.create_swarm("Delta-Swarm"); // works, but is not needed.
return 0;
}
請儘可能包含[MCVE]。 – tambre
在C++中返回一個類就好像在「good old」C中返回一個結構一樣;它會創建一個副本。 – molbdnilo
沒有[mcve]很難說,但看起來問題是你沒有返回向量中數據的引用。 – NathanOliver