标签: stdvector

指针模板清除功能的向量无法使用"未定义的引用"消息进行编译

对于我的程序,我做了一个小函数来清除我所拥有的各种std ::指针向量.

template <class S>
void clearPtrVector(std::vector<S*> &a,int size)
{
    for(size_t i = 0; i < size; i++)
         delete a[i];

    a.clear();
}
Run Code Online (Sandbox Code Playgroud)

我必须在这里做错了,因为在析构函数中调用此函数是这样的:

clearPtrVector(neurons,neurons.size());
Run Code Online (Sandbox Code Playgroud)

我两次得到以下未定义的引用:

undefined reference to `void clearPtrVector<Neuron>(std::vector<Neuron*,std::allocator<Neuron*> >&, int)'
Run Code Online (Sandbox Code Playgroud)

我不得不承认我不熟悉std :: allocator是什么,所以我无法猜出这里可能出现的问题.任何帮助都非常感谢.提前致谢!

-Lefteris

c++ templates stl stdvector

0
推荐指数
1
解决办法
1710
查看次数

C++用户定义的向量

如何在允许用户输入定义向量名称的同时在c ++中声明向量?好的,在审核了您的回复之后,这里有更详细的信息; 以下是来自VS08 C++控制台应用程序的错误消息 -

Error 2 error C2664: 'std::basic_istream<_Elem,_Traits> &std::basic_istream<_Elem,_Traits>::get(_Elem *,std::streamsize)' : cannot convert parameter 1 from 'std::istream' to 'char *' e:\c++\project 5\project 5\project 5.cpp 58 project 5
Run Code Online (Sandbox Code Playgroud)

这是代码:

void addRecord()
{
     vector<char>recordName;
     vector<inventory>recordNameUser;
     cout << "Name the record you want to store it as\n";
     cin.get(cin, recordName);
     cout << "Enter the item description\n";
     cin.get(cin, recordNameUser.itemDescription);
     cout << "Enter the quanity on hand\n";
     cin >> recordNameUser.quanityOnHand;
     cout << "Enter the wholesale cost\n";
     cin >> recordNameUser.wholesaleCost;
     cout << "Enter the retail …
Run Code Online (Sandbox Code Playgroud)

c++ stdvector

0
推荐指数
1
解决办法
677
查看次数

std :: vector中止问题

我正在考虑下面的例子,最后一行产生了一个"abort has called called"错误.我不明白为什么会这样.

在这种情况下,我使用(*abc).def而不是abc-> def.

#include <iostream>
#include <string>
#include <vector>

class branch
{
    public:
        unsigned short n;
        std::vector<branch> branches;

        branch left()
        {
            return branches.at(0);
        }
};

void main()
{
    branch trunk = branch();
        trunk.n = 0;
        branch b1, b2;
        b1.n = 0;
        b2.n = 5;
        b1.branches.push_back(b2);
        trunk.branches.push_back(b1);

    branch* focus1 = &(trunk.branches.at(0));
    branch* focus3 = &(trunk.left());

    std::cout<<trunk.left().branches.at(0).n<<std::endl; // ok
    std::cout<<(*focus1).branches.at(0).n<<std::endl; // ok
    std::cout<<(*focus1).left().n<<std::endl; // ok
    std::cout<<(*focus3).branches.at(0).n<<std::endl; // problem
}
Run Code Online (Sandbox Code Playgroud)

c++ vector abort stdvector

0
推荐指数
1
解决办法
1425
查看次数

检查std :: vector索引是否未超出范围的最快方法是什么?

通常,我想确保自己一次处于函数主体的顶部。我执行此操作的不同方法是:

// I don't like this much as it stops the program when I may not want to
assert( idx < v.size() );

if( !(idx < v.size()) )
{
    // take corrective action ...
}

if( v.size() <= idx )
{
    // take corrective action ..
}
Run Code Online (Sandbox Code Playgroud)

在第二种和第三种方法之间(也许还有其他方法),哪种方法更有效?

c++ stdvector

0
推荐指数
1
解决办法
5065
查看次数

C++:从stringstream到char**

我有一个带有parse(int argc, char* argv[])函数的类,我必须使用它来设置对象的所需状态.我正在使用gui中的参数stringstream,然后我试图将它们转换为char**以将它们传递给函数.这是我得到的:

std::stringstream sstream;

sstream << "-clip" << " " << min_x_entry.get_text()
        << " " << max_x_entry.get_text(); // etc.

std::cout << sstream.str();    // All looks good here

std::vector<std::string> args;
std::vector<char*> argv;
std::string arg;

while (sstream >> arg)
{
    args.push_back(arg);
    argv.push_back(const_cast<char*>(args.back().c_str()));
}
argv.push_back(0);

int argc = args.size();

for (int i = 0; i < argc; ++i)
    std::cout << &argv[0][i];    // This outputs garbage

my_object.parse(argc, &argv[0])  // And this fails
Run Code Online (Sandbox Code Playgroud)

我错过了什么?有没有更好的方法来实现这一目标?

c++ string stringstream stdvector

0
推荐指数
1
解决办法
1207
查看次数

如何在std :: vector中访问存储为指针的接口实现对象

所以我有这个:

std::vector<EnemyInterface*> _activeEnemies;
Run Code Online (Sandbox Code Playgroud)

EnemyInterface看起来像这样:

#include "Ogre.h"

class EnemyInterface{
public:
  virtual void update(const Ogre::Real deltaTime) = 0;
  virtual void takeDamage(const int amountOfDamage, const int typeOfDamage) = 0;
  virtual Ogre::Sphere getWorldBoundingSphere() const = 0;
  virtual ~EnemyInterface(){} 
};
Run Code Online (Sandbox Code Playgroud)

我创造了一个新的敌人:

// Spikey implements EnemyInterface
activeEnemies.push_back( (EnemyInterface*) &Spikey(_sceneManager, Ogre::Vector3(8,0,0)) );
Run Code Online (Sandbox Code Playgroud)

我想在每个敌人身上调用更新功能,但它崩溃了:

// update enemies
for (std::vector<EnemyInterface*>::iterator it=_activeEnemies.begin(); it!=_activeEnemies.end(); ++it){
        (**it).update(timeSinceLastFrame); // Option 1: access violation reading location 0xcccccccc
        (*it)->update(timeSinceLastFrame); // Option 2: access violation reading location0xcccccccc
    }
Run Code Online (Sandbox Code Playgroud)

我可以在屏幕上看到敌人,但我无法访问它.任何帮助,将不胜感激.

Spikey.h看起来像这样:

#include "EnemyInterface.h"

class Spikey: virtual public …
Run Code Online (Sandbox Code Playgroud)

c++ iterator interface stdvector

0
推荐指数
1
解决办法
166
查看次数

std :: vector :: erase/remove_if导致nullptr

我有一个std::shared_ptr<MotionTask>对象矢量,我偶尔需要清理它.

// this assert passes
assert(std::all_of(d_tasks.begin(), d_tasks.end(),
       [](shared_ptr<MotionTask> task) { return bool(task); }));

// Remove any committed tasks for which the corresponding module has completed
d_tasks.erase(
  remove_if(
    d_tasks.begin(), 
    d_tasks.end(),
    [module](shared_ptr<MotionTask> const& task)
    {
      return task->isCommitted() && task->getModule() == module;
    }
  )
);

// this assert fails
assert(std::all_of(d_tasks.begin(), d_tasks.end(),
       [](shared_ptr<MotionTask> task) { return bool(task); }));
Run Code Online (Sandbox Code Playgroud)

最后一个assert是失败的,因为在任务的向量中,一个是null(false).

我不明白如何调用erase会使成员无效.我无法在单元测试中重现这一点.

是否有可以从上面的代码中看到的解释,如果没有,我可以尝试调试这个?

c++ stdvector

0
推荐指数
1
解决办法
1203
查看次数

有和没有初始化的std :: vector分段错误

我正在用C++中的向量进行一些实验.我正在使用的代码如下

#include <vector>
#include <iostream>

int main()
{
   std::vector<float> aVector = std::vector<float>(10);  // IMPORTANT LINE
   std::cout << "Vector size: " << aVector.size() << std::endl;
   aVector.clear();
   aVector[0] = 2.2;
   std::cout << "Vector size: " << aVector.size() << std::endl;
   aVector.push_back(0.1);
   std::cout << "Vector size: " << aVector.size() << std::endl;
   aVector.clear();
   std::cout << "Vector size: " << aVector.size() << std::endl;
   aVector[0] = 2.1;
   std::cout << "Vector size: " << aVector.size() << std::endl;

   return 0;
}
Run Code Online (Sandbox Code Playgroud)

如果我有向量,用N个元素初始化它,然后清除向量并尝试放置赋值aVector[0] = 1.1,程序一直到最后没有分段错误.

另一方面,如果我创建一个向量但是没有传递初始元素的数量,那么当尝试使用aVector[0] = …

c++ stdvector

0
推荐指数
1
解决办法
4258
查看次数

将清除共享智能指针上的呼叫重置

我最近改变了我的原始指针,std::shared_ptr在这样的容器中使用:

std::vector<std::shared_ptr<AbstractPathContainer>> mGeneratedPaths;
Run Code Online (Sandbox Code Playgroud)

当我打电话clear()给这个容器时,它会reset()在每个容器上调用方法std::shared_ptr吗?

c++ smart-pointers reset shared-ptr stdvector

0
推荐指数
1
解决办法
266
查看次数

如何将现有对象push_back到共享指针向量?

使用原始指针,我可以创建一个指针和push_back地址的向量,如下所示:

Entity objEntity;
std::vector<Entity*> Entities;
Entities.push_back(&objEntity);
Run Code Online (Sandbox Code Playgroud)

如果我改为使用共享指针向量:

std::vector<std::shared_ptr<Entity>> Entities;
Run Code Online (Sandbox Code Playgroud)

...如何推送地址?

据我所知,std :: shared_ptr :: reset用于将现有对象的地址分配给智能指针.我是否需要先创建一个临时指针,调用reset,然后再调用push_back?

std::shared_ptr<Entity> temp;
temp.reset(&objEntity);
Entities.push_back(temp);
Run Code Online (Sandbox Code Playgroud)

c++ smart-pointers stdvector

0
推荐指数
2
解决办法
1930
查看次数