用c ++中的向量填充数组

时间:2018-05-01 13:40:50

标签: c++ arrays

我是c ++的新手,想要实现以下目标:

2.4.1 :002 > d = Deck.new(name: "Deck t")
(0.5ms)  SET NAMES utf8,  @@SESSION.sql_mode = CONCAT(CONCAT(@@sql_mode, ',STRICT_ALL_TABLES'), ',NO_AUTO_VALUE_ON_ZERO'),  @@SESSION.sql_auto_is_null = 0, @@SESSION.wait_timeout = 2147483
=> #<Deck id: nil, user_id: nil, name: "Deck t", created_at: nil, updated_at: nil> 
2.4.1 :003 > u = User.new(hp: 20, armor: 20, choosen_deck: d)
=> #<User id: nil, choosen_deck_id: nil, hp: 20, armor: 20, created_at: nil, updated_at: nil> 
2.4.1 :004 > d.user
=> #<User id: nil, choosen_deck_id: nil, hp: 20, armor: 20, created_at: nil, updated_at: nil> 
2.4.1 :005 > d.save
(0.2ms)  BEGIN
User Create (0.3ms)  INSERT INTO `users` (`hp`, `armor`, `created_at`, `updated_at`) VALUES (20, 20, '2018-05-01 13:53:18', '2018-05-01 13:53:18')
Deck Create (0.2ms)  INSERT INTO `decks` (`user_id`, `name`, `created_at`, `updated_at`) VALUES (1, 'Deck t', '2018-05-01 13:53:18', '2018-05-01 13:53:18')
(5.7ms)  COMMIT
=> true 
2.4.1 :007 > d2 = Deck.create(name: "Deck 2", user: u)
(0.2ms)  BEGIN
Deck Create (0.3ms)  INSERT INTO `decks` (`user_id`, `name`, `created_at`, `updated_at`) VALUES (1, 'Deck 2', '2018-05-01 13:53:57', '2018-05-01 13:53:57')
(4.0ms)  COMMIT
=> #<Deck id: 2, user_id: 1, name: "Deck 2", created_at: "2018-05-01      13:53:57", updated_at: "2018-05-01 13:53:57"> 
2.4.1 :008 > u
=> #<User id: 1, choosen_deck_id: nil, hp: 20, armor: 20, created_at: "2018-05-01 13:53:18", updated_at: "2018-05-01 13:53:18"> 
2.4.1 :009 > u.choosen_deck
=> #<Deck id: 1, user_id: 1, name: "Deck t", created_at: "2018-05-01 13:53:18", updated_at: "2018-05-01 13:53:18"> 
2.4.1 :010 > u.save
(0.2ms)  BEGIN
(0.1ms)  COMMIT
=> true 
2.4.1 :011 > d.save
(0.2ms)  BEGIN
(0.2ms)  COMMIT
=> true 
2.4.1 :012 > d2.save
(0.2ms)  BEGIN
(0.1ms)  COMMIT
=> true 

问题是,我不知道值并且必须动态填充此数组。

我尝试使用以下功能实现它:

 ttgl::vec3f positions[] = {
    ttgl::vec3f(-1.0f, 1.0f, 0.0f),
    ttgl::vec3f(1.0f, 1.0f, 0.0f),
    ttgl::vec3f(1.0f, -1.0f, 0.0f),
    ttgl::vec3f(1.0f, -1.0f, 0.0f),
    ttgl::vec3f(-1.0f, -1.0f, 0.0f),
    ttgl::vec3f(-1.0f, 1.0f, 0.0f),

};

这会导致以下错误:

void getCirclePositions(GLfloat radius, GLint sides) {
ttgl::vec3f center = ttgl::vec3f(0.0f, 0.0f, 0.0f);
GLfloat angle = (2.0f * M_PI) / sides;
ttgl::vec3f positions[100];
positions[0] = center;


for (int i = 1; i <= sides; i++) {
    GLfloat angleFan = angle * (i + 1);
    GLfloat xCoordinate = radius * cos(angleFan);
    GLfloat yCoordinate = radius * sin(angleFan);
    ttgl::vec3f point = ttgl::vec3f(xCoordinate, yCoordinate, 0.0f);
    positions[i] = point;


}
return positions;
};

如何正确插入值?

修改

该函数调用如下:

Run-Time Check Failure #2 - Stack around the variable 'positions' was 
corrupted.

我相应地编辑了代码并解决了错误。谢谢你。

 getCirclePositions(1.0f, 100);

};

如何打印此阵列?

0 个答案:

没有答案