By user1429101

2013-07-05 17:57:17 8 Comments

For some reason, resizing this two dimensional vector in my class does not work. Thanks for your help!

Map::Map(const unsigned int& w,
         const unsigned int& h,
         const unsigned int& s)
    : width(w), height(h), size(s)
    for (unsigned int i = 0; i < width; ++i)
        squares[i].resize(height); // error here
        for (unsigned int j = 0; j < height; ++j)
            squares[i][j] = Square(sf::Vector2f(i * size, j * size));

std::vector<std::vector<Square>> squares;

Some error messages:

c:\mingw\bin..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_uninitialized.h|481| instanziiert von »static void std::__uninitialized_default_n_1<TrivialValueType>::_uninit_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Square*, _Size = unsigned int, bool _TrivialValueType = false]«|

c:\mingw\bin..\lib\gcc\mingw32\4.6.2\include\c++\bits\stl_uninitialized.h|529| instanziiert von »void std::__uninitialized_default_n(_ForwardIterator, _Size) [with _ForwardIterator = Square*, _Size = unsigned int]«|


@aryjczyk 2013-07-05 18:28:39

When you vector.resize(n) and n > vector.size() you are asking it to create n - vector.size() new elements. Vector needs to default construct them, since he doesn't know what arguments to use. If they are not default constructible, resize fails.

However, if you don't want Square to be default constructible you can change failing line to vector.reserve() - that only makes sure there is enough space for new elements, but doesn't actually create them. Then you can create them one by one in a loop using

squares[i].emplace_back(sf::Vector2f(i * size, j * size));

Oh, and by the way passing primitives by reference has suboptimal performance. Pass them by value unless you need them as out parameters.

@user1429101 2013-07-06 14:44:02

So is it a good practice to only use const references in method parameters when it's not a primitive type?

@aryjczyk 2013-07-06 14:56:02

in general if you are not passing an out argument it should be either passed by value or by const reference. there are 2 differences 1) performance - passing by value copies the object, passing by ref is under the hood passing by pointer, so there is a level of indirection, so you want to pass small things by value and big ones by reference. i've read somewhere, that 16B is a good threshold on x86, but it's more of a guideline than a rule. if you find yourself writing code that passes a lot of small structs in perf-critical areas you should profile it and see what's best

@aryjczyk 2013-07-06 14:57:54

2) if you want to perform some intermediate non-const computation on an argument, you would need to copy it anyway, so it's okay to pass it by value even if it's big

Related Questions

Sponsored Content

7 Answered Questions

[SOLVED] How can I use std::maps with user-defined types as key?

  • 2009-07-09 07:42:27
  • unknown
  • 80803 View
  • 53 Score
  • 7 Answer
  • Tags:   c++ stl maps

4 Answered Questions

[SOLVED] How can I use a struct as key in a std::map?

  • 2011-08-26 11:56:15
  • user181351
  • 31276 View
  • 21 Score
  • 4 Answer
  • Tags:   c++ c++11 stdmap

1 Answered Questions

[SOLVED] How to fix overloading operands +?

1 Answered Questions

[SOLVED] std::set.insert won't compile with custom class

0 Answered Questions

minGW error on locale library

1 Answered Questions

[SOLVED] Unable to insert smart pointer into map

  • 2017-08-20 06:12:01
  • Esther
  • 94 View
  • 0 Score
  • 1 Answer
  • Tags:   c++

4 Answered Questions

[SOLVED] Appending a vector to a vector

  • 2010-03-31 09:33:02
  • sub
  • 465837 View
  • 596 Score
  • 4 Answer
  • Tags:   c++ stl vector

1 Answered Questions

[SOLVED] Can't push_back an unique_ptr in a vector

2 Answered Questions

[SOLVED] Why can I not call reserve on a vector of const elements?

  • 2013-09-06 07:27:03
  • Chris
  • 4590 View
  • 4 Score
  • 2 Answer
  • Tags:   c++ vector

2 Answered Questions

[SOLVED] Why is the error "invalid conversion from 'char' to 'const char*"?

  • 2012-12-31 17:51:29
  • aucguy
  • 3687 View
  • 1 Score
  • 2 Answer
  • Tags:   c++

Sponsored Content